@@ -6,15 +6,21 @@ import (
66 "os"
77 "path"
88 "path/filepath"
9+ "sort"
910 "strings"
1011
1112 "github.com/kyma-project/cli.v3/internal/clierror"
1213 "github.com/kyma-project/cli.v3/internal/cmdcommon"
13- "github.com/kyma-project/cli.v3/internal/cmdcommon/flags "
14+ "github.com/pkg/errors "
1415 "github.com/spf13/cobra"
1516 "gopkg.in/yaml.v3"
1617)
1718
19+ type extensionConfig struct {
20+ DefaultRuntime string `yaml:"defaultRuntime"`
21+ Runtimes map [string ]runtimeConfig `yaml:"runtimes"`
22+ }
23+
1824type runtimeConfig struct {
1925 DepsFilename string `yaml:"depsFilename"`
2026 DepsData string `yaml:"depsData"`
@@ -25,77 +31,78 @@ type runtimeConfig struct {
2531type initConfig struct {
2632 * cmdcommon.KymaConfig
2733
28- runtimesConfig map [ string ] runtimeConfig
34+ extensionConfig * extensionConfig
2935
3036 runtime string
3137 dir string
3238}
3339
34- func NewInitCmd (kymaConfig * cmdcommon.KymaConfig , cmdConfig interface {}) * cobra.Command {
40+ func NewInitCmd (kymaConfig * cmdcommon.KymaConfig , cmdConfig interface {}) (* cobra.Command , error ) {
41+ extensionConfig , err := parseExtensionConfig (cmdConfig )
42+ if err != nil {
43+ return nil , err
44+ }
45+
3546 cfg := & initConfig {
36- KymaConfig : kymaConfig ,
47+ KymaConfig : kymaConfig ,
48+ extensionConfig : extensionConfig ,
3749 }
3850
3951 cmd := & cobra.Command {
4052 Use : "init" ,
4153 Short : "Init source and dependencies files locally" ,
4254 Long : "Use this command to initialize source and dependencies files for a Function." ,
4355 PreRun : func (cmd * cobra.Command , _ []string ) {
44- clierror .Check (flags .Validate (cmd .Flags (), flags .MarkRequired ("runtime" )))
45- clierror .Check (cfg .complete (cmdConfig ))
4656 clierror .Check (cfg .validate ())
4757 },
4858 Run : func (cmd * cobra.Command , _ []string ) {
4959 clierror .Check (runInit (cfg , cmd .OutOrStdout ()))
5060 },
5161 }
5262
53- cmd .Flags ().StringVar (& cfg .runtime , "runtime" , "" , "Runtime for which the files are generated" )
63+ cmd .Flags ().StringVar (& cfg .runtime , "runtime" , cfg . extensionConfig . DefaultRuntime , fmt . Sprintf ( "Runtime for which the files are generated [ %s ]" , sortedRuntimesString ( cfg . extensionConfig . Runtimes )) )
5464 cmd .Flags ().StringVar (& cfg .dir , "dir" , "." , "Path to the directory where files must be created" )
5565
56- return cmd
66+ return cmd , nil
5767}
5868
59- func ( c * initConfig ) complete ( cmdConfig interface {}) clierror. Error {
69+ func parseExtensionConfig ( cmdConfig interface {}) ( * extensionConfig , error ) {
6070 if cmdConfig == nil {
61- return clierror .New ("unexpected extension error, empty config" )
71+ return nil , errors .New ("unexpected extension error, empty config object " )
6272 }
6373
6474 configBytes , err := yaml .Marshal (cmdConfig )
6575 if err != nil {
66- return clierror .Wrap (err , clierror . New ( "failed to marshal config" ) )
76+ return nil , errors .Wrap (err , "failed to marshal config" )
6777 }
6878
69- err = yaml .Unmarshal (configBytes , & c .runtimesConfig )
79+ extCfg := extensionConfig {}
80+ err = yaml .Unmarshal (configBytes , & extCfg )
7081 if err != nil {
71- return clierror .Wrap (err , clierror . New ( "failed to unmarshal config" ) )
82+ return nil , errors .Wrap (err , "failed to unmarshal config" )
7283 }
7384
74- return nil
85+ if extCfg .DefaultRuntime == "" || len (extCfg .Runtimes ) == 0 {
86+ // simple validation
87+ return nil , errors .New ("unexpected extension error, empty config data" )
88+ }
89+
90+ return & extCfg , nil
7591}
7692
7793func (c * initConfig ) validate () clierror.Error {
78- if _ , ok := c .runtimesConfig [c .runtime ]; ! ok {
94+ if _ , ok := c .extensionConfig . Runtimes [c .runtime ]; ! ok {
7995 return clierror .New (
8096 fmt .Sprintf ("unsupported runtime %s" , c .runtime ),
81- fmt .Sprintf ("use on the allowed runtimes on this cluster [ %s ]" , strings . Join ( mapKeys ( c . runtimesConfig ), " / " )),
97+ fmt .Sprintf ("use on the allowed runtimes on this cluster [ %s ]" , sortedRuntimesString ( c . extensionConfig . Runtimes )),
8298 )
8399 }
84100
85101 return nil
86102}
87103
88- func mapKeys (m map [string ]runtimeConfig ) []string {
89- keys := []string {}
90- for key := range m {
91- keys = append (keys , key )
92- }
93-
94- return keys
95- }
96-
97104func runInit (cfg * initConfig , out io.Writer ) clierror.Error {
98- runtimeCfg := cfg .runtimesConfig [cfg .runtime ]
105+ runtimeCfg := cfg .extensionConfig . Runtimes [cfg .runtime ]
99106
100107 handlerPath := path .Join (cfg .dir , runtimeCfg .HandlerFilename )
101108 err := os .WriteFile (handlerPath , []byte (runtimeCfg .HandlerData ), os .ModePerm )
@@ -115,8 +122,20 @@ func runInit(cfg *initConfig, out io.Writer) clierror.Error {
115122 outDir = cfg .dir
116123 }
117124
118- fmt .Fprintf (out , "Functions files initialized to dir %s\n " , outDir )
119- fmt .Fprint (out , "\n Example usage:\n " )
120- fmt .Fprintf (out , "kyma alpha function create %s --runtime %s --source %s --dependencies %s\n " , cfg .runtime , cfg .runtime , handlerPath , depsPath )
125+ fmt .Fprintf (out , "Functions files of runtime %s initialized to dir %s\n " , cfg .runtime , outDir )
126+ fmt .Fprint (out , "\n Next steps:\n " )
127+ fmt .Fprint (out , "* update output files in your favorite IDE\n " )
128+ fmt .Fprintf (out , "* create Function, for example:\n " )
129+ fmt .Fprintf (out , " kyma alpha function create %s --runtime %s --source %s --dependencies %s\n " , cfg .runtime , cfg .runtime , handlerPath , depsPath )
121130 return nil
122131}
132+
133+ func sortedRuntimesString (m map [string ]runtimeConfig ) string {
134+ keys := []string {}
135+ for key := range m {
136+ keys = append (keys , key )
137+ }
138+
139+ sort .Strings (sort .StringSlice (keys ))
140+ return strings .Join (keys , ", " )
141+ }
0 commit comments