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