blob: 21f87a692fb2cbd54050a56eba4b0d5e86feaff6 [file] [log] [blame]
Lorenz Brun6adf8842021-10-05 13:39:11 +02001package main
2
3import (
Serge Bazanski7eeef0f2024-02-05 14:40:15 +01004 "context"
5 "crypto/x509"
Serge Bazanskica8d9512024-09-12 14:20:57 +02006 "os"
Mateusz Zalega8234c162022-07-08 17:05:50 +02007 "path/filepath"
8
9 "github.com/adrg/xdg"
Lorenz Brun6adf8842021-10-05 13:39:11 +020010 "github.com/spf13/cobra"
Serge Bazanski1f8cad72023-03-20 16:58:10 +010011
Serge Bazanskica8d9512024-09-12 14:20:57 +020012 "source.monogon.dev/go/logging"
Serge Bazanski1f8cad72023-03-20 16:58:10 +010013 "source.monogon.dev/metropolis/cli/metroctl/core"
Lorenz Brun6adf8842021-10-05 13:39:11 +020014)
15
16// rootCmd represents the base command when called without any subcommands
17var rootCmd = &cobra.Command{
Tim Windelschmidtfc6e1cf2024-09-18 17:34:07 +020018 Use: "metroctl",
19 Short: "metroctl controls Metropolis nodes and clusters.",
20 SilenceUsage: true,
21 SilenceErrors: true,
Lorenz Brun6adf8842021-10-05 13:39:11 +020022}
23
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020024type metroctlFlags struct {
25 // clusterEndpoints is a list of the targeted cluster's endpoints, used by
26 // commands that perform RPC on it.
27 clusterEndpoints []string
Mateusz Zalegaf7774962022-07-08 12:26:55 +020028 // proxyAddr is a SOCKS5 proxy address the cluster will be accessed through.
29 proxyAddr string
Mateusz Zalega8234c162022-07-08 17:05:50 +020030 // configPath overrides the default XDG config path
31 configPath string
Mateusz Zalegab2cac082022-07-14 14:55:43 +020032 // verbose, if set, will make this utility log additional runtime
33 // information.
34 verbose bool
Mateusz Zalegadb75e212022-08-04 17:31:34 +020035 // format refers to how the output data, except logs, is formatted.
36 format string
37 // filter specifies a CEL filter used to narrow down the set of output
38 // objects.
39 filter string
40 // output is an optional output file path the resulting data will be saved
41 // at. If unspecified, the data will be written to stdout.
42 output string
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010043 // acceptAnyCA will persist the first encountered (while connecting) CA
44 // certificate of the cluster as the trusted CA certificate for this cluster.
45 // This is unsafe and should only be used for testing.
46 acceptAnyCA bool
Serge Bazanski98840342024-05-22 13:03:55 +020047 // columns is a comma-separated list of column names which selects which columns
48 // will be output to the user. An empty string means all columns will be
49 // displayed.
50 columns string
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020051}
52
53var flags metroctlFlags
54
55func init() {
Serge Bazanskicf33f682023-03-17 00:16:16 +010056 rootCmd.PersistentFlags().StringSliceVar(&flags.clusterEndpoints, "endpoints", nil, "A list of the target cluster's endpoints.")
Mateusz Zalegaf7774962022-07-08 12:26:55 +020057 rootCmd.PersistentFlags().StringVar(&flags.proxyAddr, "proxy", "", "SOCKS5 proxy address")
Mateusz Zalega8234c162022-07-08 17:05:50 +020058 rootCmd.PersistentFlags().StringVar(&flags.configPath, "config", filepath.Join(xdg.ConfigHome, "metroctl"), "An alternative cluster config path")
Mateusz Zalegab2cac082022-07-14 14:55:43 +020059 rootCmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Log additional runtime information")
Serge Bazanskie012b722023-03-29 17:49:04 +020060 rootCmd.PersistentFlags().StringVar(&flags.format, "format", "plaintext", "Data output format")
Mateusz Zalegadb75e212022-08-04 17:31:34 +020061 rootCmd.PersistentFlags().StringVar(&flags.filter, "filter", "", "The object filter applied to the output data")
Serge Bazanski98840342024-05-22 13:03:55 +020062 rootCmd.PersistentFlags().StringVar(&flags.columns, "columns", "", "Comma-separated list of column names to show. If not set, all columns will be shown")
Mateusz Zalegadb75e212022-08-04 17:31:34 +020063 rootCmd.PersistentFlags().StringVarP(&flags.output, "output", "o", "", "Redirects output to the specified file")
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010064 rootCmd.PersistentFlags().BoolVar(&flags.acceptAnyCA, "insecure-accept-and-persist-first-encountered-ca", false, "Accept the first encountered CA while connecting as the trusted CA for future metroctl connections with this config path. This is very insecure and should only be used for testing.")
Tim Windelschmidtfc6e1cf2024-09-18 17:34:07 +020065 rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
66 cmd.PrintErr(cmd.UsageString())
67 return err
68 })
69}
70
71func PrintUsageOnWrongArgs(pArgs cobra.PositionalArgs) cobra.PositionalArgs {
72 return func(cmd *cobra.Command, args []string) error {
73 err := pArgs(cmd, args)
74 if err != nil {
75 cmd.PrintErr(cmd.UsageString())
76 }
77 return err
78 }
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020079}
80
Lorenz Brun6adf8842021-10-05 13:39:11 +020081func main() {
82 cobra.CheckErr(rootCmd.Execute())
83}
Serge Bazanski1f8cad72023-03-20 16:58:10 +010084
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010085type acceptall struct{}
86
87func (a *acceptall) Ask(ctx context.Context, _ *core.ConnectOptions, _ *x509.Certificate) (bool, error) {
88 return true, nil
89}
90
Serge Bazanski1f8cad72023-03-20 16:58:10 +010091// connectOptions returns core.ConnectOptions as defined by the metroctl flags
92// currently set.
93func connectOptions() *core.ConnectOptions {
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010094 var tofu core.CertificateTOFU
95 if flags.acceptAnyCA {
96 tofu = &acceptall{}
97 }
Serge Bazanskica8d9512024-09-12 14:20:57 +020098 logger := logging.NewWriterBackend(os.Stderr)
99 if !flags.verbose {
100 logger.MinimumSeverity = logging.WARNING
101 }
Serge Bazanski1f8cad72023-03-20 16:58:10 +0100102 return &core.ConnectOptions{
Serge Bazanski925ec3d2024-02-05 14:38:20 +0100103 ConfigPath: flags.configPath,
104 ProxyServer: flags.proxyAddr,
105 Endpoints: flags.clusterEndpoints,
Serge Bazanskica8d9512024-09-12 14:20:57 +0200106 ResolverLogger: logger,
Serge Bazanski7eeef0f2024-02-05 14:40:15 +0100107 TOFU: tofu,
Serge Bazanski1f8cad72023-03-20 16:58:10 +0100108 }
109}