github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/create.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/spf13/pflag" 23 "github.com/projectriff/riff-cli/pkg/ioutils" 24 "os" 25 "github.com/projectriff/riff-cli/cmd/utils" 26 "github.com/projectriff/riff-cli/cmd/opts" 27 ) 28 29 var createChainCmd = utils.CommandChain(initCmd, buildCmd, applyCmd) 30 31 var createJavaChainCmd = utils.CommandChain(initJavaCmd, buildCmd, applyCmd) 32 33 var createNodeChainCmd = utils.CommandChain(initNodeCmd, buildCmd, applyCmd) 34 35 var createPythonChainCmd = utils.CommandChain(initPythonCmd, buildCmd, applyCmd) 36 37 var createShellChainCmd = utils.CommandChain(initShellCmd, buildCmd, applyCmd) 38 39 var createCmd = &cobra.Command{ 40 Use: "create [language]", 41 Short: "Create a function", 42 Long: utils.CreateCmdLong(), 43 44 RunE: createChainCmd.RunE, 45 PreRun: createChainCmd.PreRun, 46 PersistentPreRun: func(cmd *cobra.Command, args []string) { 47 if !opts.CreateOptions.Initialized { 48 opts.CreateOptions = options.CreateOptions{} 49 var flagset pflag.FlagSet 50 if cmd.Parent() == rootCmd { 51 flagset = *cmd.PersistentFlags() 52 } else { 53 flagset = *cmd.Parent().PersistentFlags() 54 } 55 56 utils.MergeInitOptions(flagset, &opts.CreateOptions.InitOptions) 57 utils.MergeBuildOptions(flagset, &opts.CreateOptions) 58 utils.MergeApplyOptions(flagset, &opts.CreateOptions) 59 60 if len(args) > 0 { 61 if len(args) == 1 && opts.CreateOptions.FilePath == "" { 62 opts.CreateOptions.FilePath = args[0] 63 } else { 64 ioutils.Errorf("Invalid argument(s) %v\n", args) 65 cmd.Usage() 66 os.Exit(1) 67 } 68 } 69 70 err := options.ValidateAndCleanInitOptions(&opts.CreateOptions.InitOptions) 71 if err != nil { 72 ioutils.Error(err) 73 os.Exit(1) 74 } 75 opts.CreateOptions.Initialized = true 76 } 77 createChainCmd.PersistentPreRun(cmd, args) 78 }, 79 } 80 81 var createJavaCmd = &cobra.Command{ 82 Use: "java", 83 Short: "Create a Java function", 84 Long: utils.CreateJavaCmdLong(), 85 86 RunE: createJavaChainCmd.RunE, 87 PreRun: func(cmd *cobra.Command, args []string) { 88 opts.Handler = utils.GetHandler(cmd) 89 createJavaChainCmd.PreRun(cmd, args) 90 }, 91 } 92 93 var createShellCmd = &cobra.Command{ 94 Use: "shell", 95 Short: "Create a shell script function", 96 Long: utils.CreateShellCmdLong(), 97 PreRun: createShellChainCmd.PreRun, 98 RunE: createShellChainCmd.RunE, 99 } 100 101 var createNodeCmd = &cobra.Command{ 102 Use: "node", 103 Short: "Create a node.js function", 104 Long: utils.InitNodeCmdLong(), 105 PreRun: createNodeChainCmd.PreRun, 106 RunE: createNodeChainCmd.RunE, 107 } 108 109 var createJsCmd = &cobra.Command{ 110 Use: "js", 111 Short: createNodeCmd.Short, 112 Long: createNodeCmd.Long, 113 RunE: createNodeCmd.RunE, 114 } 115 116 var createPythonCmd = &cobra.Command{ 117 Use: "python", 118 Short: "Create a Python function", 119 Long: utils.InitPythonCmdLong(), 120 121 PreRun: func(cmd *cobra.Command, args []string) { 122 opts.Handler = utils.GetHandler(cmd) 123 createPythonChainCmd.PreRun(cmd, args) 124 }, 125 RunE: createPythonChainCmd.RunE, 126 } 127 128 func init() { 129 rootCmd.AddCommand(createCmd) 130 utils.CreateInitFlags(createCmd.PersistentFlags()) 131 utils.CreateBuildFlags(createCmd.PersistentFlags()) 132 utils.CreateApplyFlags(createCmd.PersistentFlags()) 133 134 createCmd.AddCommand(createJavaCmd) 135 createCmd.AddCommand(createJsCmd) 136 createCmd.AddCommand(createNodeCmd) 137 createCmd.AddCommand(createPythonCmd) 138 createCmd.AddCommand(createShellCmd) 139 140 createJavaCmd.Flags().String("handler", "", "the fully qualified class name of the function handler") 141 createJavaCmd.MarkFlagRequired("handler") 142 143 createPythonCmd.Flags().String("handler", "", "the name of the function handler") 144 createPythonCmd.MarkFlagRequired("handler") 145 }