Skip to content

Commit

Permalink
Use signal.NotifyContext
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Oct 19, 2021
1 parent 6447264 commit cbcb2c7
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 22 deletions.
5 changes: 4 additions & 1 deletion pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package commands

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"

"github.com/google/ko/internal"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
package commands

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/google/ko/pkg/commands/options"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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 {
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,11 +21,9 @@ import (
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package commands

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"

"github.com/google/ko/internal"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package commands

import (
"context"
"errors"
"os"
"os/exec"
"os/signal"

"github.com/spf13/cobra"
)
Expand All @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package commands

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/google/ko/pkg/commands/options"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package commands

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cbcb2c7

Please sign in to comment.