summaryrefslogtreecommitdiff
path: root/internal/cli/root.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2022-03-06 12:35:14 +0100
committerGitHub <[email protected]>2022-03-06 12:35:14 +0100
commit1c5a6a590832bbefb2cab99a81f413a5a0e756de (patch)
treecc6595214e4ff39600099fb8c6f39e7a3dd560e9 /internal/cli/root.go
parent587702be62b65c151d27dc79e62fcbbd11290e6f (diff)
parent70e4f020afd0352dc52114651f0f65c1965ed399 (diff)
[#1036]: chore(cli): remove config plugin from the `root.go`
Diffstat (limited to 'internal/cli/root.go')
-rw-r--r--internal/cli/root.go69
1 files changed, 38 insertions, 31 deletions
diff --git a/internal/cli/root.go b/internal/cli/root.go
index 723e8a01..0f35d416 100644
--- a/internal/cli/root.go
+++ b/internal/cli/root.go
@@ -6,6 +6,7 @@ import (
"path/filepath"
"runtime"
+ "github.com/roadrunner-server/errors"
"github.com/roadrunner-server/roadrunner/v2/internal/cli/reset"
"github.com/roadrunner-server/roadrunner/v2/internal/cli/serve"
"github.com/roadrunner-server/roadrunner/v2/internal/cli/workers"
@@ -13,24 +14,25 @@ import (
"github.com/roadrunner-server/roadrunner/v2/internal/meta"
"github.com/joho/godotenv"
- "github.com/roadrunner-server/config/v2"
"github.com/spf13/cobra"
)
// NewCommand creates root command.
func NewCommand(cmdName string) *cobra.Command { //nolint:funlen
- const envDotenv = "DOTENV_PATH" // env var name: path to the .env file
+ const (
+ envDotenv string = "DOTENV_PATH" // env var name: path to the .env file
+ )
var ( // flag values
- cfgFile string // path to the .rr.yaml
- workDir string // working directory
- dotenv string // path to the .env file
- debug bool // debug mode
- override []string // override config values
+ cfgFile = strPtr("") // path to the .rr.yaml
+ workDir string // working directory
+ dotenv string // path to the .env file
+ debug bool // debug mode
+ override = &[]string{} // override config values
+ // do not print startup message
+ silent = boolPtr(false)
)
- var configPlugin = &config.Plugin{} // will be overwritten on pre-run action
-
cmd := &cobra.Command{
Use: cmdName,
Short: "High-performance PHP application server, load-balancer and process manager",
@@ -38,14 +40,18 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen
SilenceUsage: true,
Version: fmt.Sprintf("%s (build time: %s, %s)", meta.Version(), meta.BuildTime(), runtime.Version()),
PersistentPreRunE: func(*cobra.Command, []string) error {
- if cfgFile != "" {
- if absPath, err := filepath.Abs(cfgFile); err == nil {
- cfgFile = absPath // switch config path to the absolute
-
- // force working absPath related to config file
- if err = os.Chdir(filepath.Dir(absPath)); err != nil {
- return err
- }
+ // cfgFile could be defined by user or default `.rr.yaml`
+ // this check added just to be safe
+ if cfgFile == nil || *cfgFile == "" {
+ return errors.Str("no configuration file provided")
+ }
+
+ if absPath, err := filepath.Abs(*cfgFile); err == nil {
+ cfgFile = &absPath // switch config path to the absolute
+
+ // force working absPath related to config file
+ if err = os.Chdir(filepath.Dir(absPath)); err != nil {
+ return err
}
}
@@ -66,36 +72,37 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen
}
}
- cfg := &config.Plugin{Path: cfgFile, Prefix: "rr", Flags: override}
- if err := cfg.Init(); err != nil {
- return err
- }
-
if debug {
srv := dbg.NewServer()
go func() { _ = srv.Start(":6061") }() // TODO implement graceful server stopping
}
- // overwrite
- *configPlugin = *cfg
-
return nil
},
}
f := cmd.PersistentFlags()
- f.StringVarP(&cfgFile, "config", "c", ".rr.yaml", "config file")
- f.StringVarP(&workDir, "WorkDir", "w", "", "working directory") // TODO change to `workDir`?
+ f.StringVarP(cfgFile, "config", "c", ".rr.yaml", "config file")
+ f.StringVarP(&workDir, "WorkDir", "w", "", "working directory")
f.StringVarP(&dotenv, "dotenv", "", "", fmt.Sprintf("dotenv file [$%s]", envDotenv))
f.BoolVarP(&debug, "debug", "d", false, "debug mode")
- f.StringArrayVarP(&override, "override", "o", nil, "override config value (dot.notation=value)")
+ f.BoolVarP(silent, "silent", "s", false, "print startup message")
+ f.StringArrayVarP(override, "override", "o", nil, "override config value (dot.notation=value)")
cmd.AddCommand(
- workers.NewCommand(configPlugin),
- reset.NewCommand(configPlugin),
- serve.NewCommand(configPlugin),
+ workers.NewCommand(cfgFile),
+ reset.NewCommand(cfgFile),
+ serve.NewCommand(override, cfgFile, silent),
)
return cmd
}
+
+func strPtr(s string) *string {
+ return &s
+}
+
+func boolPtr(b bool) *bool {
+ return &b
+}