From 1a26ecb56317a49bb1e63bb5b22d213b7b3ac828 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Tue, 24 Mar 2020 11:46:01 +0300 Subject: [PATCH] Add `--retain-keys` command --- cmd/skywire-cli/commands/visor/gen-config.go | 27 ++++++++++++++++++++ pkg/app/appcommon/config.go | 1 + pkg/util/pathutil/configpath.go | 8 +++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cmd/skywire-cli/commands/visor/gen-config.go b/cmd/skywire-cli/commands/visor/gen-config.go index 76dd6f2ab..0f1090550 100644 --- a/cmd/skywire-cli/commands/visor/gen-config.go +++ b/cmd/skywire-cli/commands/visor/gen-config.go @@ -1,9 +1,12 @@ package visor import ( + "encoding/json" "errors" "fmt" + "io/ioutil" "net" + "path" "path/filepath" "time" @@ -26,6 +29,7 @@ func init() { var ( output string replace bool + retainKeys bool configLocType = pathutil.WorkingDirLoc testenv bool ) @@ -33,6 +37,7 @@ var ( func init() { genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.") genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.") + genConfigCmd.Flags().BoolVar(&retainKeys, "retain-keys", false, "retain current keys") genConfigCmd.Flags().VarP(&configLocType, "type", "m", fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes())) genConfigCmd.Flags().BoolVarP(&testenv, "testing-environment", "t", false, "whether to use production or test deployment service.") } @@ -62,10 +67,32 @@ var genConfigCmd = &cobra.Command{ default: logger.Fatalln("invalid config type:", configLocType) } + if replace && retainKeys && pathutil.Exists(output) { + if err := fillInOldKeys(output, conf); err != nil { + logger.WithError(err).Fatalln("Error retaining old keys") + } + } pathutil.WriteJSONConfig(conf, output, replace) }, } +func fillInOldKeys(confPath string, conf *visor.Config) error { + oldConfBytes, err := ioutil.ReadFile(path.Clean(confPath)) + if err != nil { + return fmt.Errorf("error reading old config file: %w", err) + } + + var oldConf visor.Config + if err := json.Unmarshal(oldConfBytes, &oldConf); err != nil { + return fmt.Errorf("invalid old configuration file: %w", err) + } + + conf.Visor.StaticPubKey = oldConf.Visor.StaticPubKey + conf.Visor.StaticSecKey = oldConf.Visor.StaticSecKey + + return nil +} + func homeConfig() *visor.Config { c := defaultConfig() c.AppsPath = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/apps") diff --git a/pkg/app/appcommon/config.go b/pkg/app/appcommon/config.go index c57f9fbfa..36ee807a9 100644 --- a/pkg/app/appcommon/config.go +++ b/pkg/app/appcommon/config.go @@ -1,5 +1,6 @@ package appcommon +// DefaultServerAddr is a default address to run the app server at. const DefaultServerAddr = "localhost:5505" // Config defines configuration parameters for `Proc`. diff --git a/pkg/util/pathutil/configpath.go b/pkg/util/pathutil/configpath.go index 3e8265073..b809be2fc 100644 --- a/pkg/util/pathutil/configpath.go +++ b/pkg/util/pathutil/configpath.go @@ -142,6 +142,12 @@ func FindConfigPath(args []string, argsIndex int, env string, defaults ConfigPat return "" } +// Exists checks if file or directory specified with `path` exists. +func Exists(path string) bool { + _, err := os.Stat(path) + return err == nil +} + // WriteJSONConfig is used by config file generators. // 'output' specifies the path to save generated config files. // 'replace' is true if replacing files is allowed. @@ -151,7 +157,7 @@ func WriteJSONConfig(conf interface{}, output string, replace bool) { log.WithError(err).Fatal("unexpected error, report to dev") } - if _, err := os.Stat(output); !replace && err == nil { + if !replace && Exists(output) { log.Fatalf("file %s already exists, stopping as 'replace,r' flag is not set", output) }