github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/update.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 "github.com/spf13/cobra" 21 "github.com/projectriff/riff-cli/pkg/options" 22 "github.com/projectriff/riff-cli/pkg/ioutils" 23 "github.com/projectriff/riff-cli/cmd/utils" 24 "github.com/projectriff/riff-cli/cmd/opts" 25 "github.com/spf13/pflag" 26 "os" 27 ) 28 29 var updateChainCmd = utils.CommandChain(buildCmd, applyCmd) 30 31 // updateCmd represents the update command 32 var updateCmd = &cobra.Command{ 33 Use: "update", 34 Short: "Update a function", 35 Long: `Build the function based on the code available in the path directory, using the name and version specified 36 for the image that is built. Then Apply the resource definition[s] included in the path.`, 37 Example: ` riff update -n <name> -v <version> -f <path> [--push]`, 38 39 RunE: updateChainCmd.RunE, 40 PreRun: updateChainCmd.PreRun, 41 PersistentPreRun: func(cmd *cobra.Command, args []string) { 42 if !opts.CreateOptions.Initialized { 43 opts.CreateOptions = options.CreateOptions{} 44 var flagset pflag.FlagSet 45 if cmd.Parent() == rootCmd { 46 flagset = *cmd.PersistentFlags() 47 } else { 48 flagset = *cmd.Parent().PersistentFlags() 49 } 50 51 utils.MergeInitOptions(flagset, &opts.CreateOptions.InitOptions) 52 utils.MergeBuildOptions(flagset, &opts.CreateOptions) 53 utils.MergeApplyOptions(flagset, &opts.CreateOptions) 54 55 if len(args) > 0 { 56 if len(args) == 1 && opts.CreateOptions.FilePath == "" { 57 opts.CreateOptions.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.ValidateAndCleanInitOptions(&opts.CreateOptions.InitOptions) 66 if err != nil { 67 ioutils.Error(err) 68 os.Exit(1) 69 } 70 opts.CreateOptions.Initialized = true 71 } 72 updateChainCmd.PersistentPreRun(cmd, args) 73 }, 74 } 75 76 77 func init() { 78 rootCmd.AddCommand(updateCmd) 79 utils.CreateBuildFlags(updateCmd.PersistentFlags()) 80 utils.CreateApplyFlags(updateCmd.PersistentFlags()) 81 }