jpictl: introduce log verbosity flag
Signed-off-by: Nagy Károly Gábriel <k@jpi.io>
This commit is contained in:
+3
-2
@@ -52,8 +52,9 @@ const encoding = YAMLEncoding
|
|||||||
|
|
||||||
// Command
|
// Command
|
||||||
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
|
||||||
|
|||||||
+3
-2
@@ -8,8 +8,9 @@ import (
|
|||||||
|
|
||||||
// Command
|
// Command
|
||||||
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 {
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ var gatewayCmd = &cobra.Command{
|
|||||||
|
|
||||||
// gateway set
|
// gateway set
|
||||||
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 {
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
+22
-4
@@ -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 (
|
||||||
Use: CmdName,
|
log = zerolog.New(nil, slog.Error)
|
||||||
Short: "control tool for jpi.cloud",
|
verbosity int
|
||||||
}
|
rootCmd = &cobra.Command{
|
||||||
|
Use: CmdName,
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|||||||
+3
-2
@@ -6,8 +6,9 @@ import (
|
|||||||
|
|
||||||
// Command
|
// Command
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user