diff --git a/main.go b/main.go index 0505c366ae..43d70b664a 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,10 @@ limitations under the License. package main import ( + "context" "log" "os" + "os/signal" "github.com/google/go-containerregistry/pkg/logs" "github.com/google/ko/pkg/commands" @@ -28,7 +30,9 @@ func main() { logs.Warn.SetOutput(os.Stderr) logs.Progress.SetOutput(os.Stderr) - if err := commands.Root.Execute(); err != nil { + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + if err := commands.Root.ExecuteContext(ctx); err != nil { log.Fatal("error during command execution:", err) } } diff --git a/pkg/commands/apply.go b/pkg/commands/apply.go index 42522c7223..de71724f34 100644 --- a/pkg/commands/apply.go +++ b/pkg/commands/apply.go @@ -85,9 +85,7 @@ func addApply(topLevel *cobra.Command) { if !isKubectlAvailable() { return errors.New("error: kubectl is not available. kubectl must be installed to use ko apply") } - - // Cancel on signals. - ctx := createCancellableContext() + ctx := cmd.Context() bo.InsecureRegistry = po.InsecureRegistry builder, err := makeBuilder(ctx, bo) diff --git a/pkg/commands/build.go b/pkg/commands/build.go index 06a2b36893..9472d6d232 100644 --- a/pkg/commands/build.go +++ b/pkg/commands/build.go @@ -57,8 +57,9 @@ func addBuild(topLevel *cobra.Command) { # This always preserves import paths. ko build --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`, Args: cobra.MinimumNArgs(1), - RunE: func(_ *cobra.Command, args []string) error { - ctx := createCancellableContext() + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + bo.InsecureRegistry = po.InsecureRegistry builder, err := makeBuilder(ctx, bo) if err != nil { diff --git a/pkg/commands/config.go b/pkg/commands/config.go index 71b061a4e4..bd3517344d 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -21,10 +21,8 @@ import ( "fmt" "log" "os" - "os/signal" "strconv" "strings" - "syscall" "time" "github.com/google/go-containerregistry/pkg/authn" @@ -142,16 +140,3 @@ func getCreationTime() (*v1.Time, error) { func getKoDataCreationTime() (*v1.Time, error) { return getTimeFromEnv("KO_DATA_DATE_EPOCH") } - -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 -} diff --git a/pkg/commands/create.go b/pkg/commands/create.go index d37f3e4f48..21874cb144 100644 --- a/pkg/commands/create.go +++ b/pkg/commands/create.go @@ -70,9 +70,7 @@ func addCreate(topLevel *cobra.Command) { if !isKubectlAvailable() { return errors.New("error: kubectl is not available. kubectl must be installed to use ko create") } - - // Cancel on signals. - ctx := createCancellableContext() + ctx := cmd.Context() bo.InsecureRegistry = po.InsecureRegistry builder, err := makeBuilder(ctx, bo) diff --git a/pkg/commands/delete.go b/pkg/commands/delete.go index 7f996e20e1..07f7d8eb43 100644 --- a/pkg/commands/delete.go +++ b/pkg/commands/delete.go @@ -28,27 +28,25 @@ type runCmd func(*cobra.Command, []string) error // passthru returns a runCmd that simply passes our CLI arguments // through to a binary named command. func passthru(command string) runCmd { - return func(_ *cobra.Command, _ []string) error { + return func(cmd *cobra.Command, _ []string) error { if !isKubectlAvailable() { return errors.New("error: kubectl is not available. kubectl must be installed to use ko delete") } - - // Cancel on signals. - ctx := createCancellableContext() + ctx := cmd.Context() // Start building a command line invocation by passing // through our arguments to command's CLI. - cmd := exec.CommandContext(ctx, command, os.Args[1:]...) + ecmd := exec.CommandContext(ctx, command, os.Args[1:]...) // Pass through our environment - cmd.Env = os.Environ() + ecmd.Env = os.Environ() // Pass through our stdfoo - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin + ecmd.Stderr = os.Stderr + ecmd.Stdout = os.Stdout + ecmd.Stdin = os.Stdin // Run it. - return cmd.Run() + return ecmd.Run() } } diff --git a/pkg/commands/deps.go b/pkg/commands/deps.go index 4926e89851..cc2c4d7c34 100644 --- a/pkg/commands/deps.go +++ b/pkg/commands/deps.go @@ -43,7 +43,7 @@ If the image was not built using ko, or if it was built without embedding depend ko deps docker.io/my-user/my-image:v3`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - ctx := createCancellableContext() + ctx := cmd.Context() ref, err := name.ParseReference(args[0]) if err != nil { diff --git a/pkg/commands/resolve.go b/pkg/commands/resolve.go index 59e183b158..c3de69d37b 100644 --- a/pkg/commands/resolve.go +++ b/pkg/commands/resolve.go @@ -55,7 +55,8 @@ func addResolve(topLevel *cobra.Command) { ko resolve --local -f config/`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - ctx := createCancellableContext() + ctx := cmd.Context() + bo.InsecureRegistry = po.InsecureRegistry builder, err := makeBuilder(ctx, bo) if err != nil { diff --git a/pkg/commands/run.go b/pkg/commands/run.go index b863cfc4f3..e72788fdfe 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -49,7 +49,7 @@ func addRun(topLevel *cobra.Command) { # You can also supply args and flags to the command. ko run ./cmd/baz -- -v arg1 arg2 --yes`, RunE: func(cmd *cobra.Command, args []string) error { - ctx := createCancellableContext() + ctx := cmd.Context() // Args after -- are for kubectl, so only consider importPaths before it. importPaths := args