diff --git a/cmd/edenConfig.go b/cmd/edenConfig.go index 88014793b..2e9f9a35d 100644 --- a/cmd/edenConfig.go +++ b/cmd/edenConfig.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "github.com/lf-edge/eden/pkg/defaults" "github.com/lf-edge/eden/pkg/openevec" @@ -10,7 +11,12 @@ import ( ) func newConfigCmd(configName, verbosity *string) *cobra.Command { - cfg := &openevec.EdenSetupArgs{} + currentPath, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + cfg := openevec.GetDefaultConfig(currentPath) var configCmd = &cobra.Command{ Use: "config", Short: "work with config", @@ -71,15 +77,15 @@ func newConfigAddCmd(cfg *openevec.EdenSetupArgs) *cobra.Command { configAddCmd.Flags().StringVar(&contextFile, "file", "", "file with config to add") //not used in function configAddCmd.Flags().StringVarP(&cfg.Eve.QemuFileToSave, "qemu-config", "", defaults.DefaultQemuFileToSave, "file to save config") - configAddCmd.Flags().IntVarP(&cfg.Eve.QemuCpus, "cpus", "", defaults.DefaultCpus, "cpus") - configAddCmd.Flags().IntVarP(&cfg.Eve.QemuMemory, "memory", "", defaults.DefaultMemory, "memory (MB)") - configAddCmd.Flags().StringSliceVarP(&cfg.Eve.QemuFirmware, "eve-firmware", "", nil, "firmware path") - configAddCmd.Flags().StringVarP(&cfg.Eve.QemuConfigPath, "config-part", "", "", "path for config drive") - configAddCmd.Flags().StringVarP(&cfg.Eve.QemuDTBPath, "dtb-part", "", "", "path for device tree drive (for arm)") - configAddCmd.Flags().StringToStringVarP(&cfg.Eve.HostFwd, "eve-hostfwd", "", defaults.DefaultQemuHostFwd, "port forward map") - configAddCmd.Flags().StringVar(&cfg.Eve.Ssid, "ssid", "", "set ssid of wifi for rpi") - configAddCmd.Flags().StringVar(&cfg.Eve.Arch, "arch", "", "arch of EVE (amd64 or arm64)") - configAddCmd.Flags().StringVar(&cfg.Eve.ModelFile, "devmodel-file", "", "File to use for overwrite of model defaults") + configAddCmd.Flags().IntVarP(&cfg.Eve.QemuCpus, "cpus", "", cfg.Eve.QemuCpus, "cpus") + configAddCmd.Flags().IntVarP(&cfg.Eve.QemuMemory, "memory", "", cfg.Eve.QemuMemory, "memory (MB)") + configAddCmd.Flags().StringSliceVarP(&cfg.Eve.QemuFirmware, "eve-firmware", "", cfg.Eve.QemuFirmware, "firmware path") + configAddCmd.Flags().StringVarP(&cfg.Eve.QemuConfigPath, "config-part", "", cfg.Eve.QemuConfigPath, "path for config drive") + configAddCmd.Flags().StringVarP(&cfg.Eve.QemuDTBPath, "dtb-part", "", cfg.Eve.QemuDTBPath, "path for device tree drive (for arm)") + configAddCmd.Flags().StringToStringVarP(&cfg.Eve.HostFwd, "eve-hostfwd", "", cfg.Eve.HostFwd, "port forward map") + configAddCmd.Flags().StringVar(&cfg.Eve.Ssid, "ssid", cfg.Eve.Ssid, "set ssid of wifi for rpi") + configAddCmd.Flags().StringVar(&cfg.Eve.Arch, "arch", cfg.Eve.Arch, "arch of EVE (amd64 or arm64)") + configAddCmd.Flags().StringVar(&cfg.Eve.ModelFile, "devmodel-file", cfg.Eve.ModelFile, "File to use for overwrite of model defaults") configAddCmd.Flags().BoolVarP(&force, "force", "", false, "force overwrite config file") return configAddCmd diff --git a/pkg/openevec/edenConfig.go b/pkg/openevec/edenConfig.go index 053f303fe..233e3e780 100644 --- a/pkg/openevec/edenConfig.go +++ b/pkg/openevec/edenConfig.go @@ -5,9 +5,9 @@ import ( "fmt" "os" "path/filepath" + "reflect" "github.com/lf-edge/eden/pkg/defaults" - "github.com/lf-edge/eden/pkg/models" "github.com/lf-edge/eden/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/viper" @@ -62,10 +62,7 @@ func ConfigAdd(cfg *EdenSetupArgs, currentContext, contextFile string, force boo return fmt.Errorf("fail in DefaultConfigPath: %w", err) } } - model, err := models.GetDevModelByName(cfg.Eve.DevModel) - if err != nil { - return fmt.Errorf("GetDevModelByName: %w", err) - } + if _, err := os.Stat(cfg.ConfigFile); !os.IsNotExist(err) { if force { if err := os.Remove(cfg.ConfigFile); err != nil { @@ -75,24 +72,16 @@ func ConfigAdd(cfg *EdenSetupArgs, currentContext, contextFile string, force boo log.Debugf("current config already exists: %s", cfg.ConfigFile) } } - if _, err = os.Stat(cfg.ConfigFile); os.IsNotExist(err) { - if err = utils.GenerateConfigFile(cfg.ConfigFile); err != nil { - return fmt.Errorf("fail in generate yaml: %w", err) - } - log.Infof("Config file generated: %s", cfg.ConfigFile) - } - context, err := utils.ContextLoad() + context, err := utils.ContextInit() if err != nil { - return fmt.Errorf("load context error: %w", err) + return fmt.Errorf("Init context error: %w", err) } - currentContextName := context.Current if currentContext != "" { context.Current = currentContext - } else { - context.Current = "default" } cfg.ConfigFile = context.GetCurrentConfig() + if contextFile != "" { if err := utils.CopyFile(contextFile, cfg.ConfigFile); err != nil { return fmt.Errorf("cannot copy file: %w", err) @@ -100,45 +89,22 @@ func ConfigAdd(cfg *EdenSetupArgs, currentContext, contextFile string, force boo log.Infof("Context file generated: %s", contextFile) } else { if _, err := os.Stat(cfg.ConfigFile); os.IsNotExist(err) { - if err = utils.GenerateConfigFileDiff(cfg.ConfigFile, context); err != nil { - return fmt.Errorf("error generate config: %w", err) + if err := os.MkdirAll(filepath.Dir(cfg.ConfigFile), 0755); err != nil { + return fmt.Errorf("Error creating folders: %v", err) } - log.Infof("Context file generated: %s", cfg.ConfigFile) - } else { - log.Infof("Config file already exists %s", cfg.ConfigFile) - } - } - context.SetContext(context.Current) - if err := ReloadConfigDetails(cfg); err != nil { - return err - } + file, err := os.Create(cfg.ConfigFile) + if err != nil { + return fmt.Errorf("Error creating file %v", err) + } + defer file.Close() - // we prepare viper config here from EdenSetupArgs - // to feed into GenerateConfigFileFromViper + WriteConfig(reflect.ValueOf(*cfg), file, 0) - if cfg.Eve.Arch != "" { - viper.Set("eve.arch", cfg.Eve.Arch) - imageDist := fmt.Sprintf("%s-%s", context.Current, defaults.DefaultImageDist) - switch cfg.Eve.Arch { - case "amd64": - viper.Set("eve.firmware", []string{filepath.Join(imageDist, "eve", "OVMF_CODE.fd"), - filepath.Join(imageDist, "eve", "OVMF_VARS.fd")}) - case "arm64": - viper.Set("eve.firmware", []string{filepath.Join(imageDist, "eve", "OVMF.fd")}) + log.Infof("Config file generated: %s", cfg.ConfigFile) + } else { + log.Infof("Config file already exists %s", cfg.ConfigFile) } } - if cfg.Eve.Ssid != "" { - viper.Set("eve.ssid", cfg.Eve.Ssid) - } - - for k, v := range model.Config() { - viper.Set(k, v) - } - - if err = utils.GenerateConfigFileFromViper(); err != nil { - return fmt.Errorf("error writing config: %w", err) - } - context.SetContext(currentContextName) return nil } @@ -343,9 +309,6 @@ func ConfigDelete(target string, cfg *EdenSetupArgs) error { } context.Current = target configFile := context.GetCurrentConfig() - if err := ReloadConfigDetails(cfg); err != nil { - log.Infof("Failed to ReloadConfigDetails: %s", err.Error()) - } if _, err := os.Stat(cfg.Eve.QemuFileToSave); !os.IsNotExist(err) { if err := os.Remove(cfg.Eve.QemuFileToSave); err == nil { log.Infof("deleted qemu config %s", cfg.Eve.QemuFileToSave)