package main 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{ Base: "m", Domain: "jpi.cloud", } // 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.Wrap(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)") }