11package function
22
33import (
4+ "bufio"
45 "fmt"
56 "io"
67 "os"
@@ -56,7 +57,7 @@ func NewInitCmd(kymaConfig *cmdcommon.KymaConfig, cmdConfig interface{}) (*cobra
5657 clierror .Check (cfg .validate ())
5758 },
5859 Run : func (cmd * cobra.Command , _ []string ) {
59- clierror .Check (runInit (cfg , cmd .OutOrStdout ()))
60+ clierror .Check (runInit (cfg , cmd .InOrStdin (), cmd . OutOrStdout ()))
6061 },
6162 }
6263
@@ -87,6 +88,16 @@ func parseExtensionConfig(cmdConfig interface{}) (*extensionConfig, error) {
8788 return nil , errors .New ("unexpected extension error, empty config data" )
8889 }
8990
91+ for runtimeName , runtimeCfg := range extCfg .Runtimes {
92+ if ! filepath .IsLocal (runtimeCfg .DepsFilename ) {
93+ return nil , errors .New (fmt .Sprintf ("deps filename %s for runtime %s is not a single file name" , runtimeCfg .DepsFilename , runtimeName ))
94+ }
95+
96+ if ! filepath .IsLocal (runtimeCfg .HandlerFilename ) {
97+ return nil , errors .New (fmt .Sprintf ("handler filename %s for runtime %s is not a single file name" , runtimeCfg .HandlerFilename , runtimeName ))
98+ }
99+ }
100+
90101 return & extCfg , nil
91102}
92103
@@ -101,9 +112,17 @@ func (c *initConfig) validate() clierror.Error {
101112 return nil
102113}
103114
104- func runInit (cfg * initConfig , out io.Writer ) clierror.Error {
115+ func runInit (cfg * initConfig , in io. Reader , out io.Writer ) clierror.Error {
105116 runtimeCfg := cfg .extensionConfig .Runtimes [cfg .runtime ]
106117
118+ if ! filepath .IsLocal (cfg .dir ) {
119+ // output dir is not a local path, ask user for confirmation
120+ clierr := getUserAcceptance (in , out , cfg .dir )
121+ if clierr != nil {
122+ return clierr
123+ }
124+ }
125+
107126 handlerPath := path .Join (cfg .dir , runtimeCfg .HandlerFilename )
108127 err := os .WriteFile (handlerPath , []byte (runtimeCfg .HandlerData ), os .ModePerm )
109128 if err != nil {
@@ -139,3 +158,26 @@ func sortedRuntimesString(m map[string]runtimeConfig) string {
139158 sort .Strings (sort .StringSlice (keys ))
140159 return strings .Join (keys , ", " )
141160}
161+
162+ func getUserAcceptance (in io.Reader , out io.Writer , path string ) clierror.Error {
163+ fmt .Fprintf (out , "The output path ( %s ) seems to be outside of the current working directory.\n " , path )
164+ fmt .Fprint (out , "Do you want to proceed? (y/n): " )
165+
166+ input , err := bufio .NewReader (in ).ReadString ('\n' ) // wait for user to press enter
167+ fmt .Fprintln (out )
168+
169+ if err != nil {
170+ return clierror .Wrap (err , clierror .New ("failed to read user input" ))
171+ }
172+
173+ lowerInput := strings .ToLower (input )
174+ if lowerInput == "y\n " || lowerInput == "yes\n " {
175+ // user accepted, continue
176+ return nil
177+ }
178+
179+ return clierror .New (
180+ "function init aborted" ,
181+ "you must provide a local path for the output directory or accept the default one by typing 'y' and pressing enter" ,
182+ )
183+ }
0 commit comments