github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/delete.go (about) 1 /* 2 * Copyright 2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cmd 18 19 import ( 20 "fmt" 21 22 "os" 23 "path/filepath" 24 "strings" 25 26 "github.com/projectriff/riff-cli/cmd/utils" 27 "github.com/projectriff/riff-cli/pkg/functions" 28 "github.com/projectriff/riff-cli/pkg/ioutils" 29 "github.com/projectriff/riff-cli/pkg/kubectl" 30 "github.com/projectriff/riff-cli/pkg/options" 31 "github.com/projectriff/riff-cli/pkg/osutils" 32 "github.com/spf13/cobra" 33 ) 34 35 var DeleteAllOptions options.DeleteAllOptions 36 37 // deleteCmd represents the delete command 38 var deleteCmd = &cobra.Command{ 39 Use: "delete", 40 Short: "Delete function resources", 41 Long: `Delete the resource[s] for the function or path specified.`, 42 Example: ` riff delete -n square 43 or 44 riff delete -f function/square`, 45 46 RunE: func(cmd *cobra.Command, args []string) error { 47 48 return delete(cmd, options.GetDeleteOptions(DeleteAllOptions)) 49 50 }, 51 PreRun: func(cmd *cobra.Command, args []string) { 52 53 if !DeleteAllOptions.Initialized { 54 utils.MergeDeleteOptions(*cmd.Flags(), &DeleteAllOptions) 55 if len(args) > 0 { 56 if len(args) == 1 && DeleteAllOptions.FilePath == "" { 57 DeleteAllOptions.FilePath = args[0] 58 } else { 59 ioutils.Errorf("Invalid argument(s) %v\n", args) 60 cmd.Usage() 61 os.Exit(1) 62 } 63 } 64 65 err := options.ValidateNamePathOptions(&DeleteAllOptions.FunctionName, &DeleteAllOptions.FilePath) 66 if err != nil { 67 ioutils.Error(err) 68 os.Exit(1) 69 } 70 } 71 DeleteAllOptions.Initialized = true 72 }, 73 } 74 75 func delete(cmd *cobra.Command, opts options.DeleteOptions) error { 76 77 if opts.FunctionName == "" { 78 var err error 79 opts.FunctionName, err = functions.FunctionNameFromPath(opts.FilePath) 80 if err != nil { 81 cmd.SilenceUsage = true 82 return err 83 } 84 } 85 86 abs, err := functions.AbsPath(opts.FilePath) 87 if err != nil { 88 cmd.SilenceUsage = true 89 return err 90 } 91 92 var cmdArgs []string 93 var message string 94 95 if opts.All { 96 optionPath := opts.FilePath 97 if !osutils.IsDirectory(abs) { 98 abs = filepath.Dir(abs) 99 optionPath = filepath.Dir(optionPath) 100 } 101 message = fmt.Sprintf("Deleting %v resources\n\n", optionPath) 102 resourceDefinitionPaths, err := osutils.FindRiffResourceDefinitionPaths(abs) 103 if err != nil { 104 return err 105 } 106 cmdArgs = []string{"delete", "--namespace", opts.Namespace} 107 for _, resourceDefinitionPath := range resourceDefinitionPaths { 108 cmdArgs = append(cmdArgs, "-f", resourceDefinitionPath) 109 } 110 } else { 111 if osutils.IsDirectory(abs) { 112 message = fmt.Sprintf("Deleting %v function\n\n", opts.FunctionName) 113 cmdArgs = []string{"delete", "--namespace", opts.Namespace, "function", opts.FunctionName} 114 } else { 115 message = fmt.Sprintf("Deleting %v resource\n\n", opts.FilePath) 116 cmdArgs = []string{"delete", "--namespace", opts.Namespace, "-f", opts.FilePath} 117 } 118 } 119 120 if opts.DryRun { 121 fmt.Printf("\nDelete Command: kubectl %s\n\n", strings.Trim(fmt.Sprint(cmdArgs), "[]")) 122 } else { 123 fmt.Print(message) 124 output, err := kubectl.ExecForString(cmdArgs) 125 if err != nil { 126 cmd.SilenceUsage = true 127 return err 128 } 129 fmt.Printf("%v\n", output) 130 } 131 132 return nil 133 } 134 135 func init() { 136 rootCmd.AddCommand(deleteCmd) 137 utils.CreateDeleteFlags(deleteCmd.Flags()) 138 }