Skip to content

Commit

Permalink
kedge <subcommand> errors out without -f/--files
Browse files Browse the repository at this point in the history
Before this commit, running `kedge` subcommands without passing
any files would do nothing.

$ kedge deploy
$

$ kedge generate
$

$ kedge undeploy
$

This commit adds a check that input files are passed using -f or
--files flags, and if not, throws an error.

$ kedge generate
Error: Unable to validate input files: No files were passed. Please pass file(s) using '-f' or '--files'

The flags have also been marked as required, however, that takes
no effect, and there is an open issue on the spf13/cobra package
tracking the same - spf13/cobra#206

Fixes kedgeproject#96
  • Loading branch information
concaf committed Jul 12, 2017
1 parent b2c2433 commit 7a0b047
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package cmd

import (
"fmt"
"os"

pkgcmd "github.com/kedgeproject/kedge/pkg/cmd"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
)

// Variables
Expand All @@ -33,6 +35,10 @@ var createCmd = &cobra.Command{
Use: "create",
Short: "Create the resource on the Kubernetes cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := validateFiles(CreateFiles); err != nil {
fmt.Println(errors.Wrap(err, "Error: Unable to validate input files"))
os.Exit(-1)
}
if err := pkgcmd.Create(CreateFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
Expand All @@ -42,5 +48,6 @@ var createCmd = &cobra.Command{

func init() {
createCmd.Flags().StringArrayVarP(&CreateFiles, "files", "f", []string{}, "Specify files")
createCmd.MarkFlagRequired("files")
RootCmd.AddCommand(createCmd)
}
9 changes: 8 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package cmd

import (
"fmt"
"os"

pkgcmd "github.com/kedgeproject/kedge/pkg/cmd"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
)

// Variables
Expand All @@ -33,6 +35,10 @@ var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete the resource from the Kubernetes cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := validateFiles(DeleteFiles); err != nil {
fmt.Println(errors.Wrap(err, "Error: Unable to validate input files"))
os.Exit(-1)
}
if err := pkgcmd.Delete(DeleteFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
Expand All @@ -42,5 +48,6 @@ var deleteCmd = &cobra.Command{

func init() {
deleteCmd.Flags().StringArrayVarP(&DeleteFiles, "files", "f", []string{}, "Specify files")
deleteCmd.MarkFlagRequired("files")
RootCmd.AddCommand(deleteCmd)
}
6 changes: 6 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

pkgcmd "github.com/kedgeproject/kedge/pkg/cmd"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -34,6 +35,10 @@ var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate Kubernetes resources from App definition",
Run: func(cmd *cobra.Command, args []string) {
if err := validateFiles(AppFiles); err != nil {
fmt.Println(errors.Wrap(err, "Error: Unable to validate input files"))
os.Exit(-1)
}
if err := pkgcmd.Generate(AppFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
Expand All @@ -43,5 +48,6 @@ var generateCmd = &cobra.Command{

func init() {
generateCmd.Flags().StringArrayVarP(&AppFiles, "files", "f", []string{}, "input files")
generateCmd.MarkFlagRequired("files")
RootCmd.AddCommand(generateCmd)
}
10 changes: 10 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmd

import "github.com/pkg/errors"

func validateFiles(files []string) error {
if len(files) == 0 {
return errors.New("No files were passed. Please pass file(s) using '-f' or '--files'")
}
return nil
}

0 comments on commit 7a0b047

Please sign in to comment.