mirror of
https://github.com/ko-build/ko.git
synced 2025-07-12 23:50:31 +02:00
Use cobra's RunE wherever possible (#343)
This commit is contained in:
@ -15,8 +15,8 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
@ -62,10 +62,9 @@ func addApply(topLevel *cobra.Command) {
|
||||
# Apply from stdin:
|
||||
cat config.yaml | ko apply -f -`,
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !isKubectlAvailable() {
|
||||
log.Print("error: kubectl is not available. kubectl must be installed to use ko apply.")
|
||||
return
|
||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko apply")
|
||||
}
|
||||
|
||||
// Cancel on signals.
|
||||
@ -73,11 +72,11 @@ func addApply(topLevel *cobra.Command) {
|
||||
|
||||
builder, err := makeBuilder(ctx, bo)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating builder: %v", err)
|
||||
return fmt.Errorf("error creating builder: %v", err)
|
||||
}
|
||||
publisher, err := makePublisher(po)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating publisher: %v", err)
|
||||
return fmt.Errorf("error creating publisher: %v", err)
|
||||
}
|
||||
defer publisher.Close()
|
||||
// Create a set of ko-specific flags to ignore when passing through
|
||||
@ -110,7 +109,7 @@ func addApply(topLevel *cobra.Command) {
|
||||
// Wire up kubectl stdin to resolveFilesToWriter.
|
||||
stdin, err := kubectlCmd.StdinPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("error piping to 'kubectl apply': %v", err)
|
||||
return fmt.Errorf("error piping to 'kubectl apply': %v", err)
|
||||
}
|
||||
|
||||
// Make sure builds are cancelled if kubectl apply fails.
|
||||
@ -138,9 +137,7 @@ func addApply(topLevel *cobra.Command) {
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := g.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return g.Wait()
|
||||
},
|
||||
}
|
||||
options.AddPublishArg(apply, po)
|
||||
|
@ -15,8 +15,8 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
@ -62,10 +62,9 @@ func addCreate(topLevel *cobra.Command) {
|
||||
# Create from stdin:
|
||||
cat config.yaml | ko create -f -`,
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !isKubectlAvailable() {
|
||||
log.Print("error: kubectl is not available. kubectl must be installed to use ko create.")
|
||||
return
|
||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko create")
|
||||
}
|
||||
|
||||
// Cancel on signals.
|
||||
@ -73,11 +72,11 @@ func addCreate(topLevel *cobra.Command) {
|
||||
|
||||
builder, err := makeBuilder(ctx, bo)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating builder: %v", err)
|
||||
return fmt.Errorf("error creating builder: %v", err)
|
||||
}
|
||||
publisher, err := makePublisher(po)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating publisher: %v", err)
|
||||
return fmt.Errorf("error creating publisher: %v", err)
|
||||
}
|
||||
defer publisher.Close()
|
||||
// Create a set of ko-specific flags to ignore when passing through
|
||||
@ -110,7 +109,7 @@ func addCreate(topLevel *cobra.Command) {
|
||||
// Wire up kubectl stdin to resolveFilesToWriter.
|
||||
stdin, err := kubectlCmd.StdinPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("error piping to 'kubectl create': %v", err)
|
||||
return fmt.Errorf("error piping to 'kubectl create': %v", err)
|
||||
}
|
||||
|
||||
// Make sure builds are cancelled if kubectl create fails.
|
||||
@ -138,9 +137,7 @@ func addCreate(topLevel *cobra.Command) {
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := g.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return g.Wait()
|
||||
},
|
||||
}
|
||||
options.AddPublishArg(create, po)
|
||||
|
@ -15,7 +15,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"log"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
@ -23,15 +23,14 @@ import (
|
||||
)
|
||||
|
||||
// runCmd is suitable for use with cobra.Command's Run field.
|
||||
type runCmd func(*cobra.Command, []string)
|
||||
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) {
|
||||
return func(_ *cobra.Command, _ []string) error {
|
||||
if !isKubectlAvailable() {
|
||||
log.Print("error: kubectl is not available. kubectl must be installed to use ko delete.")
|
||||
return
|
||||
return errors.New("error: kubectl is not available. kubectl must be installed to use ko delete")
|
||||
}
|
||||
|
||||
// Cancel on signals.
|
||||
@ -49,9 +48,7 @@ func passthru(command string) runCmd {
|
||||
cmd.Stdin = os.Stdin
|
||||
|
||||
// Run it.
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Fatalf("error executing %q command with args: %v; %v", command, os.Args[1:], err)
|
||||
}
|
||||
return cmd.Run()
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +57,7 @@ func addDelete(topLevel *cobra.Command) {
|
||||
topLevel.AddCommand(&cobra.Command{
|
||||
Use: "delete",
|
||||
Short: `See "kubectl help delete" for detailed usage.`,
|
||||
Run: passthru("kubectl"),
|
||||
RunE: passthru("kubectl"),
|
||||
// We ignore unknown flags to avoid importing everything Go exposes
|
||||
// from our commands.
|
||||
FParseErrWhitelist: cobra.FParseErrWhitelist{
|
||||
|
@ -16,7 +16,6 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/google/ko/pkg/commands/options"
|
||||
"github.com/spf13/cobra"
|
||||
@ -57,24 +56,25 @@ func addPublish(topLevel *cobra.Command) {
|
||||
# This always preserves import paths.
|
||||
ko publish --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(_ *cobra.Command, args []string) {
|
||||
RunE: func(_ *cobra.Command, args []string) error {
|
||||
ctx := createCancellableContext()
|
||||
builder, err := makeBuilder(ctx, bo)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating builder: %v", err)
|
||||
return fmt.Errorf("error creating builder: %v", err)
|
||||
}
|
||||
publisher, err := makePublisher(po)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating publisher: %v", err)
|
||||
return fmt.Errorf("error creating publisher: %v", err)
|
||||
}
|
||||
defer publisher.Close()
|
||||
images, err := publishImages(ctx, args, publisher, builder)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to publish images: %v", err)
|
||||
return fmt.Errorf("failed to publish images: %v", err)
|
||||
}
|
||||
for _, img := range images {
|
||||
fmt.Println(img)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
options.AddPublishArg(publish, po)
|
||||
|
@ -15,7 +15,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"log"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/ko/pkg/commands/options"
|
||||
@ -54,20 +54,18 @@ func addResolve(topLevel *cobra.Command) {
|
||||
# This always preserves import paths.
|
||||
ko resolve --local -f config/`,
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := createCancellableContext()
|
||||
builder, err := makeBuilder(ctx, bo)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating builder: %v", err)
|
||||
return fmt.Errorf("error creating builder: %v", err)
|
||||
}
|
||||
publisher, err := makePublisher(po)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating publisher: %v", err)
|
||||
return fmt.Errorf("error creating publisher: %v", err)
|
||||
}
|
||||
defer publisher.Close()
|
||||
if err := resolveFilesToWriter(ctx, builder, publisher, fo, so, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return resolveFilesToWriter(ctx, builder, publisher, fo, so, os.Stdout)
|
||||
},
|
||||
}
|
||||
options.AddPublishArg(resolve, po)
|
||||
|
@ -15,6 +15,8 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -46,7 +48,7 @@ func addRun(topLevel *cobra.Command) {
|
||||
|
||||
# You can also supply args and flags to the command.
|
||||
ko run ./cmd/baz -- -v arg1 arg2 --yes`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := createCancellableContext()
|
||||
|
||||
// Args after -- are for kubectl, so only consider importPaths before it.
|
||||
@ -56,7 +58,7 @@ func addRun(topLevel *cobra.Command) {
|
||||
importPaths = args[:cmd.Flags().ArgsLenAtDash()]
|
||||
}
|
||||
if len(importPaths) == 0 {
|
||||
log.Fatalf("ko run: no importpaths listed")
|
||||
return errors.New("ko run: no importpaths listed")
|
||||
}
|
||||
|
||||
kubectlArgs := []string{}
|
||||
@ -67,24 +69,24 @@ func addRun(topLevel *cobra.Command) {
|
||||
|
||||
builder, err := makeBuilder(ctx, bo)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating builder: %v", err)
|
||||
return fmt.Errorf("error creating builder: %v", err)
|
||||
}
|
||||
publisher, err := makePublisher(po)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating publisher: %v", err)
|
||||
return fmt.Errorf("error creating publisher: %v", err)
|
||||
}
|
||||
defer publisher.Close()
|
||||
|
||||
if len(os.Args) < 3 {
|
||||
log.Fatalf("usage: %s run <package>", os.Args[0])
|
||||
return fmt.Errorf("usage: %s run <package>", os.Args[0])
|
||||
}
|
||||
ip := os.Args[2]
|
||||
if strings.HasPrefix(ip, "-") {
|
||||
log.Fatalf("expected first arg to be positional, got %q", ip)
|
||||
return fmt.Errorf("expected first arg to be positional, got %q", ip)
|
||||
}
|
||||
imgs, err := publishImages(ctx, importPaths, publisher, builder)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to publish images: %v", err)
|
||||
return fmt.Errorf("failed to publish images: %v", err)
|
||||
}
|
||||
|
||||
// Usually only one, but this is the simple way to access the
|
||||
@ -127,9 +129,10 @@ func addRun(topLevel *cobra.Command) {
|
||||
|
||||
// Run it.
|
||||
if err := kubectlCmd.Run(); err != nil {
|
||||
log.Fatalf("error executing \"kubectl run\": %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
// We ignore unknown flags to avoid importing everything Go exposes
|
||||
// from our commands.
|
||||
|
Reference in New Issue
Block a user