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

Implement register-buildpack for github type #716

Merged
merged 4 commits into from Jul 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/cmd.go
Expand Up @@ -82,6 +82,10 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
rootCmd.AddCommand(commands.Version(logger, pack.Version))
rootCmd.AddCommand(commands.Report(logger, pack.Version))

if cfg.Experimental {
rootCmd.AddCommand(commands.RegisterBuildpack(logger, cfg, &packClient))
}

rootCmd.AddCommand(commands.CompletionCommand(logger))

rootCmd.Version = pack.Version
Expand Down
1 change: 1 addition & 0 deletions internal/commands/commands.go
Expand Up @@ -24,6 +24,7 @@ type PackClient interface {
CreateBuilder(context.Context, pack.CreateBuilderOptions) error
PackageBuildpack(ctx context.Context, opts pack.PackageBuildpackOptions) error
Build(context.Context, pack.BuildOptions) error
RegisterBuildpack(context.Context, pack.RegisterBuildpackOptions) error
}

func AddHelpFlag(cmd *cobra.Command, commandName string) {
Expand Down
44 changes: 44 additions & 0 deletions internal/commands/register_buildpack.go
@@ -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 := config.GetRegistry(cfg, 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
}
151 changes: 151 additions & 0 deletions internal/commands/register_buildpack_test.go
@@ -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-index",
}

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{
DefaultRegistryName: "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{
DefaultRegistryName: "missing registry",
}
command = commands.RegisterBuildpack(logger, cfg, mockClient)
command.SetArgs([]string{buildpackImage})

err := command.Execute()
h.AssertNotNil(t, err)
elbandito marked this conversation as resolved.
Show resolved Hide resolved
})
})

it("should support buildpack-registry flag", func() {
buildpackRegistry := "override"
cfg = config.Config{
DefaultRegistryName: "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())
})
})
})
}
14 changes: 14 additions & 0 deletions internal/commands/testmocks/mock_pack_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 35 additions & 5 deletions internal/config/config.go
Expand Up @@ -6,14 +6,24 @@ import (

"github.com/BurntSushi/toml"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/style"
)

type Config struct {
RunImages []RunImage `toml:"run-images"`
DefaultBuilder string `toml:"default-builder-image,omitempty"`
DefaultRegistry string `toml:"default-registry-url,omitempty"`
Experimental bool `toml:"experimental,omitempty"`
TrustedBuilders []TrustedBuilder `toml:"trusted-builders,omitempty"`
RunImages []RunImage `toml:"run-images"`
DefaultBuilder string `toml:"default-builder-image,omitempty"`
DefaultRegistry string `toml:"default-registry-url,omitempty"`
DefaultRegistryName string `toml:"default-registry,omitempty"`
Experimental bool `toml:"experimental,omitempty"`
TrustedBuilders []TrustedBuilder `toml:"trusted-builders,omitempty"`
Registries []Registry `toml:"registries,omitempty"`
}

type Registry struct {
Name string `toml:"name"`
Type string `toml:"type"`
URL string `toml:"url"`
}

type RunImage struct {
Expand Down Expand Up @@ -82,3 +92,23 @@ func SetRunImageMirrors(cfg Config, image string, mirrors []string) Config {
cfg.RunImages = append(cfg.RunImages, RunImage{Image: image, Mirrors: mirrors})
return cfg
}

func GetRegistry(cfg Config, registryName string) (Registry, error) {
if registryName == "" {
registryName = cfg.DefaultRegistryName
}
if registryName != "" {
for _, registry := range cfg.Registries {
if registry.Name == registryName {
return registry, nil
}
}
return Registry{}, errors.Errorf("registry %s is not defined in your config file", style.Symbol(registryName))
}

return Registry{
"official",
"github",
"https://github.com/buildpacks/registry-index",
}, nil
}
68 changes: 68 additions & 0 deletions internal/config/config_test.go
Expand Up @@ -174,4 +174,72 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
})
})
})

when("#GetRegistry", func() {
it("should return a default registry", func() {
cfg := config.Config{}

registry, err := config.GetRegistry(cfg, "")

h.AssertNil(t, err)
h.AssertEq(t, registry, config.Registry{
Name: "official",
Type: "github",
URL: "https://github.com/buildpacks/registry-index",
})
})

it("should return the corresponding registry", func() {
cfg := config.Config{
Registries: []config.Registry{
{
Name: "registry",
Type: "github",
URL: "https://github.com/registry/buildpack-registry",
},
},
}

registry, err := config.GetRegistry(cfg, "registry")

h.AssertNil(t, err)
h.AssertEq(t, registry, config.Registry{
Name: "registry",
Type: "github",
URL: "https://github.com/registry/buildpack-registry",
})
})

it("should return the first matched registry", func() {
cfg := config.Config{
Registries: []config.Registry{
{
Name: "duplicate registry",
Type: "github",
URL: "https://github.com/duplicate1/buildpack-registry",
},
{
Name: "duplicate registry",
Type: "github",
URL: "https://github.com/duplicate2/buildpack-registry",
},
},
}

registry, err := config.GetRegistry(cfg, "duplicate registry")

h.AssertNil(t, err)
h.AssertEq(t, registry, config.Registry{
Name: "duplicate registry",
Type: "github",
URL: "https://github.com/duplicate1/buildpack-registry",
})
})

it("should return an error when mismatched", func() {
cfg := config.Config{}
_, err := config.GetRegistry(cfg, "missing")
h.AssertError(t, err, "registry 'missing' is not defined in your config file")
})
})
}