Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pack set-default-registry command #876

Merged
merged 6 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.SetDefaultRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.RemoveRegistry(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 SetDefaultRegistry(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",
Example: "pack set-default-registry myregistry",
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
}),
}
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, "SetDefaultRegistryCommand", 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.SetDefaultRegistry(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.SetDefaultRegistry(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")
})
})
}