jpictl: introduce log verbosity flag

Signed-off-by: Nagy Károly Gábriel <k@jpi.io>
This commit is contained in:
2023-09-10 13:12:50 +03:00
parent 2016b27707
commit d1198328f6
6 changed files with 34 additions and 18 deletions
+1
View File
@@ -54,6 +54,7 @@ const encoding = YAMLEncoding
var dumpCmd = &cobra.Command{ var dumpCmd = &cobra.Command{
Use: "dump", Use: "dump",
Short: "generates a text representation of the config", Short: "generates a text representation of the config",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, _ []string) error {
var buf bytes.Buffer var buf bytes.Buffer
var enc Encoder var enc Encoder
+1
View File
@@ -10,6 +10,7 @@ import (
var envCmd = &cobra.Command{ var envCmd = &cobra.Command{
Use: "env", Use: "env",
Short: "generates environment variables for shell scripts", Short: "generates environment variables for shell scripts",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false) m, err := cfg.LoadZones(false)
if err != nil { if err != nil {
+1
View File
@@ -21,6 +21,7 @@ var gatewayCmd = &cobra.Command{
var gatewaySetCmd = &cobra.Command{ var gatewaySetCmd = &cobra.Command{
Use: "set", Use: "set",
Short: "gateway set sets machines as gateways", Short: "gateway set sets machines as gateways",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, args []string) error { RunE: func(_ *cobra.Command, args []string) error {
m, err := cfg.LoadZones(false) m, err := cfg.LoadZones(false)
if err != nil { if err != nil {
-6
View File
@@ -3,15 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog" "darvaza.org/slog"
) )
// TODO: make log level configurable via flags
var (
log = zerolog.New(nil, slog.Debug)
)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print() // fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) { func fatal(err error, msg string, args ...any) {
l := log.Fatal() l := log.Fatal()
+20 -2
View File
@@ -2,6 +2,8 @@
package main package main
import ( import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -10,13 +12,29 @@ const (
CmdName = "jpictl" CmdName = "jpictl"
) )
var rootCmd = &cobra.Command{ var (
log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
Use: CmdName, Use: CmdName,
Short: "control tool for jpi.cloud", Short: "control tool for jpi.cloud",
} }
)
func main() { func main() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fatal(err, "") 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 = log.WithLevel(slog.LogLevel(desired))
}
+1
View File
@@ -8,6 +8,7 @@ import (
var writeCmd = &cobra.Command{ var writeCmd = &cobra.Command{
Use: "write", Use: "write",
Short: "rewrites all config files", Short: "rewrites all config files",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false) m, err := cfg.LoadZones(false)
if err != nil { if err != nil {