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)") +}