diff --git a/pkg/commands/apply.go b/pkg/commands/apply.go index 42522c7223..dba4f42d85 100644 --- a/pkg/commands/apply.go +++ b/pkg/commands/apply.go @@ -15,11 +15,13 @@ package commands import ( + "context" "errors" "fmt" "log" "os" "os/exec" + "os/signal" "strings" "github.com/google/ko/internal" @@ -87,7 +89,8 @@ func addApply(topLevel *cobra.Command) { } // Cancel on signals. - ctx := createCancellableContext() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() bo.InsecureRegistry = po.InsecureRegistry builder, err := makeBuilder(ctx, bo) diff --git a/pkg/commands/build.go b/pkg/commands/build.go index 06a2b36893..d1f301e891 100644 --- a/pkg/commands/build.go +++ b/pkg/commands/build.go @@ -15,7 +15,10 @@ package commands import ( + "context" "fmt" + "os" + "os/signal" "github.com/google/ko/pkg/commands/options" "github.com/spf13/cobra" @@ -58,7 +61,8 @@ func addBuild(topLevel *cobra.Command) { 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() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() 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 e576972388..94ce7e4514 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -21,11 +21,9 @@ import ( "fmt" "log" "os" - "os/signal" "path/filepath" "strconv" "strings" - "syscall" "time" "github.com/google/go-containerregistry/pkg/authn" @@ -160,19 +158,6 @@ 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 -} - func createBuildConfigMap(workingDirectory string, configs []build.Config) (map[string]build.Config, error) { buildConfigsByImportPath := make(map[string]build.Config) for i, config := range configs { diff --git a/pkg/commands/create.go b/pkg/commands/create.go index d37f3e4f48..071073ab69 100644 --- a/pkg/commands/create.go +++ b/pkg/commands/create.go @@ -15,11 +15,13 @@ package commands import ( + "context" "errors" "fmt" "log" "os" "os/exec" + "os/signal" "strings" "github.com/google/ko/internal" @@ -72,7 +74,8 @@ func addCreate(topLevel *cobra.Command) { } // Cancel on signals. - ctx := createCancellableContext() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() bo.InsecureRegistry = po.InsecureRegistry builder, err := makeBuilder(ctx, bo) diff --git a/pkg/commands/delete.go b/pkg/commands/delete.go index 7f996e20e1..815961250a 100644 --- a/pkg/commands/delete.go +++ b/pkg/commands/delete.go @@ -15,9 +15,11 @@ package commands import ( + "context" "errors" "os" "os/exec" + "os/signal" "github.com/spf13/cobra" ) @@ -34,7 +36,8 @@ func passthru(command string) runCmd { } // Cancel on signals. - ctx := createCancellableContext() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() // Start building a command line invocation by passing // through our arguments to command's CLI. diff --git a/pkg/commands/deps.go b/pkg/commands/deps.go index 4926e89851..0ca32e3533 100644 --- a/pkg/commands/deps.go +++ b/pkg/commands/deps.go @@ -16,11 +16,13 @@ package commands import ( "archive/tar" + "context" "fmt" "io" "io/ioutil" "os" "os/exec" + "os/signal" "path/filepath" "github.com/google/go-containerregistry/pkg/authn" @@ -43,7 +45,8 @@ 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, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() ref, err := name.ParseReference(args[0]) if err != nil { diff --git a/pkg/commands/resolve.go b/pkg/commands/resolve.go index 59e183b158..a2e327991f 100644 --- a/pkg/commands/resolve.go +++ b/pkg/commands/resolve.go @@ -15,8 +15,10 @@ package commands import ( + "context" "fmt" "os" + "os/signal" "github.com/google/ko/pkg/commands/options" "github.com/spf13/cobra" @@ -55,7 +57,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, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() 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..26845ed41f 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -15,11 +15,13 @@ package commands import ( + "context" "errors" "fmt" "log" "os" "os/exec" + "os/signal" "path/filepath" "strings" @@ -49,7 +51,8 @@ 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, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() // Args after -- are for kubectl, so only consider importPaths before it. importPaths := args