1
0
mirror of https://github.com/ko-build/ko.git synced 2025-03-17 20:47:51 +02:00

Use signal.NotifyContext and cmd.Context (#482)

This commit is contained in:
Jason Hall 2021-11-02 17:15:25 -04:00 committed by GitHub
parent b1c35d29f5
commit 1e46fdebd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 22 additions and 37 deletions

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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 {

View File

@ -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
}

View File

@ -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)

View File

@ -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()
}
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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