jpictl: add -S flag to ignore the config file and always scan

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-10-28 21:14:50 +00:00
parent 0c5429a681
commit b8e1b321e5
+25 -15
View File
@@ -22,6 +22,8 @@ type Config struct {
ConfigFile string ConfigFile string
} }
var forceScan bool
var cfg = &Config{ var cfg = &Config{
Base: "m", Base: "m",
Domain: "jpi.cloud", Domain: "jpi.cloud",
@@ -30,19 +32,24 @@ var cfg = &Config{
// LoadZones loads all zones and machines in the config directory // LoadZones loads all zones and machines in the config directory
// or file // or file
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) { func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
// try config file first var zones *cluster.Cluster
zones, err := cluster.NewFromConfig(cfg.ConfigFile, var err error
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
switch { if !forceScan {
case err == nil: // try config file first
// file was good zones, err = cluster.NewFromConfig(cfg.ConfigFile,
return zones, nil cluster.ResolvePublicAddresses(resolve),
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile: cluster.WithLogger(log),
// 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. // default file doesn't exist. scan instead.
@@ -53,7 +60,10 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
} }
func init() { func init() {
rootCmd.PersistentFlags(). flags := rootCmd.PersistentFlags()
StringVarP(&cfg.ConfigFile, "config-file", "f",
DefaultConfigFile, "config file (JSON or YAML)") 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")
} }