diff --git a/cmd/apply.go b/cmd/apply.go new file mode 100644 index 000000000..1f85783d5 --- /dev/null +++ b/cmd/apply.go @@ -0,0 +1,46 @@ +/* +Copyright 2017 The Kedge Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "fmt" + pkgcmd "github.com/kedgeproject/kedge/pkg/cmd" + "github.com/spf13/cobra" + "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 { + fmt.Println(err) + os.Exit(-1) + } + }, +} + +func init() { + applyCmd.Flags().StringArrayVarP(&ApplyFiles, "files", "f", []string{}, "Specify files") + RootCmd.AddCommand(applyCmd) +} diff --git a/cmd/create.go b/cmd/create.go index 8f68d920a..76d8aa6b9 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -33,7 +33,7 @@ var createCmd = &cobra.Command{ Use: "create", Short: "Create the resource on the Kubernetes cluster", Run: func(cmd *cobra.Command, args []string) { - if err := pkgcmd.Create(CreateFiles); err != nil { + if err := pkgcmd.ExecuteKubectl(CreateFiles, "create"); err != nil { fmt.Println(err) os.Exit(-1) } diff --git a/cmd/delete.go b/cmd/delete.go index e4390b654..2878d3121 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -33,7 +33,7 @@ var deleteCmd = &cobra.Command{ Use: "delete", Short: "Delete the resource from the Kubernetes cluster", Run: func(cmd *cobra.Command, args []string) { - if err := pkgcmd.Delete(DeleteFiles); err != nil { + if err := pkgcmd.ExecuteKubectl(DeleteFiles, "delete"); err != nil { fmt.Println(err) os.Exit(-1) } diff --git a/pkg/cmd/kubernetes.go b/pkg/cmd/kubernetes.go index 655dedc50..c33f4d180 100644 --- a/pkg/cmd/kubernetes.go +++ b/pkg/cmd/kubernetes.go @@ -28,19 +28,7 @@ import ( "github.com/pkg/errors" ) -func Delete(files []string) error { - return executeKubectl(files, true) -} - -func Create(files []string) error { - return executeKubectl(files, false) -} - -func executeKubectl(files []string, delete bool) error { - command := "create" - if delete { - command = "delete" - } +func ExecuteKubectl(files []string, command string) error { appData, err := getApplicationsFromFiles(files) if err != nil {