diff options
author | Valery Piashchynski <[email protected]> | 2020-12-24 11:05:00 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-24 11:05:00 +0300 |
commit | 439c93225d7a9ebaf7cbf1010a54594b906f7d54 (patch) | |
tree | adc7d78e28a9512cb1b1ba85cb4f19dec7d1323d /cmd/cli/root.go | |
parent | 40b6c3169931a3fef62b649db19ff01dc685b7d4 (diff) | |
parent | 62ee1770cc233328300438ffd690ea1d8fc747bb (diff) |
Merge pull request #462 from spiral/feature/CLIv2.0.0-alpha29
feature/CLI
Diffstat (limited to 'cmd/cli/root.go')
-rw-r--r-- | cmd/cli/root.go | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/cmd/cli/root.go b/cmd/cli/root.go new file mode 100644 index 00000000..febe410b --- /dev/null +++ b/cmd/cli/root.go @@ -0,0 +1,97 @@ +package cli + +import ( + "log" + "net/rpc" + "os" + "path/filepath" + + "github.com/spiral/errors" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" + rpcPlugin "github.com/spiral/roadrunner-plugins/rpc" + + "github.com/spiral/roadrunner-plugins/config" + + "github.com/spf13/cobra" + "github.com/spiral/endure" +) + +var ( + WorkDir string + CfgFile string + Container *endure.Endure + 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") + + 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) + } + } + + cfg = &config.Viper{} + cfg.Path = CfgFile + cfg.Prefix = "rr" + + // register config + err := Container.Register(cfg) + if err != nil { + panic(err) + } + }) +} + +// RPCClient is using to make a requests to the ./rr reset, ./rr workers +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 +} |