diff options
author | Valery Piashchynski <[email protected]> | 2022-03-06 12:13:02 +0100 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-03-06 12:13:02 +0100 |
commit | 70e4f020afd0352dc52114651f0f65c1965ed399 (patch) | |
tree | cc6595214e4ff39600099fb8c6f39e7a3dd560e9 /internal/cli | |
parent | 587702be62b65c151d27dc79e62fcbbd11290e6f (diff) |
remove config plugin usage from the root
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal/cli')
-rw-r--r-- | internal/cli/reset/command.go | 9 | ||||
-rw-r--r-- | internal/cli/reset/command_test.go | 4 | ||||
-rw-r--r-- | internal/cli/root.go | 69 | ||||
-rw-r--r-- | internal/cli/serve/command.go | 28 | ||||
-rw-r--r-- | internal/cli/serve/command_test.go | 11 | ||||
-rw-r--r-- | internal/cli/workers/command.go | 9 | ||||
-rw-r--r-- | internal/cli/workers/command_test.go | 5 |
7 files changed, 84 insertions, 51 deletions
diff --git a/internal/cli/reset/command.go b/internal/cli/reset/command.go index 1d7dd78c..a0634c31 100644 --- a/internal/cli/reset/command.go +++ b/internal/cli/reset/command.go @@ -8,7 +8,6 @@ import ( "github.com/fatih/color" "github.com/mattn/go-runewidth" - "github.com/roadrunner-server/config/v2" "github.com/roadrunner-server/errors" "github.com/spf13/cobra" "github.com/vbauerster/mpb/v5" @@ -18,7 +17,7 @@ import ( var spinnerStyle = []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"} //nolint:gochecknoglobals // NewCommand creates `reset` command. -func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen +func NewCommand(cfgFile *string) *cobra.Command { //nolint:funlen return &cobra.Command{ Use: "reset", Short: "Reset workers of all or specific RoadRunner service", @@ -29,7 +28,11 @@ func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen resetterReset = "resetter.Reset" ) - client, err := internalRpc.NewClient(cfgPlugin) + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } + + client, err := internalRpc.NewClient(*cfgFile) if err != nil { return err } diff --git a/internal/cli/reset/command_test.go b/internal/cli/reset/command_test.go index 1983cc5c..ca68cb54 100644 --- a/internal/cli/reset/command_test.go +++ b/internal/cli/reset/command_test.go @@ -5,12 +5,12 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/cli/reset" - "github.com/roadrunner-server/config/v2" "github.com/stretchr/testify/assert" ) func TestCommandProperties(t *testing.T) { - cmd := reset.NewCommand(&config.Plugin{}) + path := "" + cmd := reset.NewCommand(&path) assert.Equal(t, "reset", cmd.Use) assert.NotNil(t, cmd.RunE) 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 +} diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index a0ce36db..559868d5 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -14,23 +14,35 @@ import ( "github.com/spf13/cobra" ) +const ( + rrPrefix string = "rr" +) + // NewCommand creates `serve` command. -func NewCommand(cfgPlugin *configImpl.Plugin) *cobra.Command { //nolint:funlen +func NewCommand(override *[]string, cfgFile *string, silent *bool) *cobra.Command { //nolint:funlen,gocognit return &cobra.Command{ Use: "serve", Short: "Start RoadRunner server", RunE: func(*cobra.Command, []string) error { const op = errors.Op("handle_serve_command") + // just to be safe + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } // create endure container config - containerCfg, err := container.NewConfig(cfgPlugin) + containerCfg, err := container.NewConfig(*cfgFile) if err != nil { return errors.E(op, err) } - // set the grace period which would be same for all the plugins - cfgPlugin.Timeout = containerCfg.GracePeriod - cfgPlugin.Version = meta.Version() + cfg := &configImpl.Plugin{ + Path: *cfgFile, + Prefix: rrPrefix, + Timeout: containerCfg.GracePeriod, + Flags: *override, + Version: meta.Version(), + } // create endure container endureContainer, err := container.NewContainer(*containerCfg) @@ -39,7 +51,7 @@ func NewCommand(cfgPlugin *configImpl.Plugin) *cobra.Command { //nolint:funlen } // register config plugin - if err = endureContainer.Register(cfgPlugin); err != nil { + if err = endureContainer.Register(cfg); err != nil { return errors.E(op, err) } @@ -77,7 +89,9 @@ func NewCommand(cfgPlugin *configImpl.Plugin) *cobra.Command { //nolint:funlen os.Exit(1) }() - fmt.Printf("[INFO] RoadRunner server started; version: %s, buildtime: %s\n", meta.Version(), meta.BuildTime()) + if !*silent { + fmt.Printf("[INFO] RoadRunner server started; version: %s, buildtime: %s\n", meta.Version(), meta.BuildTime()) + } for { select { diff --git a/internal/cli/serve/command_test.go b/internal/cli/serve/command_test.go index 8aef0ff7..4c8c6128 100644 --- a/internal/cli/serve/command_test.go +++ b/internal/cli/serve/command_test.go @@ -5,12 +5,19 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/cli/serve" - "github.com/roadrunner-server/config/v2" "github.com/stretchr/testify/assert" ) func TestCommandProperties(t *testing.T) { - cmd := serve.NewCommand(&config.Plugin{}) + path := "" + cmd := serve.NewCommand(nil, &path, nil) + + assert.Equal(t, "serve", cmd.Use) + assert.NotNil(t, cmd.RunE) +} + +func TestCommandNil(t *testing.T) { + cmd := serve.NewCommand(nil, nil, nil) assert.Equal(t, "serve", cmd.Use) assert.NotNil(t, cmd.RunE) diff --git a/internal/cli/workers/command.go b/internal/cli/workers/command.go index e3f8f7e3..f498b7af 100644 --- a/internal/cli/workers/command.go +++ b/internal/cli/workers/command.go @@ -13,14 +13,13 @@ import ( tm "github.com/buger/goterm" "github.com/fatih/color" - "github.com/roadrunner-server/config/v2" "github.com/roadrunner-server/errors" "github.com/roadrunner-server/informer/v2" "github.com/spf13/cobra" ) // NewCommand creates `workers` command. -func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen +func NewCommand(cfgFile *string) *cobra.Command { //nolint:funlen var ( // interactive workers updates interactive bool @@ -35,7 +34,11 @@ func NewCommand(cfgPlugin *config.Plugin) *cobra.Command { //nolint:funlen informerList = "informer.List" ) - client, err := internalRpc.NewClient(cfgPlugin) + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } + + client, err := internalRpc.NewClient(*cfgFile) if err != nil { return err } diff --git a/internal/cli/workers/command_test.go b/internal/cli/workers/command_test.go index d0c92e63..a3d144df 100644 --- a/internal/cli/workers/command_test.go +++ b/internal/cli/workers/command_test.go @@ -5,19 +5,18 @@ import ( "github.com/roadrunner-server/roadrunner/v2/internal/cli/workers" - "github.com/roadrunner-server/config/v2" "github.com/stretchr/testify/assert" ) func TestCommandProperties(t *testing.T) { - cmd := workers.NewCommand(&config.Plugin{}) + cmd := workers.NewCommand(nil) assert.Equal(t, "workers", cmd.Use) assert.NotNil(t, cmd.RunE) } func TestCommandFlags(t *testing.T) { - cmd := workers.NewCommand(&config.Plugin{}) + cmd := workers.NewCommand(nil) cases := []struct { giveName string |