Skip to content

Commit

Permalink
remove-registry command
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 b2a5386
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
rootCmd.AddCommand(commands.ListBuildpackRegistries(logger, cfg))
rootCmd.AddCommand(commands.AddBuildpackRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.RegisterBuildpack(logger, cfg, &packClient))
rootCmd.AddCommand(commands.RemoveRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.YankBuildpack(logger, cfg, &packClient))
}

Expand Down
67 changes: 67 additions & 0 deletions internal/commands/remove_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 RemoveRegistry(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
var (
registryName string
)

cmd := &cobra.Command{
Use: "remove-registry <name>",
Args: cobra.ExactArgs(1),
Short: "Remove registry",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
registryName = args[0]

if registryName == config.OfficialRegistryName {
return errors.Errorf("%s is a reserved registry name, please provide a different registry",
style.Symbol(config.OfficialRegistryName))
}

index := findRegistryIndex(registryName, cfg.Registries)
if index < 0 {
return errors.Errorf("registry %s does not exist", style.Symbol(registryName))
}

updatedRegistries := removeRegistry(index, cfg.Registries)
cfg.Registries = updatedRegistries

if cfg.DefaultRegistryName == registryName {
cfg.DefaultRegistryName = config.OfficialRegistryName
}
config.Write(cfg, cfgPath)

logger.Infof("Successfully removed %s to from registries", style.Symbol(registryName))

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

return cmd
}

func findRegistryIndex(registryName string, registries []config.Registry) int {
for index, r := range registries {
if r.Name == registryName {
return index
}
}

return -1
}

func removeRegistry(index int, registries []config.Registry) []config.Registry {
registries[index] = registries[len(registries)-1]

return registries[:len(registries)-1]
}
126 changes: 126 additions & 0 deletions internal/commands/remove_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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 TestRemoveRegistry(t *testing.T) {
color.Disable(true)
defer color.Disable(false)

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

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

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

cfg = config.Config{
DefaultRegistryName: "buildpack-registry",
Registries: []config.Registry{
{
Name: "buildpack-registry",
URL: "https://github.com/buildpacks/registry-index",
Type: "github",
},
{
Name: "elbandito-registry",
URL: "https://github.com/elbandito/registry-index",
Type: "github",
},
},
}

configFile = filepath.Join(tmpDir, "config.toml")
err = config.Write(cfg, configFile)
assert.Nil(err)
})

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

it("should remove the registry", func() {
command := commands.RemoveRegistry(logger, cfg, configFile)
command.SetArgs([]string{"elbandito-registry"})
assert.Succeeds(command.Execute())

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

assert.Equal(newCfg, config.Config{
DefaultRegistryName: "buildpack-registry",
Registries: []config.Registry{
{
Name: "buildpack-registry",
URL: "https://github.com/buildpacks/registry-index",
Type: "github",
},
},
})
})

it("should remove the registry and matching default registry name", func() {
command := commands.RemoveRegistry(logger, cfg, configFile)
command.SetArgs([]string{"buildpack-registry"})
assert.Succeeds(command.Execute())

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

assert.Equal(newCfg, config.Config{
DefaultRegistryName: config.OfficialRegistryName,
Registries: []config.Registry{
{
Name: "elbandito-registry",
URL: "https://github.com/elbandito/registry-index",
Type: "github",
},
},
})
})

it("should return error when registry does NOT already exist", func() {
command := commands.RemoveRegistry(logger, cfg, configFile)
command.SetArgs([]string{"missing-registry"})
assert.Error(command.Execute())

output := outBuf.String()
h.AssertContains(t, output, "registry 'missing-registry' does not exist")
})

it("should throw error when registry name is official", func() {
command := commands.RemoveRegistry(logger, config.Config{}, configFile)
command.SetArgs([]string{"official"})
assert.Error(command.Execute())

output := outBuf.String()
h.AssertContains(t, output, "'official' is a reserved registry name, please provide a different registry")
})
})
}

0 comments on commit b2a5386

Please sign in to comment.