Skip to content

Commit

Permalink
Implemented ability to read configuration from STDIN
Browse files Browse the repository at this point in the history
Demo

skywire-node:

```sh
cat skywire-config.json | go run ./cmd/skywire-node --stdin
```

```sh
./skywire-cli node gen-config -o /dev/null -r  |grep -v INFO | go run ./cmd/skywire-node -i
```

setup-node:

```sh
sw-env -l | jq '.skywire_services.setup_node.config' | go run ./cmd/setup-node -i
```
  • Loading branch information
ayuryshev committed Apr 25, 2019
1 parent 26f04ac commit 556f0d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
52 changes: 30 additions & 22 deletions cmd/setup-node/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package commands

import (
"bufio"
"context"
"encoding/json"
"io"
"log"
"log/syslog"
"net/http"
Expand All @@ -18,20 +20,16 @@ import (
)

var (
metricsAddr string
syslogAddr string
tag string
metricsAddr string
syslogAddr string
tag string
cfgFromStdin bool
)

var rootCmd = &cobra.Command{
Use: "setup-node [config.json]",
Short: "Route Setup Node for skywire",
Run: func(_ *cobra.Command, args []string) {
configFile := "config.json"
if len(args) > 0 {
configFile = args[0]
}
conf := parseConfig(configFile)

logger := logging.MustGetLogger(tag)
if syslogAddr != "" {
Expand All @@ -42,6 +40,29 @@ var rootCmd = &cobra.Command{
logging.AddHook(hook)
}

var rdr io.Reader
var err error

if !cfgFromStdin {
configFile := "config.json"

if len(args) > 0 {
configFile = args[0]
}
rdr, err = os.Open(configFile)
if err != nil {
log.Fatalf("Failed to open config: %s", err)
}
} else {
logger.Info("Reading config from STDIN")
rdr = bufio.NewReader(os.Stdin)
}

conf := &setup.Config{}
if err := json.NewDecoder(rdr).Decode(&conf); err != nil {
log.Fatalf("Failed to decode %s: %s", rdr, err)
}

sn, err := setup.NewNode(conf, metrics.NewPrometheus("setupnode"))
if err != nil {
logger.Fatal("Failed to setup Node: ", err)
Expand All @@ -62,20 +83,7 @@ func init() {
rootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", ":2121", "address to bind metrics API to")
rootCmd.Flags().StringVar(&syslogAddr, "syslog", "", "syslog server address. E.g. localhost:514")
rootCmd.Flags().StringVar(&tag, "tag", "setup-node", "logging tag")
}

func parseConfig(path string) *setup.Config {
file, err := os.Open(path)
if err != nil {
log.Fatalf("Failed to open config: %s", err)
}

conf := &setup.Config{}
if err := json.NewDecoder(file).Decode(&conf); err != nil {
log.Fatalf("Failed to decode %s: %s", path, err)
}

return conf
rootCmd.Flags().BoolVarP(&cfgFromStdin, "stdin", "i", false, "read config from STDIN")
}

// Execute executes root CLI command.
Expand Down
27 changes: 19 additions & 8 deletions cmd/skywire-node/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package commands

import (
"bufio"
"encoding/json"
"io"
"log"
"log/syslog"
"os"
Expand All @@ -21,15 +23,15 @@ import (
const configEnv = "SW_CONFIG"

var (
syslogAddr string
tag string
syslogAddr string
tag string
cfgFromStdin bool
)

var rootCmd = &cobra.Command{
Use: "skywire-node [config-path]",
Short: "App Node for skywire",
Run: func(_ *cobra.Command, args []string) {
configPath := pathutil.FindConfigPath(args, 0, configEnv, pathutil.NodeDefaults())

logger := logging.MustGetLogger(tag)

Expand All @@ -42,14 +44,22 @@ var rootCmd = &cobra.Command{
}
}

file, err := os.Open(configPath)
if err != nil {
logger.Fatalf("Failed to open config: %s", err)
var rdr io.Reader
var err error
if !cfgFromStdin {
configPath := pathutil.FindConfigPath(args, 0, configEnv, pathutil.NodeDefaults())
rdr, err = os.Open(configPath)
if err != nil {
logger.Fatalf("Failed to open config: %s", err)
}
} else {
logger.Info("Reading config from STDIN")
rdr = bufio.NewReader(os.Stdin)
}

conf := &node.Config{}
if err := json.NewDecoder(file).Decode(&conf); err != nil {
logger.Fatalf("Failed to decode %s: %s", configPath, err)
if err := json.NewDecoder(rdr).Decode(&conf); err != nil {
logger.Fatalf("Failed to decode %s: %s", rdr, err)
}

node, err := node.NewNode(conf)
Expand Down Expand Up @@ -87,6 +97,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.Flags().StringVarP(&syslogAddr, "syslog", "", "none", "syslog server address. E.g. localhost:514")
rootCmd.Flags().StringVarP(&tag, "tag", "", "skywire", "logging tag")
rootCmd.Flags().BoolVarP(&cfgFromStdin, "stdin", "i", false, "read config from STDIN")
}

// Execute executes root CLI command.
Expand Down

0 comments on commit 556f0d5

Please sign in to comment.