jpictl: introduce --config-file/-f as alternative to scanning m/

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-09-10 23:38:46 +00:00
parent a02b7574b0
commit 3ad6adec3b
+37 -1
View File
@@ -1,20 +1,56 @@
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{
Base: "m",
Domain: "jpi.cloud",
ConfigFile: rootCmd.PersistentFlags().
StringP("config-file", "f", DefaultConfigFile, "config file (JSON or YAML)"),
}
// LoadZones loads all zones and machines in the config directory
// or file
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
configFile := *cfg.ConfigFile
// try config file first
zones, err := cluster.NewFromConfig(configFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || configFile != DefaultConfigFile:
// file was bad
return nil, core.Wrapf(err, "NewFromConfig(%q)", configFile)
}
// default file doesn't exist. scan instead.
return cluster.NewFromDirectory(cfg.Base, cfg.Domain,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),