-
Notifications
You must be signed in to change notification settings - Fork 294
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
134 additions
and
1 deletion.
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,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 | ||
} |
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,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") | ||
}) | ||
}) | ||
} |