-
Notifications
You must be signed in to change notification settings - Fork 294
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
Add builder subcommand #965
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eeed910
Make commands funcs public, to allow for easier usage when distributi…
dfreilich b6e9922
Create builder subcommand
dfreilich 19b0d52
Move into commands package, and make functions private
dfreilich 39d6fda
Use fix for pack.Supports instead of feature test
dfreilich 9caf489
Merge main into branch
dfreilich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,22 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/config" | ||
"github.com/buildpacks/pack/logging" | ||
) | ||
|
||
func NewBuilderCommand(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "builder", | ||
Aliases: []string{"builders"}, | ||
Short: "Interact with builders", | ||
RunE: nil, | ||
} | ||
|
||
cmd.AddCommand(BuilderCreate(logger, cfg, client)) | ||
cmd.AddCommand(BuilderSuggest(logger, client)) | ||
AddHelpFlag(cmd, "builder") | ||
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,100 @@ | ||||||
package commands | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
|
||||||
"github.com/pkg/errors" | ||||||
"github.com/spf13/cobra" | ||||||
|
||||||
"github.com/buildpacks/pack" | ||||||
"github.com/buildpacks/pack/builder" | ||||||
pubcfg "github.com/buildpacks/pack/config" | ||||||
"github.com/buildpacks/pack/internal/config" | ||||||
"github.com/buildpacks/pack/internal/style" | ||||||
"github.com/buildpacks/pack/logging" | ||||||
) | ||||||
|
||||||
// BuilderCreateFlags define flags provided to the CreateBuilder command | ||||||
type BuilderCreateFlags struct { | ||||||
BuilderTomlPath string | ||||||
Publish bool | ||||||
Registry string | ||||||
Policy string | ||||||
} | ||||||
|
||||||
// CreateBuilder creates a builder image, based on a builder config | ||||||
func BuilderCreate(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command { | ||||||
var flags BuilderCreateFlags | ||||||
|
||||||
cmd := &cobra.Command{ | ||||||
Use: "create <image-name> --config <builder-config-path>", | ||||||
Args: cobra.ExactArgs(1), | ||||||
Short: "Create builder image", | ||||||
Example: "pack builder create my-builder:bionic --config ./builder.toml", | ||||||
Long: `A builder is an image that bundles all the bits and information on how to build your apps, such as buildpacks, an implementation of the lifecycle, and a build-time environment that pack uses when executing the lifecycle. When building an app, you can use community builders; you can see our suggestions by running | ||||||
|
||||||
pack builders suggest | ||||||
|
||||||
Creating a custom builder allows you to control what buildpacks are used and what image apps are based on. For more on how to create a builder, see: https://buildpacks.io/docs/operator-guide/create-a-builder/. | ||||||
`, | ||||||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||||||
if err := validateCreateFlags(&flags, cfg); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
pullPolicy, err := pubcfg.ParsePullPolicy(flags.Policy) | ||||||
if err != nil { | ||||||
return errors.Wrapf(err, "parsing pull policy %s", flags.Policy) | ||||||
} | ||||||
|
||||||
builderConfig, warns, err := builder.ReadConfig(flags.BuilderTomlPath) | ||||||
if err != nil { | ||||||
return errors.Wrap(err, "invalid builder toml") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
} | ||||||
for _, w := range warns { | ||||||
logger.Warnf("builder configuration: %s", w) | ||||||
} | ||||||
|
||||||
imageName := args[0] | ||||||
if err := client.CreateBuilder(cmd.Context(), pack.CreateBuilderOptions{ | ||||||
BuilderName: imageName, | ||||||
Config: builderConfig, | ||||||
Publish: flags.Publish, | ||||||
Registry: flags.Registry, | ||||||
PullPolicy: pullPolicy, | ||||||
}); err != nil { | ||||||
return err | ||||||
} | ||||||
logger.Infof("Successfully created builder image %s", style.Symbol(imageName)) | ||||||
logging.Tip(logger, "Run %s to use this builder", style.Symbol(fmt.Sprintf("pack build <image-name> --builder %s", imageName))) | ||||||
return nil | ||||||
}), | ||||||
} | ||||||
|
||||||
cmd.Flags().StringVarP(&flags.Registry, "buildpack-registry", "R", cfg.DefaultRegistryName, "Buildpack Registry by name") | ||||||
if !cfg.Experimental { | ||||||
cmd.Flags().MarkHidden("buildpack-registry") | ||||||
} | ||||||
cmd.Flags().StringVarP(&flags.BuilderTomlPath, "config", "c", "", "Path to builder TOML file (required)") | ||||||
cmd.Flags().BoolVar(&flags.Publish, "publish", false, "Publish to registry") | ||||||
cmd.Flags().StringVar(&flags.Policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always") | ||||||
|
||||||
AddHelpFlag(cmd, "create") | ||||||
return cmd | ||||||
} | ||||||
|
||||||
func validateCreateFlags(flags *BuilderCreateFlags, cfg config.Config) error { | ||||||
if flags.Publish && flags.Policy == pubcfg.PullNever.String() { | ||||||
return errors.Errorf("--publish and --pull-policy never cannot be used together. The --publish flag requires the use of remote images.") | ||||||
} | ||||||
|
||||||
if flags.Registry != "" && !cfg.Experimental { | ||||||
return pack.NewExperimentError("Support for buildpack registries is currently experimental.") | ||||||
} | ||||||
|
||||||
if flags.BuilderTomlPath == "" { | ||||||
return errors.Errorf("Please provide a builder config path, using --config.") | ||||||
} | ||||||
|
||||||
return nil | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since many of the command descriptions include "builder", it was falsely returning true, even though the initial line said "Unknown help topic". This ensures that we don't get false positives.