summaryrefslogtreecommitdiff
path: root/internal/cli/root.go
blob: f71368f134f4ad9ec5b8f742c7cad58be0abe068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cli

import (
	"fmt"
	"os"
	"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"
	dbg "github.com/roadrunner-server/roadrunner/v2/internal/debug"
	"github.com/roadrunner-server/roadrunner/v2/internal/meta"

	"github.com/joho/godotenv"
	"github.com/spf13/cobra"
)

// NewCommand creates root command.
func NewCommand(cmdName string) *cobra.Command { //nolint:funlen
	const (
		envDotenv string = "DOTENV_PATH" // env var name: path to the .env file
	)

	var ( // flag 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)
	)

	cmd := &cobra.Command{
		Use:           cmdName,
		Short:         "High-performance PHP application server, load-balancer and process manager",
		SilenceErrors: true,
		SilenceUsage:  true,
		Version:       fmt.Sprintf("%s (build time: %s, %s)", meta.Version(), meta.BuildTime(), runtime.Version()),
		PersistentPreRunE: func(*cobra.Command, []string) error {
			// 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
				}
			}

			if workDir != "" {
				if err := os.Chdir(workDir); err != nil {
					return err
				}
			}

			if v, ok := os.LookupEnv(envDotenv); ok { // read path to the dotenv file from environment variable
				dotenv = v
			}

			if dotenv != "" {
				err := godotenv.Load(dotenv)
				if err != nil {
					return err
				}
			}

			if debug {
				srv := dbg.NewServer()
				go func() { _ = srv.Start(":6061") }() // TODO implement graceful server stopping
			}

			return nil
		},
	}

	f := cmd.PersistentFlags()

	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.BoolVarP(silent, "silent", "s", false, "print startup message")
	f.StringArrayVarP(override, "override", "o", nil, "override config value (dot.notation=value)")

	cmd.AddCommand(
		workers.NewCommand(cfgFile, override),
		reset.NewCommand(cfgFile, override),
		serve.NewCommand(override, cfgFile, silent),
	)

	return cmd
}

func strPtr(s string) *string {
	return &s
}

func boolPtr(b bool) *bool {
	return &b
}