diff --git a/cmd/setup-node/commands/root.go b/cmd/setup-node/commands/root.go index b3bb9eb79..a34f24118 100644 --- a/cmd/setup-node/commands/root.go +++ b/cmd/setup-node/commands/root.go @@ -1,8 +1,10 @@ package commands import ( + "bufio" "context" "encoding/json" + "io" "log" "log/syslog" "net/http" @@ -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 != "" { @@ -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) @@ -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. diff --git a/cmd/skywire-node/commands/root.go b/cmd/skywire-node/commands/root.go index 5aa7a0db9..ec52912ff 100644 --- a/cmd/skywire-node/commands/root.go +++ b/cmd/skywire-node/commands/root.go @@ -1,7 +1,9 @@ package commands import ( + "bufio" "encoding/json" + "io" "log" "log/syslog" "os" @@ -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) @@ -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) @@ -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.