From 0c5429a681d35b374fa88b3be9ac52761b5fc85c Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 28 Oct 2023 21:08:07 +0000 Subject: [PATCH 1/3] jpictl: move verbosity handling to the log module Signed-off-by: Alejandro Mery --- cmd/jpictl/log.go | 21 +++++++++++++++++++++ cmd/jpictl/main.go | 19 +------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cmd/jpictl/log.go b/cmd/jpictl/log.go index 709f5b7..7f31282 100644 --- a/cmd/jpictl/log.go +++ b/cmd/jpictl/log.go @@ -3,9 +3,13 @@ package main import ( "fmt" + "darvaza.org/sidecar/pkg/logger/zerolog" "darvaza.org/slog" + "github.com/spf13/cobra" ) +var log = zerolog.New(nil, slog.Error) + // fatal is a convenience wrapper for slog.Logger.Fatal().Print() func fatal(err error, msg string, args ...any) { l := log.Fatal() @@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) { panic("unreachable") } + +var verbosity int + +// setVerbosity replaces the global logger using the +// verbosity level specified via -v flags +func setVerbosity(_ *cobra.Command, _ []string) { + desired := int8(slog.Error) + int8(verbosity) + if desired > 6 { + desired = 6 + } + log = zerolog.New(nil, slog.LogLevel(desired)) +} + +func init() { + rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v", + "increase the verbosity level to Warn, Info or Debug") +} diff --git a/cmd/jpictl/main.go b/cmd/jpictl/main.go index 1cb84a6..5199ca6 100644 --- a/cmd/jpictl/main.go +++ b/cmd/jpictl/main.go @@ -2,8 +2,6 @@ package main import ( - "darvaza.org/sidecar/pkg/logger/zerolog" - "darvaza.org/slog" "github.com/spf13/cobra" ) @@ -13,9 +11,7 @@ const ( ) var ( - log = zerolog.New(nil, slog.Error) - verbosity int - rootCmd = &cobra.Command{ + rootCmd = &cobra.Command{ Use: CmdName, Short: "control tool for jpi.cloud", } @@ -26,16 +22,3 @@ func main() { fatal(err, "") } } - -func init() { - rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v", - "increase the verbosity level to Warn, Info or Debug") -} - -func setVerbosity(_ *cobra.Command, _ []string) { - desired := int8(slog.Error) + int8(verbosity) - if desired > 6 { - desired = 6 - } - log = zerolog.New(nil, slog.LogLevel(desired)) -} -- 2.17.1 From b8e1b321e5961d8603ed4f58e35ffaeea25b058c Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 28 Oct 2023 21:14:50 +0000 Subject: [PATCH 2/3] jpictl: add `-S` flag to ignore the config file and always scan Signed-off-by: Alejandro Mery --- cmd/jpictl/config.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/cmd/jpictl/config.go b/cmd/jpictl/config.go index 35c91a7..1f8aad8 100644 --- a/cmd/jpictl/config.go +++ b/cmd/jpictl/config.go @@ -22,6 +22,8 @@ type Config struct { ConfigFile string } +var forceScan bool + var cfg = &Config{ Base: "m", Domain: "jpi.cloud", @@ -30,19 +32,24 @@ var cfg = &Config{ // LoadZones loads all zones and machines in the config directory // or file func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) { - // try config file first - zones, err := cluster.NewFromConfig(cfg.ConfigFile, - cluster.ResolvePublicAddresses(resolve), - cluster.WithLogger(log), - ) + var zones *cluster.Cluster + var err error + + if !forceScan { + // try config file first + zones, err = cluster.NewFromConfig(cfg.ConfigFile, + cluster.ResolvePublicAddresses(resolve), + cluster.WithLogger(log), + ) - switch { - case err == nil: - // file was good - return zones, nil - case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile: - // file was bad - return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile) + switch { + case err == nil: + // file was good + return zones, nil + case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile: + // file was bad + return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile) + } } // default file doesn't exist. scan instead. @@ -53,7 +60,10 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) { } func init() { - rootCmd.PersistentFlags(). - StringVarP(&cfg.ConfigFile, "config-file", "f", - DefaultConfigFile, "config file (JSON or YAML)") + flags := rootCmd.PersistentFlags() + + flags.StringVarP(&cfg.ConfigFile, "config-file", "f", + DefaultConfigFile, "config file (JSON or YAML)") + flags.BoolVarP(&forceScan, "force-scan", "S", + false, "ignore config file and scan the directory instead") } -- 2.17.1 From 727fd67bc64a4132de22decadc7f83aa9771d193 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 28 Oct 2023 21:21:48 +0000 Subject: [PATCH 3/3] jpictl: add --domain/-D and --scan-dir/-d options Signed-off-by: Alejandro Mery --- cmd/jpictl/config.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/jpictl/config.go b/cmd/jpictl/config.go index 1f8aad8..31ee3ea 100644 --- a/cmd/jpictl/config.go +++ b/cmd/jpictl/config.go @@ -12,6 +12,14 @@ const ( // DefaultConfigFile is read if -f/--config-file isn't specified. // If it doesn't exist, m/ will be scanned DefaultConfigFile = "cloud.yaml" + + // DefaultClusterDir is the directory we will scan and write + // unless something else is indicated + DefaultClusterDir = "m" + + // DefaultDomain indicates the domain to use unless + // something else is specified + DefaultDomain = "jpi.cloud" ) // Config describes the repository @@ -25,8 +33,8 @@ type Config struct { var forceScan bool var cfg = &Config{ - Base: "m", - Domain: "jpi.cloud", + Base: DefaultClusterDir, + Domain: DefaultDomain, } // LoadZones loads all zones and machines in the config directory @@ -62,8 +70,13 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) { func init() { flags := rootCmd.PersistentFlags() + flags.StringVarP(&cfg.Base, "scan-dir", "d", + DefaultClusterDir, "directory to scan for cluster data") + flags.StringVarP(&cfg.Domain, "domain", "D", + DefaultDomain, "domain to use for scanned data") flags.StringVarP(&cfg.ConfigFile, "config-file", "f", DefaultConfigFile, "config file (JSON or YAML)") + flags.BoolVarP(&forceScan, "force-scan", "S", false, "ignore config file and scan the directory instead") } -- 2.17.1