Skip to content

Commit

Permalink
feat: pack set-registry
Browse files Browse the repository at this point in the history
Signed-off-by: Travis <longoria.public@gmail.com>
  • Loading branch information
elbandito committed Oct 6, 2020
1 parent 7378ebe commit 3be55f1
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
rootCmd.AddCommand(commands.Report(logger, pack.Version))

if cfg.Experimental {
rootCmd.AddCommand(commands.ListBuildpackRegistries(logger, cfg))
rootCmd.AddCommand(commands.AddBuildpackRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.ListBuildpackRegistries(logger, cfg))
rootCmd.AddCommand(commands.RegisterBuildpack(logger, cfg, &packClient))
rootCmd.AddCommand(commands.SetRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.YankBuildpack(logger, cfg, &packClient))
}

Expand Down
54 changes: 54 additions & 0 deletions internal/commands/set_default_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package commands

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
)

func SetRegistry(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
var (
registryName string
)

cmd := &cobra.Command{
Use: "set-default-registry <name>",
Args: cobra.ExactArgs(1),
Short: "Set default registry",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
registryName = args[0]
if !containsRegistry(config.GetRegistries(cfg), registryName) {
return errors.Errorf("no registry with the name %s exists", style.Symbol(registryName))
}

if cfg.DefaultRegistryName != registryName {
cfg.DefaultRegistryName = registryName
err := config.Write(cfg, cfgPath)
if err != nil {
return err
}
}

logger.Infof("Successfully set %s as the default registry", style.Symbol(registryName))

return nil
}),
}
cmd.Example = "pack set-default-registry myregistry"
AddHelpFlag(cmd, "set-default-registry")

return cmd
}

func containsRegistry(registries []config.Registry, registry string) bool {
for _, r := range registries {
if r.Name == registry {
return true
}
}

return false
}
78 changes: 78 additions & 0 deletions internal/commands/set_default_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package commands_test

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/config"
ilogging "github.com/buildpacks/pack/internal/logging"
h "github.com/buildpacks/pack/testhelpers"
)

func TestSetDefaultRegistry(t *testing.T) {
color.Disable(true)
defer color.Disable(false)

spec.Run(t, "Commands", testSetDefaultRegistryCommand, spec.Parallel(), spec.Report(report.Terminal{}))
}

func testSetDefaultRegistryCommand(t *testing.T, when spec.G, it spec.S) {
when("#SetDefaultRegistry", func() {
var (
outBuf bytes.Buffer
logger = ilogging.NewLogWithWriters(&outBuf, &outBuf)
tmpDir string
configFile string
assert = h.NewAssertionManager(t)
)

it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "pack-home-*")
assert.Nil(err)

configFile = filepath.Join(tmpDir, "config.toml")
})

it.After(func() {
_ = os.RemoveAll(tmpDir)
})

it("should set the default registry", func() {
cfg := config.Config{
Registries: []config.Registry{
{
Name: "myregistry",
URL: "https://github.com/buildpacks/registry-index",
Type: "Github",
},
},
}
command := commands.SetRegistry(logger, cfg, configFile)
command.SetArgs([]string{"myregistry"})
assert.Succeeds(command.Execute())

cfg, err := config.Read(configFile)
assert.Nil(err)

assert.Equal(cfg.DefaultRegistryName, "myregistry")
})

it("should fail if no corresponding registry exists", func() {
command := commands.SetRegistry(logger, config.Config{}, configFile)
command.SetArgs([]string{"myregistry"})
assert.Error(command.Execute())

output := outBuf.String()
h.AssertContains(t, output, "no registry with the name 'myregistry' exists")
})
})
}

0 comments on commit 3be55f1

Please sign in to comment.