From 5555d8ab59a3acf2aa94c9a03b2908af0cd282b6 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Wed, 6 Mar 2019 21:54:06 +0100 Subject: [PATCH 1/5] added multiple paths for config --- cmd/skywire-node/commands/root.go | 12 +++++++++ internal/pathutil/data.go | 42 +++++++++++++++++++++++++++++++ internal/pathutil/homedir.go | 19 ++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 internal/pathutil/data.go create mode 100644 internal/pathutil/homedir.go diff --git a/cmd/skywire-node/commands/root.go b/cmd/skywire-node/commands/root.go index aac57eb323..2dd6ba0015 100644 --- a/cmd/skywire-node/commands/root.go +++ b/cmd/skywire-node/commands/root.go @@ -9,6 +9,8 @@ import ( "syscall" "time" + "github.com/skycoin/skywire/internal/pathutil" + "github.com/spf13/cobra" "github.com/skycoin/skywire/pkg/node" @@ -21,8 +23,18 @@ var rootCmd = &cobra.Command{ configFile := "skywire.json" if len(args) > 0 { configFile = args[0] + } else if conf, ok := os.LookupEnv("SKYWIRE_CONFIG"); ok { + configFile = conf + } else { + conf, err := pathutil.Find("skywire.json") + if err != nil { + log.Fatalln(err) + } + configFile = conf } + log.Println("using conf file at: ", configFile) + file, err := os.Open(configFile) if err != nil { log.Fatalf("Failed to open config: %s", err) diff --git a/internal/pathutil/data.go b/internal/pathutil/data.go new file mode 100644 index 0000000000..0f340f3505 --- /dev/null +++ b/internal/pathutil/data.go @@ -0,0 +1,42 @@ +package pathutil + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +var ( + ErrDirectoryNotFound = errors.New(fmt.Sprintf( + "directory not found in any of the following paths: %s", strings.Join(paths(), ", "))) +) + +// paths return the paths in which skywire looks for data directories +func paths() []string { + // if we are unable to find the local directory try next option + localDir, _ := os.Getwd() // nolint: errcheck + + return []string{ + localDir, + filepath.Join(HomeDir(), ".skycoin/skywire"), + "/usr/local/skycoin/skywire", + } +} + +// Find tries to find given directory or file in: +// 1) local app directory +// 2) ${HOME}/.skycoin/skywire/{directory} +// 3) /usr/local/skycoin/skywire/{directory} +// It will return the first path, including given directory if found, by preference. +func Find(name string) (string, error) { + for _, path := range paths() { + _, err := os.Stat(filepath.Join(path, name)) + if err == nil { + return filepath.Join(path, name), nil + } + } + + return "", ErrDirectoryNotFound +} diff --git a/internal/pathutil/homedir.go b/internal/pathutil/homedir.go new file mode 100644 index 0000000000..442a989938 --- /dev/null +++ b/internal/pathutil/homedir.go @@ -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") +} From 3fb44b9337020ee34c5fd724ddf1029dd1618918 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Thu, 7 Mar 2019 15:53:50 +0100 Subject: [PATCH 2/5] linted --- cmd/skywire-node/commands/root.go | 2 +- internal/pathutil/data.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/skywire-node/commands/root.go b/cmd/skywire-node/commands/root.go index 2dd6ba0015..7f97e78b07 100644 --- a/cmd/skywire-node/commands/root.go +++ b/cmd/skywire-node/commands/root.go @@ -20,7 +20,7 @@ var rootCmd = &cobra.Command{ Use: "skywire-node [skywire.json]", Short: "App Node for skywire", Run: func(_ *cobra.Command, args []string) { - configFile := "skywire.json" + var configFile string if len(args) > 0 { configFile = args[0] } else if conf, ok := os.LookupEnv("SKYWIRE_CONFIG"); ok { diff --git a/internal/pathutil/data.go b/internal/pathutil/data.go index 0f340f3505..94ecf10bfc 100644 --- a/internal/pathutil/data.go +++ b/internal/pathutil/data.go @@ -1,16 +1,16 @@ package pathutil import ( - "errors" "fmt" "os" "path/filepath" "strings" ) +// pathutil errors var ( - ErrDirectoryNotFound = errors.New(fmt.Sprintf( - "directory not found in any of the following paths: %s", strings.Join(paths(), ", "))) + ErrDirectoryNotFound = fmt.Errorf( + "directory not found in any of the following paths: %s", strings.Join(paths(), ", ")) ) // paths return the paths in which skywire looks for data directories From bd51899a34d3efdf89487e2b2a2f320f00ef324e Mon Sep 17 00:00:00 2001 From: ivcosla Date: Mon, 11 Mar 2019 17:51:15 +0100 Subject: [PATCH 3/5] added config modes --- cmd/skywire-cli/commands/config.go | 41 +++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/cmd/skywire-cli/commands/config.go b/cmd/skywire-cli/commands/config.go index 547c3a148f..4b32da96de 100644 --- a/cmd/skywire-cli/commands/config.go +++ b/cmd/skywire-cli/commands/config.go @@ -5,6 +5,9 @@ import ( "encoding/json" "log" "os" + "path/filepath" + + "github.com/skycoin/skywire/internal/pathutil" "github.com/spf13/cobra" @@ -16,6 +19,10 @@ func init() { rootCmd.AddCommand(configCmd) } +var ( + configMode string +) + var configCmd = &cobra.Command{ Use: "config [skywire.json]", Short: "Generate default config file", @@ -25,7 +32,15 @@ var configCmd = &cobra.Command{ configFile = args[0] } - conf := defaultConfig() + var conf *node.Config + switch configMode { + case "local": + conf = defaultConfig() + case "home": + conf = homeConfig() + case "system": + conf = systemConfig() + } confFile, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0600) if err != nil { log.Fatal("Failed to create config files: ", err) @@ -41,6 +56,26 @@ var configCmd = &cobra.Command{ }, } +func homeConfig() *node.Config { + c := defaultConfig() + + c.AppsPath = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/apps") + c.Transport.LogStore.Location = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/transport_logs") + c.Routing.Table.Location = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/routing.db") + + return c +} + +func systemConfig() *node.Config { + c := defaultConfig() + + c.AppsPath = "/usr/local/skycoin/skywire/apps" + c.Transport.LogStore.Location = "/usr/local/skycoin/skywire/transport_logs" + c.Routing.Table.Location = "/usr/local/skycoin/skywire/routing.db" + + return c +} + func defaultConfig() *node.Config { conf := &node.Config{} conf.Version = "1.0" @@ -82,3 +117,7 @@ func defaultConfig() *node.Config { return conf } + +func init() { + configCmd.Flags().StringVar(&configMode, "mode", "home", "either home or local") +} From 12ab1648513e040e625ed221277505ee3a463623 Mon Sep 17 00:00:00 2001 From: Ivan Costa Date: Mon, 18 Mar 2019 10:42:37 +0100 Subject: [PATCH 4/5] generating local config in docker node --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 348ca9d87a..7b993c8207 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OPTS?=GO111MODULE=on +OPTS?=GO111MODULE=on DOCKER_IMAGE?=skywire-runner # docker image to use for running skywire-node.`golang`, `buildpack-deps:stretch-scm` is OK too DOCKER_NETWORK?=SKYNET DOCKER_NODE?=SKY01 @@ -80,7 +80,7 @@ docker-bin: docker-volume: docker-apps docker-bin bin - ./skywire-cli config ./node/skywire.json + ./skywire-cli config --mode local ./node/skywire.json cat ./node/skywire.json|grep static_public_key |cut -d ':' -f2 |tr -d '"'','' ' > ./node/PK cat ./node/PK From 3237345a8b96c97aaea84de932cb564794a2bcd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BF=97=E5=AE=87?= Date: Fri, 29 Mar 2019 01:15:47 +0800 Subject: [PATCH 5/5] changed env --- cmd/skywire-node/commands/root.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/skywire-node/commands/root.go b/cmd/skywire-node/commands/root.go index 7f97e78b07..1d2a6f8d9c 100644 --- a/cmd/skywire-node/commands/root.go +++ b/cmd/skywire-node/commands/root.go @@ -9,13 +9,14 @@ import ( "syscall" "time" - "github.com/skycoin/skywire/internal/pathutil" - "github.com/spf13/cobra" + "github.com/skycoin/skywire/internal/pathutil" "github.com/skycoin/skywire/pkg/node" ) +const configEnv = "SW_CONFIG" + var rootCmd = &cobra.Command{ Use: "skywire-node [skywire.json]", Short: "App Node for skywire", @@ -23,7 +24,7 @@ var rootCmd = &cobra.Command{ var configFile string if len(args) > 0 { configFile = args[0] - } else if conf, ok := os.LookupEnv("SKYWIRE_CONFIG"); ok { + } else if conf, ok := os.LookupEnv(configEnv); ok { configFile = conf } else { conf, err := pathutil.Find("skywire.json") @@ -33,7 +34,7 @@ var rootCmd = &cobra.Command{ configFile = conf } - log.Println("using conf file at: ", configFile) + log.Println("using conf file at:", configFile) file, err := os.Open(configFile) if err != nil {