summaryrefslogtreecommitdiff
path: root/cmd/cli/root.go
blob: 00d192f611cf6e65d137bda7b4c9f402e5f29807 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cli

import (
	"log"
	"net/rpc"
	"os"
	"path/filepath"

	"github.com/spiral/errors"
	goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc"
	"github.com/spiral/roadrunner-plugins/logger"
	rpcPlugin "github.com/spiral/roadrunner-plugins/rpc"

	"github.com/spiral/roadrunner-plugins/config"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"

	"github.com/spf13/cobra"
	"github.com/spiral/endure"
)

var (
	WorkDir   string
	CfgFile   string
	Container *endure.Endure
	Logger    *zap.Logger
	cfg       *config.Viper
	root      = &cobra.Command{
		Use:           "rr",
		SilenceErrors: true,
		SilenceUsage:  true,
	}
)

func Execute() {
	if err := root.Execute(); err != nil {
		// exit with error, fatal invoke os.Exit(1)
		log.Fatal(err)
	}
}

func init() {
	root.PersistentFlags().StringVarP(&CfgFile, "config", "c", ".rr.yaml", "config file (default is .rr.yaml)")
	root.PersistentFlags().StringVarP(&WorkDir, "WorkDir", "w", "", "work directory")

	// todo: properly handle debug level
	Logger = initLogger()
	// endureLogger := logger.NewZapAdapter(Logger)

	cobra.OnInitialize(func() {
		if CfgFile != "" {
			if absPath, err := filepath.Abs(CfgFile); err == nil {
				CfgFile = absPath

				// force working absPath related to config file
				if err := os.Chdir(filepath.Dir(absPath)); err != nil {
					panic(err)
				}
			}
		}

		if WorkDir != "" {
			if err := os.Chdir(WorkDir); err != nil {
				panic(err)
			}
		}

		// todo: config is global, not only for serve
		cfg = &config.Viper{}
		cfg.Path = CfgFile
		cfg.Prefix = "rr"

		err := Container.Register(cfg)
		if err != nil {
			panic(err)
		}

		err = Container.Register(&logger.ZapLogger{})
		if err != nil {
			panic(err)
		}
	})
}

// todo: improve
func RPCClient() (*rpc.Client, error) {
	rpcConfig := &rpcPlugin.Config{}

	err := cfg.Init()
	if err != nil {
		return nil, err
	}

	if !cfg.Has(rpcPlugin.PluginName) {
		return nil, errors.E("rpc service disabled")
	}

	err = cfg.UnmarshalKey(rpcPlugin.PluginName, rpcConfig)
	if err != nil {
		return nil, err
	}
	rpcConfig.InitDefaults()

	conn, err := rpcConfig.Dialer()
	if err != nil {
		return nil, err
	}

	return rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)), nil
}

func initLogger() *zap.Logger {
	// todo: we do not need it
	cfg := zap.Config{
		Level:    zap.NewAtomicLevelAt(zap.ErrorLevel),
		Encoding: "console",
		EncoderConfig: zapcore.EncoderConfig{
			MessageKey:    "message",
			LevelKey:      "level",
			TimeKey:       "time",
			CallerKey:     "caller",
			NameKey:       "name",
			StacktraceKey: "stack",
			EncodeLevel:   zapcore.CapitalLevelEncoder,
			EncodeTime:    zapcore.ISO8601TimeEncoder,
			EncodeCaller:  zapcore.ShortCallerEncoder,
		},
		OutputPaths:      []string{"stderr"},
		ErrorOutputPaths: []string{"stderr"},
	}

	l, err := cfg.Build(zap.AddCaller())
	if err != nil {
		panic(err)
	}

	return l
}