mirror of
https://github.com/ko-build/ko.git
synced 2025-03-20 20:55:00 +02:00
Use signal.NotifyContext and cmd.Context (#482)
This commit is contained in:
parent
b1c35d29f5
commit
1e46fdebd5
6
main.go
6
main.go
@ -17,8 +17,10 @@ limitations under the License.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
"github.com/google/go-containerregistry/pkg/logs"
|
"github.com/google/go-containerregistry/pkg/logs"
|
||||||
"github.com/google/ko/pkg/commands"
|
"github.com/google/ko/pkg/commands"
|
||||||
@ -28,7 +30,9 @@ func main() {
|
|||||||
logs.Warn.SetOutput(os.Stderr)
|
logs.Warn.SetOutput(os.Stderr)
|
||||||
logs.Progress.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)
|
log.Fatal("error during command execution:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,9 +85,7 @@ func addApply(topLevel *cobra.Command) {
|
|||||||
if !isKubectlAvailable() {
|
if !isKubectlAvailable() {
|
||||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko apply")
|
return errors.New("error: kubectl is not available. kubectl must be installed to use ko apply")
|
||||||
}
|
}
|
||||||
|
ctx := cmd.Context()
|
||||||
// Cancel on signals.
|
|
||||||
ctx := createCancellableContext()
|
|
||||||
|
|
||||||
bo.InsecureRegistry = po.InsecureRegistry
|
bo.InsecureRegistry = po.InsecureRegistry
|
||||||
builder, err := makeBuilder(ctx, bo)
|
builder, err := makeBuilder(ctx, bo)
|
||||||
|
@ -57,8 +57,9 @@ func addBuild(topLevel *cobra.Command) {
|
|||||||
# This always preserves import paths.
|
# This always preserves import paths.
|
||||||
ko build --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`,
|
ko build --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := createCancellableContext()
|
ctx := cmd.Context()
|
||||||
|
|
||||||
bo.InsecureRegistry = po.InsecureRegistry
|
bo.InsecureRegistry = po.InsecureRegistry
|
||||||
builder, err := makeBuilder(ctx, bo)
|
builder, err := makeBuilder(ctx, bo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -21,10 +21,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-containerregistry/pkg/authn"
|
"github.com/google/go-containerregistry/pkg/authn"
|
||||||
@ -142,16 +140,3 @@ func getCreationTime() (*v1.Time, error) {
|
|||||||
func getKoDataCreationTime() (*v1.Time, error) {
|
func getKoDataCreationTime() (*v1.Time, error) {
|
||||||
return getTimeFromEnv("KO_DATA_DATE_EPOCH")
|
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
|
|
||||||
}
|
|
||||||
|
@ -70,9 +70,7 @@ func addCreate(topLevel *cobra.Command) {
|
|||||||
if !isKubectlAvailable() {
|
if !isKubectlAvailable() {
|
||||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko create")
|
return errors.New("error: kubectl is not available. kubectl must be installed to use ko create")
|
||||||
}
|
}
|
||||||
|
ctx := cmd.Context()
|
||||||
// Cancel on signals.
|
|
||||||
ctx := createCancellableContext()
|
|
||||||
|
|
||||||
bo.InsecureRegistry = po.InsecureRegistry
|
bo.InsecureRegistry = po.InsecureRegistry
|
||||||
builder, err := makeBuilder(ctx, bo)
|
builder, err := makeBuilder(ctx, bo)
|
||||||
|
@ -28,27 +28,25 @@ type runCmd func(*cobra.Command, []string) error
|
|||||||
// passthru returns a runCmd that simply passes our CLI arguments
|
// passthru returns a runCmd that simply passes our CLI arguments
|
||||||
// through to a binary named command.
|
// through to a binary named command.
|
||||||
func passthru(command string) runCmd {
|
func passthru(command string) runCmd {
|
||||||
return func(_ *cobra.Command, _ []string) error {
|
return func(cmd *cobra.Command, _ []string) error {
|
||||||
if !isKubectlAvailable() {
|
if !isKubectlAvailable() {
|
||||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko delete")
|
return errors.New("error: kubectl is not available. kubectl must be installed to use ko delete")
|
||||||
}
|
}
|
||||||
|
ctx := cmd.Context()
|
||||||
// Cancel on signals.
|
|
||||||
ctx := createCancellableContext()
|
|
||||||
|
|
||||||
// Start building a command line invocation by passing
|
// Start building a command line invocation by passing
|
||||||
// through our arguments to command's CLI.
|
// 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
|
// Pass through our environment
|
||||||
cmd.Env = os.Environ()
|
ecmd.Env = os.Environ()
|
||||||
// Pass through our stdfoo
|
// Pass through our stdfoo
|
||||||
cmd.Stderr = os.Stderr
|
ecmd.Stderr = os.Stderr
|
||||||
cmd.Stdout = os.Stdout
|
ecmd.Stdout = os.Stdout
|
||||||
cmd.Stdin = os.Stdin
|
ecmd.Stdin = os.Stdin
|
||||||
|
|
||||||
// Run it.
|
// Run it.
|
||||||
return cmd.Run()
|
return ecmd.Run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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`,
|
ko deps docker.io/my-user/my-image:v3`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := createCancellableContext()
|
ctx := cmd.Context()
|
||||||
|
|
||||||
ref, err := name.ParseReference(args[0])
|
ref, err := name.ParseReference(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,7 +55,8 @@ func addResolve(topLevel *cobra.Command) {
|
|||||||
ko resolve --local -f config/`,
|
ko resolve --local -f config/`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := createCancellableContext()
|
ctx := cmd.Context()
|
||||||
|
|
||||||
bo.InsecureRegistry = po.InsecureRegistry
|
bo.InsecureRegistry = po.InsecureRegistry
|
||||||
builder, err := makeBuilder(ctx, bo)
|
builder, err := makeBuilder(ctx, bo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -49,7 +49,7 @@ func addRun(topLevel *cobra.Command) {
|
|||||||
# You can also supply args and flags to the command.
|
# You can also supply args and flags to the command.
|
||||||
ko run ./cmd/baz -- -v arg1 arg2 --yes`,
|
ko run ./cmd/baz -- -v arg1 arg2 --yes`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := createCancellableContext()
|
ctx := cmd.Context()
|
||||||
|
|
||||||
// Args after -- are for kubectl, so only consider importPaths before it.
|
// Args after -- are for kubectl, so only consider importPaths before it.
|
||||||
importPaths := args
|
importPaths := args
|
||||||
|
Loading…
x
Reference in New Issue
Block a user