Skip to content

Commit

Permalink
Merge pull request #208 from skycoin/feature/config-directory
Browse files Browse the repository at this point in the history
Added multiple paths for config.
  • Loading branch information
志宇 authored Mar 29, 2019
2 parents d09c7b4 + 4643ce3 commit b071075
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
41 changes: 40 additions & 1 deletion cmd/skywire-cli/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/json"
"log"
"os"
"path/filepath"

"github.com/skycoin/skywire/internal/pathutil"

"github.com/spf13/cobra"

Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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"
Expand Down Expand Up @@ -82,3 +117,7 @@ func defaultConfig() *node.Config {

return conf
}

func init() {
configCmd.Flags().StringVar(&configMode, "mode", "home", "either home or local")
}
15 changes: 14 additions & 1 deletion cmd/skywire-node/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,31 @@ import (

"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",
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(configEnv); 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)
Expand Down
42 changes: 42 additions & 0 deletions internal/pathutil/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pathutil

import (
"fmt"
"os"
"path/filepath"
"strings"
)

// pathutil errors
var (
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
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
}

0 comments on commit b071075

Please sign in to comment.