Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Joe Kutner <jpkutner@gmail.com> Signed-off-by: Travis Longoria <longoria.public@gmail.com>
- Loading branch information
Showing
9 changed files
with
598 additions
and
6 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,44 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack" | ||
"github.com/buildpacks/pack/internal/style" | ||
|
||
"github.com/buildpacks/pack/internal/config" | ||
"github.com/buildpacks/pack/logging" | ||
) | ||
|
||
type RegisterBuildpackFlags struct { | ||
BuildpackRegistry string | ||
} | ||
|
||
func RegisterBuildpack(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command { | ||
var opts pack.RegisterBuildpackOptions | ||
var flags RegisterBuildpackFlags | ||
|
||
cmd := &cobra.Command{ | ||
Use: "register-buildpack <image>", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Register the buildpack to a registry", | ||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||
registry, err := cfg.GetRegistry(flags.BuildpackRegistry) | ||
if err != nil { | ||
return err | ||
} | ||
opts.ImageName = args[0] | ||
opts.Type = registry.Type | ||
opts.URL = registry.URL | ||
|
||
if err := client.RegisterBuildpack(cmd.Context(), opts); err != nil { | ||
return err | ||
} | ||
logger.Infof("Successfully registered %s", style.Symbol(opts.ImageName)) | ||
return nil | ||
}), | ||
} | ||
cmd.Flags().StringVarP(&flags.BuildpackRegistry, "buildpack-registry", "R", "", "Buildpack Registry name") | ||
AddHelpFlag(cmd, "register-buildpack") | ||
return cmd | ||
} |
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,151 @@ | ||
package commands_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/buildpacks/pack/internal/commands" | ||
|
||
"github.com/buildpacks/pack" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/commands/testmocks" | ||
"github.com/buildpacks/pack/internal/config" | ||
ilogging "github.com/buildpacks/pack/internal/logging" | ||
"github.com/buildpacks/pack/logging" | ||
h "github.com/buildpacks/pack/testhelpers" | ||
) | ||
|
||
func TestRegisterBuildpackCommand(t *testing.T) { | ||
spec.Run(t, "Commands", testRegisterBuildpackCommand, spec.Parallel(), spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testRegisterBuildpackCommand(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
command *cobra.Command | ||
logger logging.Logger | ||
outBuf bytes.Buffer | ||
mockController *gomock.Controller | ||
mockClient *testmocks.MockPackClient | ||
cfg config.Config | ||
) | ||
|
||
it.Before(func() { | ||
logger = ilogging.NewLogWithWriters(&outBuf, &outBuf) | ||
mockController = gomock.NewController(t) | ||
mockClient = testmocks.NewMockPackClient(mockController) | ||
cfg = config.Config{} | ||
|
||
command = commands.RegisterBuildpack(logger, cfg, mockClient) | ||
}) | ||
|
||
it.After(func() {}) | ||
|
||
when("#RegisterBuildpackCommand", func() { | ||
when("no image is provided", func() { | ||
it("fails to run", func() { | ||
err := command.Execute() | ||
h.AssertError(t, err, "accepts 1 arg") | ||
}) | ||
}) | ||
|
||
when("image name is provided", func() { | ||
var ( | ||
buildpackImage string | ||
) | ||
|
||
it.Before(func() { | ||
buildpackImage = "buildpack/image" | ||
}) | ||
|
||
it("should work for required args", func() { | ||
opts := pack.RegisterBuildpackOptions{ | ||
ImageName: buildpackImage, | ||
Type: "github", | ||
URL: "https://github.com/buildpacks/registry", | ||
} | ||
|
||
mockClient.EXPECT(). | ||
RegisterBuildpack(gomock.Any(), opts). | ||
Return(nil) | ||
|
||
command.SetArgs([]string{buildpackImage}) | ||
h.AssertNil(t, command.Execute()) | ||
}) | ||
|
||
when("config.toml exists", func() { | ||
it("should consume registry config values", func() { | ||
cfg = config.Config{ | ||
DefaultRegistryRef: "berneuse", | ||
Registries: []config.Registry{ | ||
{ | ||
Name: "berneuse", | ||
Type: "github", | ||
URL: "https://github.com/berneuse/buildpack-registry", | ||
}, | ||
}, | ||
} | ||
command = commands.RegisterBuildpack(logger, cfg, mockClient) | ||
opts := pack.RegisterBuildpackOptions{ | ||
ImageName: buildpackImage, | ||
Type: "github", | ||
URL: "https://github.com/berneuse/buildpack-registry", | ||
} | ||
|
||
mockClient.EXPECT(). | ||
RegisterBuildpack(gomock.Any(), opts). | ||
Return(nil) | ||
|
||
command.SetArgs([]string{buildpackImage}) | ||
h.AssertNil(t, command.Execute()) | ||
}) | ||
|
||
it("should handle config errors", func() { | ||
cfg = config.Config{ | ||
DefaultRegistryRef: "missing registry", | ||
} | ||
command = commands.RegisterBuildpack(logger, cfg, mockClient) | ||
command.SetArgs([]string{buildpackImage}) | ||
|
||
err := command.Execute() | ||
h.AssertNotNil(t, err) | ||
}) | ||
}) | ||
|
||
it("should support buildpack-registry flag", func() { | ||
buildpackRegistry := "override" | ||
cfg = config.Config{ | ||
DefaultRegistryRef: "default", | ||
Registries: []config.Registry{ | ||
{ | ||
Name: "default", | ||
Type: "github", | ||
URL: "https://github.com/default/buildpack-registry", | ||
}, | ||
{ | ||
Name: "override", | ||
Type: "github", | ||
URL: "https://github.com/override/buildpack-registry", | ||
}, | ||
}, | ||
} | ||
opts := pack.RegisterBuildpackOptions{ | ||
ImageName: buildpackImage, | ||
Type: "github", | ||
URL: "https://github.com/override/buildpack-registry", | ||
} | ||
mockClient.EXPECT(). | ||
RegisterBuildpack(gomock.Any(), opts). | ||
Return(nil) | ||
|
||
command = commands.RegisterBuildpack(logger, cfg, mockClient) | ||
command.SetArgs([]string{buildpackImage, "--buildpack-registry", buildpackRegistry}) | ||
h.AssertNil(t, command.Execute()) | ||
}) | ||
}) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.