Skip to content

Commit

Permalink
add kubectl check for ko delete, apply, and create (ko-build#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and jonjohnsonjr committed Jan 16, 2020
1 parent b7e1a7f commit 2e28671
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func addApply(topLevel *cobra.Command) {
cat config.yaml | ko apply -f -`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if !isKubectlAvailable() {
log.Print("error: kubectl is not available. kubectl must be installed to use ko apply.")
return
}

builder, err := makeBuilder(bo)
if err != nil {
log.Fatalf("error creating builder: %v", err)
Expand Down
10 changes: 10 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package commands

import (
"os/exec"

"github.com/spf13/cobra"
)

Expand All @@ -31,3 +33,11 @@ func AddKubeCommands(topLevel *cobra.Command) {
addRun(topLevel)
addCompletion(topLevel)
}

// check if kubectl is installed
func isKubectlAvailable() bool {
if _, err := exec.LookPath("kubectl"); err != nil {
return false
}
return true
}
5 changes: 5 additions & 0 deletions pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func addCreate(topLevel *cobra.Command) {
cat config.yaml | ko create -f -`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if !isKubectlAvailable() {
log.Print("error: kubectl is not available. kubectl must be installed to use ko create.")
return
}

builder, err := makeBuilder(bo)
if err != nil {
log.Fatalf("error creating builder: %v", err)
Expand Down
8 changes: 7 additions & 1 deletion pkg/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package commands

import (
"github.com/spf13/cobra"
"log"
"os"
"os/exec"

"github.com/spf13/cobra"
)

// runCmd is suitable for use with cobra.Command's Run field.
Expand All @@ -28,6 +29,11 @@ type runCmd func(*cobra.Command, []string)
// through to a binary named command.
func passthru(command string) runCmd {
return func(_ *cobra.Command, _ []string) {
if !isKubectlAvailable() {
log.Print("error: kubectl is not available. kubectl must be installed to use ko delete.")
return
}

// Start building a command line invocation by passing
// through our arguments to command's CLI.
cmd := exec.Command(command, os.Args[1:]...)
Expand Down

0 comments on commit 2e28671

Please sign in to comment.