From b8e1b321e5961d8603ed4f58e35ffaeea25b058c Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 28 Oct 2023 21:14:50 +0000 Subject: [PATCH] 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") }