Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 4 | "log" |
Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 5 | "path/filepath" |
| 6 | |
| 7 | "github.com/adrg/xdg" |
Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 8 | "github.com/spf13/cobra" |
| 9 | ) |
| 10 | |
| 11 | // rootCmd represents the base command when called without any subcommands |
| 12 | var rootCmd = &cobra.Command{ |
| 13 | Use: "metroctl", |
| 14 | Short: "metroctl controls Metropolis nodes and clusters.", |
| 15 | } |
| 16 | |
Mateusz Zalega | d5f2f7a | 2022-07-05 18:48:56 +0200 | [diff] [blame] | 17 | type metroctlFlags struct { |
| 18 | // clusterEndpoints is a list of the targeted cluster's endpoints, used by |
| 19 | // commands that perform RPC on it. |
| 20 | clusterEndpoints []string |
Mateusz Zalega | f777496 | 2022-07-08 12:26:55 +0200 | [diff] [blame] | 21 | // proxyAddr is a SOCKS5 proxy address the cluster will be accessed through. |
| 22 | proxyAddr string |
Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 23 | // configPath overrides the default XDG config path |
| 24 | configPath string |
Mateusz Zalega | b2cac08 | 2022-07-14 14:55:43 +0200 | [diff] [blame] | 25 | // verbose, if set, will make this utility log additional runtime |
| 26 | // information. |
| 27 | verbose bool |
Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame] | 28 | // format refers to how the output data, except logs, is formatted. |
| 29 | format string |
| 30 | // filter specifies a CEL filter used to narrow down the set of output |
| 31 | // objects. |
| 32 | filter string |
| 33 | // output is an optional output file path the resulting data will be saved |
| 34 | // at. If unspecified, the data will be written to stdout. |
| 35 | output string |
Mateusz Zalega | d5f2f7a | 2022-07-05 18:48:56 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var flags metroctlFlags |
| 39 | |
| 40 | func init() { |
| 41 | rootCmd.PersistentFlags().StringArrayVar(&flags.clusterEndpoints, "endpoints", nil, "A list of the target cluster's endpoints.") |
Mateusz Zalega | f777496 | 2022-07-08 12:26:55 +0200 | [diff] [blame] | 42 | rootCmd.PersistentFlags().StringVar(&flags.proxyAddr, "proxy", "", "SOCKS5 proxy address") |
Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 43 | rootCmd.PersistentFlags().StringVar(&flags.configPath, "config", filepath.Join(xdg.ConfigHome, "metroctl"), "An alternative cluster config path") |
Mateusz Zalega | b2cac08 | 2022-07-14 14:55:43 +0200 | [diff] [blame] | 44 | rootCmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Log additional runtime information") |
Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame] | 45 | rootCmd.PersistentFlags().StringVarP(&flags.format, "format", "f", "plaintext", "Data output format") |
| 46 | rootCmd.PersistentFlags().StringVar(&flags.filter, "filter", "", "The object filter applied to the output data") |
| 47 | rootCmd.PersistentFlags().StringVarP(&flags.output, "output", "o", "", "Redirects output to the specified file") |
Mateusz Zalega | d5f2f7a | 2022-07-05 18:48:56 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 50 | // rpcLogger passes through the cluster resolver logs, if "--verbose" flag was |
| 51 | // used. |
| 52 | func rpcLogger(f string, args ...interface{}) { |
| 53 | if flags.verbose { |
Serge Bazanski | 4c50f99 | 2022-09-05 18:43:01 +0200 | [diff] [blame] | 54 | log.Printf("resolver: "+f, args...) |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Lorenz Brun | 6adf884 | 2021-10-05 13:39:11 +0200 | [diff] [blame] | 58 | func main() { |
| 59 | cobra.CheckErr(rootCmd.Execute()) |
| 60 | } |