Skip to content

Commit

Permalink
Use cobra's RunE wherever possible (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh authored Apr 22, 2021
1 parent 75ab991 commit 29cd8e0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 49 deletions.
17 changes: 7 additions & 10 deletions pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package commands

import (
"errors"
"fmt"
"log"
"os"
"os/exec"

Expand Down Expand Up @@ -62,22 +62,21 @@ 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.
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()
// Create a set of ko-specific flags to ignore when passing through
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package commands

import (
"errors"
"fmt"
"log"
"os"
"os/exec"

Expand Down Expand Up @@ -62,22 +62,21 @@ 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.
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()
// Create a set of ko-specific flags to ignore when passing through
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions pkg/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
package commands

import (
"log"
"errors"
"os"
"os/exec"

"github.com/spf13/cobra"
)

// 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.
Expand All @@ -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()
}
}

Expand All @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package commands

import (
"fmt"
"log"

"github.com/google/ko/pkg/commands/options"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions pkg/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package commands

import (
"log"
"fmt"
"os"

"github.com/google/ko/pkg/commands/options"
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commands

import (
"errors"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -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.
Expand All @@ -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{}
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 29cd8e0

Please sign in to comment.