github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/utils/composite_flags.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 utils 18 19 import ( 20 "github.com/projectriff/riff-cli/pkg/osutils" 21 "github.com/spf13/pflag" 22 "github.com/projectriff/riff-cli/pkg/options" 23 "github.com/projectriff/riff-cli/global" 24 "github.com/spf13/cobra" 25 "github.com/projectriff/riff-cli/cmd/opts" 26 "github.com/spf13/viper" 27 ) 28 29 type Defaults struct { 30 riffVersion string 31 userAccount string 32 namespace string 33 force bool 34 dryRun bool 35 push bool 36 version string 37 } 38 39 var defaults = Defaults{ 40 riffVersion: global.RIFF_VERSION, 41 userAccount: "current OS user", 42 namespace: "default", 43 force: false, 44 dryRun: false, 45 push: false, 46 version: "0.0.1", 47 } 48 49 func CreateInitFlags(flagset *pflag.FlagSet) { 50 setVersionFlag(flagset) 51 setNameFlag(flagset) 52 setFilePathFlag(flagset) 53 setProtocolFlag(flagset) 54 setInputFlag(flagset) 55 setOutputFlag(flagset) 56 setArtifactFlag(flagset) 57 setRiffVersionFlag(flagset) 58 setUserAccountFlag(flagset) 59 setForceFlag(flagset) 60 setDryRunFlag(flagset) 61 } 62 63 func CreateBuildFlags(flagset *pflag.FlagSet) { 64 setNameFlag(flagset) 65 setFilePathFlag(flagset) 66 setVersionFlag(flagset) 67 setRiffVersionFlag(flagset) 68 setDryRunFlag(flagset) 69 setPushFlag(flagset) 70 setUserAccountFlag(flagset) 71 } 72 73 func CreateApplyFlags(flagset *pflag.FlagSet) { 74 setFilePathFlag(flagset) 75 setNamespaceFlag(flagset) 76 setDryRunFlag(flagset) 77 } 78 79 func CreateDeleteFlags(flagset *pflag.FlagSet) { 80 setFilePathFlag(flagset) 81 setNameFlag(flagset) 82 setNamespaceFlag(flagset) 83 setDryRunFlag(flagset) 84 flagset.Bool("all", false, "delete all resources including topics, not just the function resource") 85 } 86 87 func MergeInitOptions(flagset pflag.FlagSet, opts *options.InitOptions) { 88 if opts.FunctionName == "" { 89 opts.FunctionName, _ = flagset.GetString("name") 90 } 91 if opts.Version == "" { 92 opts.Version, _ = flagset.GetString("version") 93 } 94 if opts.FilePath == "" { 95 opts.FilePath, _ = flagset.GetString("filepath") 96 } 97 if opts.Protocol == "" { 98 opts.Protocol, _ = flagset.GetString("protocol") 99 } 100 if opts.Input == "" { 101 opts.Input, _ = flagset.GetString("input") 102 } 103 if opts.Output == "" { 104 opts.Output, _ = flagset.GetString("output") 105 } 106 if opts.Artifact == "" { 107 opts.Artifact, _ = flagset.GetString("artifact") 108 } 109 if opts.RiffVersion == "" { 110 opts.RiffVersion = getRiffVersionWithGlobalOverride(flagset) 111 } 112 if opts.UserAccount == "" { 113 opts.UserAccount = getUseraccountWithOverride("useraccount", flagset) 114 } 115 if opts.DryRun == false { 116 opts.DryRun, _ = flagset.GetBool("dry-run") 117 } 118 if opts.Force == false { 119 opts.Force, _ = flagset.GetBool("force") 120 } 121 } 122 123 func MergeBuildOptions(flagset pflag.FlagSet, opts *options.CreateOptions) { 124 if opts.FunctionName == "" { 125 opts.FunctionName, _ = flagset.GetString("name") 126 } 127 if opts.Version == "" { 128 opts.Version, _ = flagset.GetString("version") 129 } 130 if opts.FilePath == "" { 131 opts.FilePath, _ = flagset.GetString("filepath") 132 } 133 if opts.RiffVersion == "" { 134 opts.RiffVersion = getRiffVersionWithGlobalOverride(flagset) 135 } 136 if opts.UserAccount == "" { 137 opts.UserAccount = getUseraccountWithOverride("useraccount", flagset) 138 } 139 if opts.DryRun == false { 140 opts.DryRun, _ = flagset.GetBool("dry-run") 141 } 142 if opts.Push == false { 143 opts.Push, _ = flagset.GetBool("push") 144 } 145 } 146 147 func MergeApplyOptions(flagset pflag.FlagSet, opts *options.CreateOptions) { 148 if opts.FilePath == "" { 149 opts.FilePath, _ = flagset.GetString("filepath") 150 } 151 if opts.Namespace == "" { 152 opts.Namespace = GetStringValueWithOverride("namespace", flagset) 153 } 154 if opts.DryRun == false { 155 opts.DryRun, _ = flagset.GetBool("dry-run") 156 } 157 } 158 159 func MergeDeleteOptions(flagset pflag.FlagSet, opts *options.DeleteAllOptions) { 160 if opts.FunctionName == "" { 161 opts.FunctionName, _ = flagset.GetString("name") 162 } 163 if opts.FilePath == "" { 164 opts.FilePath, _ = flagset.GetString("filepath") 165 } 166 if opts.Namespace == "" { 167 opts.Namespace = GetStringValueWithOverride("namespace", flagset) 168 } 169 if opts.DryRun == false { 170 opts.DryRun, _ = flagset.GetBool("dry-run") 171 } 172 if opts.All == false { 173 opts.All, _ = flagset.GetBool("all") 174 } 175 } 176 177 func GetHandler(cmd *cobra.Command) string { 178 if opts.Handler == "" { 179 opts.Handler, _ = cmd.Flags().GetString("handler") 180 } 181 return opts.Handler 182 } 183 184 185 func setNameFlag(flagset *pflag.FlagSet) { 186 if !flagDefined(flagset, "name") { 187 flagset.StringP("name", "n", "", "the name of the function (defaults to the name of the current directory)") 188 } 189 } 190 191 func setVersionFlag(flagset *pflag.FlagSet) { 192 if !flagDefined(flagset, "version") { 193 flagset.StringP("version", "v", defaults.version, "the version of the function image") 194 } 195 } 196 197 func setFilePathFlag(flagset *pflag.FlagSet) { 198 if !flagDefined(flagset, "filepath") { 199 flagset.StringP("filepath", "f", "", "path or directory used for the function resources (defaults to the current directory)") 200 } 201 } 202 203 func setNamespaceFlag(flagset *pflag.FlagSet) { 204 if !flagDefined(flagset, "namespace") { 205 flagset.String("namespace", defaults.namespace, "the namespace used for the deployed resources") 206 } 207 } 208 209 func setDryRunFlag(flagset *pflag.FlagSet) { 210 if !flagDefined(flagset, "dry-run") { 211 flagset.Bool("dry-run", defaults.dryRun, "print generated function artifacts content to stdout only") 212 } 213 } 214 215 func setRiffVersionFlag(flagset *pflag.FlagSet) { 216 if !flagDefined(flagset, "riff-version") { 217 flagset.StringP("riff-version", "", defaults.riffVersion, "the version of riff to use when building containers") 218 } 219 } 220 221 func setUserAccountFlag(flagset *pflag.FlagSet) { 222 if !flagDefined(flagset, "useraccount") { 223 flagset.StringP("useraccount", "u", defaults.userAccount, "the Docker user account to be used for the image repository") 224 } 225 } 226 227 func setProtocolFlag(flagset *pflag.FlagSet) { 228 if !flagDefined(flagset, "protocol") { 229 flagset.StringP("protocol", "p", "", "the protocol to use for function invocations") 230 } 231 } 232 233 func setInputFlag(flagset *pflag.FlagSet) { 234 if !flagDefined(flagset, "input") { 235 flagset.StringP("input", "i", "", "the name of the input topic (defaults to function name)") 236 } 237 } 238 func setOutputFlag(flagset *pflag.FlagSet) { 239 if !flagDefined(flagset, "output") { 240 flagset.StringP("output", "o", "", "the name of the output topic (optional)") 241 } 242 } 243 244 func setArtifactFlag(flagset *pflag.FlagSet) { 245 if !flagDefined(flagset, "artifact") { 246 flagset.StringP("artifact", "a", "", "path to the function artifact, source code or jar file") 247 } 248 } 249 250 func setForceFlag(flagset *pflag.FlagSet) { 251 if !flagDefined(flagset, "force") { 252 flagset.Bool("force", defaults.force, "overwrite existing functions artifacts") 253 } 254 } 255 256 func setPushFlag(flagset *pflag.FlagSet) { 257 if !flagDefined(flagset, "push") { 258 flagset.BoolP("push", "", defaults.push, "push the image to Docker registry") 259 } 260 } 261 262 func flagDefined(flagset *pflag.FlagSet, name string) bool { 263 return flagset.Lookup(name) != nil 264 } 265 266 func getRiffVersionWithGlobalOverride(flagset pflag.FlagSet) string { 267 val, _ := flagset.GetString("riff-version") 268 if flagset.Changed("riff-version") { 269 return val 270 } 271 return global.RIFF_VERSION 272 } 273 274 func getUseraccountWithOverride(name string, flagset pflag.FlagSet) string { 275 userAcct := GetStringValueWithOverride("useraccount", flagset) 276 if userAcct == defaults.userAccount { 277 userAcct = osutils.GetCurrentUsername() 278 } 279 return userAcct 280 } 281 282 func GetStringValueWithOverride(name string, flagset pflag.FlagSet) string { 283 viperVal := viper.GetString(name) 284 val, _ := flagset.GetString(name) 285 if flagset.Changed(name) || viperVal == "" { 286 return val 287 } 288 return viperVal 289 }