jpictl: introduce --config-file/-f as alternative to scanning m/
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
+38
-1
@@ -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)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user