Skip to content

Commit

Permalink
Use signal.NotifyContext and cmd.Context (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Nov 2, 2021
1 parent b1c35d2 commit 1e46fde
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 37 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
}
4 changes: 1 addition & 3 deletions pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 0 additions & 15 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import (
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -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
}
4 changes: 1 addition & 3 deletions pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions pkg/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1e46fde

Please sign in to comment.