|
|
|
@ -12,6 +12,14 @@ const (
|
|
|
|
|
// DefaultConfigFile is read if -f/--config-file isn't specified.
|
|
|
|
|
// If it doesn't exist, m/ will be scanned
|
|
|
|
|
DefaultConfigFile = "cloud.yaml" |
|
|
|
|
|
|
|
|
|
// DefaultClusterDir is the directory we will scan and write
|
|
|
|
|
// unless something else is indicated
|
|
|
|
|
DefaultClusterDir = "m" |
|
|
|
|
|
|
|
|
|
// DefaultDomain indicates the domain to use unless
|
|
|
|
|
// something else is specified
|
|
|
|
|
DefaultDomain = "jpi.cloud" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Config describes the repository
|
|
|
|
@ -22,27 +30,34 @@ type Config struct {
|
|
|
|
|
ConfigFile string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var forceScan bool |
|
|
|
|
|
|
|
|
|
var cfg = &Config{ |
|
|
|
|
Base: "m", |
|
|
|
|
Domain: "jpi.cloud", |
|
|
|
|
Base: DefaultClusterDir, |
|
|
|
|
Domain: DefaultDomain, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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 +68,15 @@ 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.Base, "scan-dir", "d", |
|
|
|
|
DefaultClusterDir, "directory to scan for cluster data") |
|
|
|
|
flags.StringVarP(&cfg.Domain, "domain", "D", |
|
|
|
|
DefaultDomain, "domain to use for scanned data") |
|
|
|
|
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") |
|
|
|
|
} |
|
|
|
|