-
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.
Signed-off-by: Travis <longoria.public@gmail.com>
- Loading branch information
Showing
3 changed files
with
194 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,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] | ||
} |
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,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") | ||
}) | ||
}) | ||
} |