github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/apply.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 "os" 22 "strings" 23 24 "github.com/projectriff/riff-cli/cmd/opts" 25 "github.com/projectriff/riff-cli/cmd/utils" 26 "github.com/projectriff/riff-cli/pkg/functions" 27 "github.com/projectriff/riff-cli/pkg/ioutils" 28 "github.com/projectriff/riff-cli/pkg/kubectl" 29 "github.com/projectriff/riff-cli/pkg/options" 30 "github.com/projectriff/riff-cli/pkg/osutils" 31 "github.com/spf13/cobra" 32 ) 33 34 // applyCmd represents the apply command 35 var applyCmd = &cobra.Command{ 36 Use: "apply", 37 Short: "Apply function resource definitions", 38 Long: `Apply the resource definition[s] included in the path. A resource will be created if it doesn't exist yet.`, 39 Example: ` riff apply -f some/function/path 40 riff apply -f some/function/path/some.yaml`, 41 42 RunE: func(cmd *cobra.Command, args []string) error { 43 return apply(cmd, options.GetApplyOptions(opts.CreateOptions)) 44 }, 45 PreRun: func(cmd *cobra.Command, args []string) { 46 47 if !opts.CreateOptions.Initialized { 48 utils.MergeApplyOptions(*cmd.Flags(), &opts.CreateOptions) 49 if len(args) > 0 { 50 if len(args) == 1 && opts.CreateOptions.FilePath == "" { 51 opts.CreateOptions.FilePath = args[0] 52 } else { 53 ioutils.Errorf("Invalid argument(s) %v\n", args) 54 cmd.Usage() 55 os.Exit(1) 56 } 57 } 58 59 err := options.ValidateAndCleanInitOptions(&opts.CreateOptions.InitOptions) 60 if err != nil { 61 ioutils.Error(err) 62 os.Exit(1) 63 } 64 } 65 opts.CreateOptions.Initialized = true 66 }, 67 } 68 69 func apply(cmd *cobra.Command, opts options.ApplyOptions) error { 70 abs, err := functions.AbsPath(opts.FilePath) 71 if err != nil { 72 cmd.SilenceUsage = true 73 return err 74 } 75 76 cmdArgs := []string{"apply", "--namespace", opts.Namespace} 77 var message string 78 79 if osutils.IsDirectory(abs) { 80 message = fmt.Sprintf("Applying resources in %v\n\n", opts.FilePath) 81 resourceDefinitionPaths, err := osutils.FindRiffResourceDefinitionPaths(abs) 82 if err != nil { 83 return err 84 } 85 for _, resourceDefinitionPath := range resourceDefinitionPaths { 86 cmdArgs = append(cmdArgs, "-f", resourceDefinitionPath) 87 } 88 } else { 89 message = fmt.Sprintf("Applying resource %v\n\n", opts.FilePath) 90 cmdArgs = append(cmdArgs, "-f", abs) 91 } 92 93 if opts.DryRun { 94 fmt.Printf("\nApply Command: kubectl %s\n\n", strings.Trim(fmt.Sprint(cmdArgs), "[]")) 95 } else { 96 fmt.Print(message) 97 output, err := kubectl.ExecForString(cmdArgs) 98 if err != nil { 99 cmd.SilenceUsage = true 100 return err 101 } 102 fmt.Printf("%v\n", output) 103 } 104 return nil 105 } 106 107 func init() { 108 rootCmd.AddCommand(applyCmd) 109 utils.CreateApplyFlags(applyCmd.Flags()) 110 }