-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added manager node config and associated commands
- Loading branch information
林志宇
committed
Mar 27, 2019
1 parent
466dbac
commit 0cbba05
Showing
8 changed files
with
232 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/skycoin/skywire/pkg/manager" | ||
) | ||
|
||
const ( | ||
homeMode = "HOME" | ||
localMode = "LOCAL" | ||
) | ||
|
||
var initConfigModes = []string{homeMode, localMode} | ||
|
||
var ( | ||
output string | ||
replace bool | ||
mode string | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(initConfigCmd) | ||
|
||
initConfigCmd.Flags().StringVarP(&output, "output", "o", defaultConfigPaths[0], "path of output config file.") | ||
initConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.") | ||
initConfigCmd.Flags().StringVarP(&mode, "mode", "m", homeMode, fmt.Sprintf("config generation mode. Valid values: %v", initConfigModes)) | ||
} | ||
|
||
var initConfigCmd = &cobra.Command{ | ||
Use: "init-config", | ||
Short: "generates a configuration file", | ||
Run: func(_ *cobra.Command, _ []string) { | ||
output, err := filepath.Abs(output) | ||
if err != nil { | ||
log.WithError(err).Fatalln("invalid output provided") | ||
} | ||
var conf manager.Config | ||
switch mode { | ||
case homeMode: | ||
conf = manager.GenerateHomeConfig() | ||
case localMode: | ||
conf = manager.GenerateLocalConfig() | ||
default: | ||
log.Fatalln("invalid mode:", mode) | ||
} | ||
raw, err := json.MarshalIndent(conf, "", " ") | ||
if err != nil { | ||
log.WithError(err).Fatal("unexpected error, report to dev") | ||
} | ||
if _, err := os.Stat(output); !replace && err == nil { | ||
log.Fatalf("file %s already exists, stopping as 'replace,r' flag is not set", output) | ||
} | ||
if err := os.MkdirAll(filepath.Dir(output), 0750); err != nil { | ||
log.WithError(err).Fatalln("failed to create output directory") | ||
} | ||
if err := ioutil.WriteFile(output, raw, 0744); err != nil { | ||
log.WithError(err).Fatalln("failed to write file") | ||
} | ||
log.Infof("Wrote %d bytes to %s\n%s", len(raw), output, string(raw)) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pathutil | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
) | ||
|
||
// HomeDir obtains the path to the user's home directory via ENVs. | ||
// SRC: https://github.com/spf13/viper/blob/80ab6657f9ec7e5761f6603320d3d58dfe6970f6/util.go#L144-L153 | ||
func HomeDir() string { | ||
if runtime.GOOS == "windows" { | ||
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") | ||
if home == "" { | ||
home = os.Getenv("USERPROFILE") | ||
} | ||
return home | ||
} | ||
return os.Getenv("HOME") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.