Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --retain-keys flag #242

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmd/skywire-cli/commands/visor/gen-config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package visor

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"path"
"path/filepath"
"time"

Expand All @@ -26,13 +29,15 @@ func init() {
var (
output string
replace bool
retainKeys bool
configLocType = pathutil.WorkingDirLoc
testenv bool
)

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.")
}
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/app/appcommon/config.go
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
8 changes: 7 additions & 1 deletion pkg/util/pathutil/configpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

Expand Down