blob: 19b378da7c003ea571df7226cf52f96fc69939ab [file] [log] [blame]
Lorenz Brun6adf8842021-10-05 13:39:11 +02001package main
2
3import (
Mateusz Zalega18a67b02022-08-02 13:37:50 +02004 "log"
Mateusz Zalega8234c162022-07-08 17:05:50 +02005 "path/filepath"
6
7 "github.com/adrg/xdg"
Lorenz Brun6adf8842021-10-05 13:39:11 +02008 "github.com/spf13/cobra"
Serge Bazanski1f8cad72023-03-20 16:58:10 +01009
10 "source.monogon.dev/metropolis/cli/metroctl/core"
Lorenz Brun6adf8842021-10-05 13:39:11 +020011)
12
13// rootCmd represents the base command when called without any subcommands
14var rootCmd = &cobra.Command{
15 Use: "metroctl",
16 Short: "metroctl controls Metropolis nodes and clusters.",
17}
18
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020019type metroctlFlags struct {
20 // clusterEndpoints is a list of the targeted cluster's endpoints, used by
21 // commands that perform RPC on it.
22 clusterEndpoints []string
Mateusz Zalegaf7774962022-07-08 12:26:55 +020023 // proxyAddr is a SOCKS5 proxy address the cluster will be accessed through.
24 proxyAddr string
Mateusz Zalega8234c162022-07-08 17:05:50 +020025 // configPath overrides the default XDG config path
26 configPath string
Mateusz Zalegab2cac082022-07-14 14:55:43 +020027 // verbose, if set, will make this utility log additional runtime
28 // information.
29 verbose bool
Mateusz Zalegadb75e212022-08-04 17:31:34 +020030 // format refers to how the output data, except logs, is formatted.
31 format string
32 // filter specifies a CEL filter used to narrow down the set of output
33 // objects.
34 filter string
35 // output is an optional output file path the resulting data will be saved
36 // at. If unspecified, the data will be written to stdout.
37 output string
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020038}
39
40var flags metroctlFlags
41
42func init() {
Serge Bazanskicf33f682023-03-17 00:16:16 +010043 rootCmd.PersistentFlags().StringSliceVar(&flags.clusterEndpoints, "endpoints", nil, "A list of the target cluster's endpoints.")
Mateusz Zalegaf7774962022-07-08 12:26:55 +020044 rootCmd.PersistentFlags().StringVar(&flags.proxyAddr, "proxy", "", "SOCKS5 proxy address")
Mateusz Zalega8234c162022-07-08 17:05:50 +020045 rootCmd.PersistentFlags().StringVar(&flags.configPath, "config", filepath.Join(xdg.ConfigHome, "metroctl"), "An alternative cluster config path")
Mateusz Zalegab2cac082022-07-14 14:55:43 +020046 rootCmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Log additional runtime information")
Mateusz Zalegadb75e212022-08-04 17:31:34 +020047 rootCmd.PersistentFlags().StringVarP(&flags.format, "format", "f", "plaintext", "Data output format")
48 rootCmd.PersistentFlags().StringVar(&flags.filter, "filter", "", "The object filter applied to the output data")
49 rootCmd.PersistentFlags().StringVarP(&flags.output, "output", "o", "", "Redirects output to the specified file")
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020050}
51
Mateusz Zalega18a67b02022-08-02 13:37:50 +020052// rpcLogger passes through the cluster resolver logs, if "--verbose" flag was
53// used.
54func rpcLogger(f string, args ...interface{}) {
55 if flags.verbose {
Serge Bazanski4c50f992022-09-05 18:43:01 +020056 log.Printf("resolver: "+f, args...)
Mateusz Zalega18a67b02022-08-02 13:37:50 +020057 }
58}
59
Lorenz Brun6adf8842021-10-05 13:39:11 +020060func main() {
61 cobra.CheckErr(rootCmd.Execute())
62}
Serge Bazanski1f8cad72023-03-20 16:58:10 +010063
64// connectOptions returns core.ConnectOptions as defined by the metroctl flags
65// currently set.
66func connectOptions() *core.ConnectOptions {
67 return &core.ConnectOptions{
68 ProxyServer: flags.proxyAddr,
69 Endpoints: flags.clusterEndpoints,
70 ConfigPath: flags.configPath,
71 }
72}