Skip to content

Commit

Permalink
Merge pull request kedgeproject#178 from containscafeine/refactor_fil…
Browse files Browse the repository at this point in the history
…es_variable

refactor to reuse same InputFiles variable
  • Loading branch information
surajssd authored Jul 25, 2017
2 parents 83f12ea + ab41b40 commit db1b615
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
9 changes: 2 additions & 7 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,19 @@ import (
"os"
)

// Variables
var (
ApplyFiles []string
)

// Represents the "apply" command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply a configuration to a resource on the Kubernetes cluster. This resource will be created if it doesn't exist yet.",
Run: func(cmd *cobra.Command, args []string) {
if err := pkgcmd.ExecuteKubectl(ApplyFiles, "apply"); err != nil {
if err := pkgcmd.ExecuteKubectl(InputFiles, "apply"); err != nil {
fmt.Println(err)
os.Exit(-1)
}
},
}

func init() {
applyCmd.Flags().StringArrayVarP(&ApplyFiles, "files", "f", []string{}, "Specify files")
applyCmd.Flags().StringArrayVarP(&InputFiles, "files", "f", []string{}, "Specify files")
RootCmd.AddCommand(applyCmd)
}
11 changes: 3 additions & 8 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@ import (
"github.com/spf13/cobra"
)

// Variables
var (
CreateFiles []string
)

// Represents the "create" command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create the resource on the Kubernetes cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := ifFilesPassed(CreateFiles); err != nil {
if err := ifFilesPassed(InputFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
}
if err := pkgcmd.ExecuteKubectl(CreateFiles, "create"); err != nil {
if err := pkgcmd.ExecuteKubectl(InputFiles, "create"); err != nil {
fmt.Println(err)
os.Exit(-1)
}
},
}

func init() {
createCmd.Flags().StringArrayVarP(&CreateFiles, "files", "f", []string{}, "Specify files")
createCmd.Flags().StringArrayVarP(&InputFiles, "files", "f", []string{}, "Specify files")
createCmd.MarkFlagRequired("files")
RootCmd.AddCommand(createCmd)
}
11 changes: 3 additions & 8 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@ import (
"github.com/spf13/cobra"
)

// Variables
var (
DeleteFiles []string
)

// Represents the "delete" command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete the resource from the Kubernetes cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := ifFilesPassed(DeleteFiles); err != nil {
if err := ifFilesPassed(InputFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
}
if err := pkgcmd.ExecuteKubectl(DeleteFiles, "delete"); err != nil {
if err := pkgcmd.ExecuteKubectl(InputFiles, "delete"); err != nil {
fmt.Println(err)
os.Exit(-1)
}
},
}

func init() {
deleteCmd.Flags().StringArrayVarP(&DeleteFiles, "files", "f", []string{}, "Specify files")
deleteCmd.Flags().StringArrayVarP(&InputFiles, "files", "f", []string{}, "Specify files")
deleteCmd.MarkFlagRequired("files")
RootCmd.AddCommand(deleteCmd)
}
11 changes: 3 additions & 8 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@ import (
"github.com/spf13/cobra"
)

// Variables
var (
AppFiles []string
)

// Represents the "generate" command
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate Kubernetes resources from App definition",
Run: func(cmd *cobra.Command, args []string) {
if err := ifFilesPassed(AppFiles); err != nil {
if err := ifFilesPassed(InputFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
}
if err := pkgcmd.Generate(AppFiles); err != nil {
if err := pkgcmd.Generate(InputFiles); err != nil {
fmt.Println(err)
os.Exit(-1)
}
},
}

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

import "github.com/pkg/errors"

var (
InputFiles []string
)

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

0 comments on commit db1b615

Please sign in to comment.