Skip to content

Commit

Permalink
Add Context to root Cobra command
Browse files Browse the repository at this point in the history
 - Bump cobra to 0.0.6 to get context support
 - Creates root command context and threads through subcommands

Signed-off-by: Zander Mackie <[email protected]>
  • Loading branch information
zmackie committed Mar 19, 2020
1 parent 007ab0c commit 364a176
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 96 deletions.
3 changes: 2 additions & 1 deletion cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func main() {
rootCmd.Version = cmd.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)

if err := rootCmd.Execute(); err != nil {
ctx := commands.CreateCancellableContext()
if err := rootCmd.ExecuteContext(ctx); err != nil {
if commands.IsSoftError(err) {
os.Exit(2)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/opencontainers/selinux v1.3.3 // indirect
github.com/pkg/errors v0.9.1
github.com/sclevine/spec v1.4.0
github.com/spf13/cobra v0.0.5
github.com/spf13/cobra v0.0.6
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
Expand Down
27 changes: 27 additions & 0 deletions go.sum

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type BuildFlags struct {

func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cobra.Command {
var flags BuildFlags
ctx := createCancellableContext()

cmd := &cobra.Command{
Use: "build <image-name>",
Expand Down Expand Up @@ -80,7 +79,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
}
}

if err := packClient.Build(ctx, pack.BuildOptions{
if err := packClient.Build(cmd.Context(), pack.BuildOptions{
AppPath: flags.AppPath,
Builder: flags.Builder,
AdditionalMirrors: getMirrors(cfg),
Expand Down
26 changes: 13 additions & 13 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ func AddHelpFlag(cmd *cobra.Command, commandName string) {
cmd.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for '%s'", commandName))
}

func CreateCancellableContext() context.Context {
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())

go func() {
<-signals
cancel()
}()

return ctx
}

func logError(logger logging.Logger, f func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
Expand All @@ -47,19 +60,6 @@ func multiValueHelp(name string) string {
return fmt.Sprintf("\nRepeat for each %s in order,\n or supply once by comma-separated list", name)
}

func createCancellableContext() context.Context {
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())

go func() {
<-signals
cancel()
}()

return ctx
}

func getMirrors(config config.Config) map[string][]string {
mirrors := map[string][]string{}
for _, ri := range config.RunImages {
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type CreateBuilderFlags struct {

func CreateBuilder(logger logging.Logger, client PackClient) *cobra.Command {
var flags CreateBuilderFlags
ctx := createCancellableContext()
cmd := &cobra.Command{
Use: "create-builder <image-name> --builder-config <builder-config-path>",
Args: cobra.ExactArgs(1),
Expand All @@ -35,7 +34,7 @@ func CreateBuilder(logger logging.Logger, client PackClient) *cobra.Command {
}

imageName := args[0]
if err := client.CreateBuilder(ctx, pack.CreateBuilderOptions{
if err := client.CreateBuilder(cmd.Context(), pack.CreateBuilderOptions{
BuilderName: imageName,
Config: builderConfig,
Publish: flags.Publish,
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/package_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type BuildpackPackager interface {

func PackageBuildpack(logger logging.Logger, client BuildpackPackager, packageConfigReader PackageConfigReader) *cobra.Command {
var flags PackageBuildpackFlags
ctx := createCancellableContext()
cmd := &cobra.Command{
Use: "package-buildpack <image-name> --package-config <package-config-path>",
Args: cobra.ExactArgs(1),
Expand All @@ -40,7 +39,7 @@ func PackageBuildpack(logger logging.Logger, client BuildpackPackager, packageCo
}

imageName := args[0]
if err := client.PackageBuildpack(ctx, pack.PackageBuildpackOptions{
if err := client.PackageBuildpack(cmd.Context(), pack.PackageBuildpackOptions{
Name: imageName,
Config: config,
Publish: flags.Publish,
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

func Rebase(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command {
var opts pack.RebaseOptions
ctx := createCancellableContext()

cmd := &cobra.Command{
Use: "rebase <image-name>",
Expand All @@ -20,7 +19,7 @@ func Rebase(logger logging.Logger, cfg config.Config, client PackClient) *cobra.
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
opts.RepoName = args[0]
opts.AdditionalMirrors = getMirrors(cfg)
if err := client.Rebase(ctx, opts); err != nil {
if err := client.Rebase(cmd.Context(), opts); err != nil {
return err
}
logger.Infof("Successfully rebased image %s", style.Symbol(opts.RepoName))
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/suggest_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SuggestBuilders(logger logging.Logger, client PackClient) *cobra.Command {
Use: "suggest-builders",
Short: "Display list of recommended builders",
Args: cobra.NoArgs,
Run: func(*cobra.Command, []string) {
Run: func(cmd *cobra.Command, s []string) {
suggestBuilders(logger, client)
},
}
Expand Down
5 changes: 3 additions & 2 deletions vendor/github.com/spf13/cobra/.gitignore

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

9 changes: 3 additions & 6 deletions vendor/github.com/spf13/cobra/.travis.yml

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

105 changes: 67 additions & 38 deletions vendor/github.com/spf13/cobra/README.md

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

15 changes: 11 additions & 4 deletions vendor/github.com/spf13/cobra/bash_completions.go

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

4 changes: 2 additions & 2 deletions vendor/github.com/spf13/cobra/cobra.go

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

Loading

0 comments on commit 364a176

Please sign in to comment.