Browse Source

Merge pull request 'jpictl: add flags to control the default scan' (#34)

Reviewed-on: #34
pull/32/head
Alejandro Mery 1 year ago
parent
commit
a928ab8880
  1. 57
      cmd/jpictl/config.go
  2. 21
      cmd/jpictl/log.go
  3. 19
      cmd/jpictl/main.go

57
cmd/jpictl/config.go

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

21
cmd/jpictl/log.go

@ -3,9 +3,13 @@ package main
import (
"fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
var log = zerolog.New(nil, slog.Error)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) {
l := log.Fatal()
@ -19,3 +23,20 @@ func fatal(err error, msg string, args ...any) {
panic("unreachable")
}
var verbosity int
// setVerbosity replaces the global logger using the
// verbosity level specified via -v flags
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}

19
cmd/jpictl/main.go

@ -2,8 +2,6 @@
package main
import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
@ -13,9 +11,7 @@ const (
)
var (
log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
@ -26,16 +22,3 @@ func main() {
fatal(err, "")
}
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}

Loading…
Cancel
Save