From a0cc698a3952e6cf2c539a6ad5728dbd2a765bfb Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 10 Sep 2023 23:38:46 +0000 Subject: [PATCH] jpictl: introduce --config-file/-f as alternative to scanning m/ Signed-off-by: Alejandro Mery --- cmd/jpictl/config.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/cmd/jpictl/config.go b/cmd/jpictl/config.go index a7649fc..3f71211 100644 --- a/cmd/jpictl/config.go +++ b/cmd/jpictl/config.go @@ -1,11 +1,25 @@ package main -import "git.jpi.io/amery/jpictl/pkg/cluster" +import ( + "os" + + "darvaza.org/core" + + "git.jpi.io/amery/jpictl/pkg/cluster" +) + +const ( + // DefaultConfigFile is read if -f/--config-file isn't specified. + // If it doesn't exist, m/ will be scanned + DefaultConfigFile = "cloud.yaml" +) // Config describes the repository type Config struct { Base string Domain string + + ConfigFile string } var cfg = &Config{ @@ -14,9 +28,32 @@ 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), + ) + + switch { + case err == nil: + // file was good + return zones, nil + case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile: + // file was bad + return nil, core.Wrapf(err, "NewFromConfig(%q)", cfg.ConfigFile) + } + + // default file doesn't exist. scan instead. return cluster.NewFromDirectory(cfg.Base, cfg.Domain, cluster.ResolvePublicAddresses(resolve), cluster.WithLogger(log), ) } + +func init() { + rootCmd.PersistentFlags(). + StringVarP(&cfg.ConfigFile, "config-file", "f", + DefaultConfigFile, "config file (JSON or YAML)") +}