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