Skip to content

Commit

Permalink
Move Version to pack package
Browse files Browse the repository at this point in the history
* Move NewPackCommand to cmd package
* Inject pack.Version into internal/builder.Save() method

* Note: The save method can really do with some refactoring, but I don't want to mix it up in this PR

Signed-off-by: David Freilich <dfreilich@vmware.com>
  • Loading branch information
dfreilich committed Jun 10, 2020
1 parent 90a6e49 commit 2410a45
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 147 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tidy: mod-tidy mod-vendor format

build: out
@echo "> Building..."
$(GOCMD) build -ldflags "-s -w -X 'github.com/buildpacks/pack/cmd.Version=${PACK_VERSION}' -extldflags ${LDFLAGS}" -trimpath -o ./out/$(PACK_BIN) -a ./cmd/pack
$(GOCMD) build -ldflags "-s -w -X 'github.com/buildpacks/pack.Version=${PACK_VERSION}' -extldflags ${LDFLAGS}" -trimpath -o ./out/$(PACK_BIN) -a ./cmd/pack

package: out
tar czf ./out/$(ARCHIVE_NAME).tgz -C out/ pack
Expand Down
5 changes: 2 additions & 3 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"

"github.com/buildpacks/pack/cmd"
"github.com/buildpacks/pack/internal/api"
"github.com/buildpacks/pack/internal/archive"
"github.com/buildpacks/pack/internal/build"
Expand Down Expand Up @@ -133,7 +132,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
}
}
if !supportsPlatform {
c.logger.Debugf("pack %s supports Platform API version(s): %s", cmd.Version, strings.Join(build.SupportedPlatformAPIVersions, ", "))
c.logger.Debugf("pack %s supports Platform API version(s): %s", Version, strings.Join(build.SupportedPlatformAPIVersions, ", "))
c.logger.Debugf("Builder %s has Platform API version: %s", style.Symbol(opts.Builder), lcPlatformAPIVersion)
return errors.Errorf("Builder %s is incompatible with this version of pack", style.Symbol(opts.Builder))
}
Expand Down Expand Up @@ -602,7 +601,7 @@ func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[s
bldr.SetOrder(order)
}

if err := bldr.Save(c.logger); err != nil {
if err := bldr.Save(c.logger, Version); err != nil {
return nil, err
}
return bldr, nil
Expand Down
104 changes: 0 additions & 104 deletions cli/pack_command.go

This file was deleted.

103 changes: 100 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,103 @@
package cmd

var (
// Version is the version of `pack`. It is injected at compile time.
Version = "0.0.0"
import (
"github.com/heroku/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/config"
clilogger "github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/logging"
)

// NewPackCommand generates a Pack command
func NewPackCommand(logger *clilogger.LogWithWriters) (*cobra.Command, error) {
cobra.EnableCommandSorting = false
cfg, err := initConfig()
if err != nil {
return nil, err
}

packClient, err := initClient(logger, cfg)
if err != nil {
return nil, err
}

rootCmd := &cobra.Command{
Use: "pack",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if fs := cmd.Flags(); fs != nil {
if flag, err := fs.GetBool("no-color"); err == nil {
color.Disable(flag)
}
if flag, err := fs.GetBool("quiet"); err == nil {
logger.WantQuiet(flag)
}
if flag, err := fs.GetBool("verbose"); err == nil {
logger.WantVerbose(flag)
}
if flag, err := fs.GetBool("timestamps"); err == nil {
logger.WantTime(flag)
}
}
},
}

rootCmd.PersistentFlags().Bool("no-color", false, "Disable color output")
rootCmd.PersistentFlags().Bool("timestamps", false, "Enable timestamps in output")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Show less output")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Show more output")
rootCmd.Flags().Bool("version", false, "Show current 'pack' version")

commands.AddHelpFlag(rootCmd, "pack")

rootCmd.AddCommand(commands.Build(logger, cfg, &packClient))
rootCmd.AddCommand(commands.Rebase(logger, cfg, &packClient))
rootCmd.AddCommand(commands.InspectImage(logger, &cfg, &packClient))
rootCmd.AddCommand(commands.SetRunImagesMirrors(logger, cfg))

rootCmd.AddCommand(commands.SetDefaultBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.InspectBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.SuggestBuilders(logger, &packClient))
rootCmd.AddCommand(commands.TrustBuilder(logger, cfg))
rootCmd.AddCommand(commands.CreateBuilder(logger, cfg, &packClient))

rootCmd.AddCommand(commands.PackageBuildpack(logger, &packClient, buildpackage.NewConfigReader()))

rootCmd.AddCommand(commands.SuggestStacks(logger))

rootCmd.AddCommand(commands.Version(logger, pack.Version))
rootCmd.AddCommand(commands.Report(logger, pack.Version))

rootCmd.AddCommand(commands.CompletionCommand(logger))

rootCmd.Version = pack.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)

return rootCmd, nil
}

func initConfig() (config.Config, error) {
path, err := config.DefaultConfigPath()
if err != nil {
return config.Config{}, errors.Wrap(err, "getting config path")
}

cfg, err := config.Read(path)
if err != nil {
return config.Config{}, errors.Wrap(err, "reading pack config")
}
return cfg, nil
}

func initClient(logger logging.Logger, cfg config.Config) (pack.Client, error) {
client, err := pack.NewClient(pack.WithLogger(logger), pack.WithExperimental(cfg.Experimental))
if err != nil {
return *client, err
}

return *client, nil
}
5 changes: 3 additions & 2 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"os"

"github.com/buildpacks/pack/cmd"

"github.com/heroku/color"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/cli"
"github.com/buildpacks/pack/internal/commands"
clilogger "github.com/buildpacks/pack/internal/logging"
)
Expand All @@ -15,7 +16,7 @@ func main() {
// create logger with defaults
logger := clilogger.NewLogWithWriters(color.Stdout(), color.Stderr())

rootCmd, err := cli.NewPackCommand(logger)
rootCmd, err := cmd.NewPackCommand(logger)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) e
bldr.SetOrder(opts.Config.Order)
bldr.SetStack(opts.Config.Stack)

return bldr.Save(c.logger)
return bldr.Save(c.logger, Version)
}

func (c *Client) validateConfig(ctx context.Context, opts CreateBuilderOptions) error {
Expand Down
5 changes: 2 additions & 3 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/pkg/errors"

"github.com/buildpacks/pack/builder"
"github.com/buildpacks/pack/cmd"
"github.com/buildpacks/pack/internal/api"
"github.com/buildpacks/pack/internal/archive"
"github.com/buildpacks/pack/internal/dist"
Expand Down Expand Up @@ -259,7 +258,7 @@ func (b *Builder) SetStack(stackConfig builder.StackConfig) {
}

// Save saves the builder
func (b *Builder) Save(logger logging.Logger) error {
func (b *Builder) Save(logger logging.Logger, version string) error {
logger.Debugf("Creating builder with the following buildpacks:")
for _, bpInfo := range b.metadata.Buildpacks {
logger.Debugf("-> %s", style.Symbol(bpInfo.FullName()))
Expand Down Expand Up @@ -373,7 +372,7 @@ func (b *Builder) Save(logger logging.Logger) error {

b.metadata.CreatedBy = CreatorMetadata{
Name: packName,
Version: cmd.Version,
Version: version,
}

if err := dist.SetLabel(b.image, metadataLabel, b.metadata); err != nil {
Expand Down
Loading

0 comments on commit 2410a45

Please sign in to comment.