-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add buildpack registry configuration
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/style" | ||
|
||
"github.com/buildpacks/pack/internal/config" | ||
"github.com/buildpacks/pack/logging" | ||
) | ||
|
||
func AddBuildpackRegistry(logger logging.Logger, cfg config.Config) *cobra.Command { | ||
var setDefault bool | ||
|
||
cmd := &cobra.Command{ | ||
Use: "add-buildpack-registry <name> <type> <url>", | ||
Args: cobra.ExactArgs(3), | ||
Short: "Adds a new buildpack registry to your pack config file", | ||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||
newRegistry := config.Registry{ | ||
Name: args[0], | ||
Type: args[1], | ||
URL: args[2], | ||
} | ||
|
||
err := addRegistry(newRegistry, cfg, setDefault) | ||
if err != nil { | ||
return errors.Wrapf(err, "add buildpack registry") | ||
} | ||
logger.Infof("Successfully added %s to buildpack registries", style.Symbol(newRegistry.Name)) | ||
|
||
return nil | ||
}), | ||
} | ||
cmd.Example = "pack add-buildpack-registry myregistry github https://github.com/buildpacks/mybuildpack" | ||
cmd.Flags().BoolVar(&setDefault, "default", false, "Set this buildpack registry as the default") | ||
AddHelpFlag(cmd, "add-registry") | ||
|
||
return cmd | ||
} | ||
|
||
func addRegistry(registry config.Registry, cfg config.Config, setDefault bool) error { | ||
for _, r := range cfg.Registries { | ||
if r.Name == registry.Name { | ||
return errors.Errorf( | ||
"Buildpack registry %s already exists. First run %s and try again.", | ||
style.Symbol(registry.Name), | ||
style.Symbol(fmt.Sprintf("remove-buildpack-registry %s", registry.Name))) | ||
} | ||
} | ||
|
||
if setDefault { | ||
cfg.DefaultRegistryName = registry.Name | ||
} | ||
cfg.Registries = append(cfg.Registries, registry) | ||
configPath, err := config.DefaultConfigPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return config.Write(cfg, configPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package commands_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/heroku/color" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/commands" | ||
"github.com/buildpacks/pack/internal/config" | ||
ilogging "github.com/buildpacks/pack/internal/logging" | ||
"github.com/buildpacks/pack/logging" | ||
h "github.com/buildpacks/pack/testhelpers" | ||
) | ||
|
||
func TestAddBuildpackRegistry(t *testing.T) { | ||
color.Disable(true) | ||
defer color.Disable(false) | ||
|
||
spec.Run(t, "Commands", testAddBuildpackRegistryCommand, spec.Parallel(), spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testAddBuildpackRegistryCommand(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
command *cobra.Command | ||
logger logging.Logger | ||
outBuf bytes.Buffer | ||
cfg config.Config | ||
) | ||
|
||
when("AddBuildpackRegistry", func() { | ||
it.Before(func() { | ||
logger = ilogging.NewLogWithWriters(&outBuf, &outBuf) | ||
cfg = config.Config{} | ||
|
||
command = commands.AddBuildpackRegistry(logger, cfg) | ||
}) | ||
|
||
it("fails with missing args", func() { | ||
err := command.Execute() | ||
h.AssertError(t, err, "accepts 3 arg") | ||
}) | ||
|
||
it("should successfully add a new registry", func() { | ||
command.SetArgs([]string{"bp", "https://github.com/buildpacks/registry-index/", "github"}) | ||
command.Execute() | ||
|
||
output := outBuf.String() | ||
h.AssertContains(t, output, "Successfully added 'bp' to buildpack registries") | ||
}) | ||
|
||
it("should throw error when registry already exists", func() { | ||
cfg = config.Config{ | ||
Registries: []config.Registry{ | ||
{ | ||
Name: "bp", | ||
Type: "github", | ||
URL: "https://github.com/buildpacks/registry-index/", | ||
}, | ||
}, | ||
} | ||
command = commands.AddBuildpackRegistry(logger, cfg) | ||
command.SetArgs([]string{"bp", "https://github.com/buildpacks/registry-index/", "github"}) | ||
command.Execute() | ||
|
||
output := outBuf.String() | ||
h.AssertContains(t, output, "Buildpack registry 'bp' already exists. First run 'remove-buildpack-registry bp' and try again.") | ||
}) | ||
}) | ||
} |