Browse Source

jpictl: introduce log verbosity flag

Signed-off-by: Nagy Károly Gábriel <k@jpi.io>
pull/14/head
parent
commit
d1198328f6
Signed by: karasz
GPG Key ID: FA002E83BC206F83
  1. 1
      cmd/jpictl/dump.go
  2. 1
      cmd/jpictl/env.go
  3. 1
      cmd/jpictl/gateway.go
  4. 6
      cmd/jpictl/log.go
  5. 22
      cmd/jpictl/main.go
  6. 1
      cmd/jpictl/write.go

1
cmd/jpictl/dump.go

@ -54,6 +54,7 @@ const encoding = YAMLEncoding
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "generates a text representation of the config",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
var buf bytes.Buffer
var enc Encoder

1
cmd/jpictl/env.go

@ -10,6 +10,7 @@ import (
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {

1
cmd/jpictl/gateway.go

@ -21,6 +21,7 @@ var gatewayCmd = &cobra.Command{
var gatewaySetCmd = &cobra.Command{
Use: "set",
Short: "gateway set sets machines as gateways",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, args []string) error {
m, err := cfg.LoadZones(false)
if err != nil {

6
cmd/jpictl/log.go

@ -3,15 +3,9 @@ package main
import (
"fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"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()
func fatal(err error, msg string, args ...any) {
l := log.Fatal()

22
cmd/jpictl/main.go

@ -2,6 +2,8 @@
package main
import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
@ -10,13 +12,29 @@ const (
CmdName = "jpictl"
)
var rootCmd = &cobra.Command{
var (
log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
}
)
func main() {
if err := rootCmd.Execute(); err != nil {
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
cmd/jpictl/write.go

@ -8,6 +8,7 @@ import (
var writeCmd = &cobra.Command{
Use: "write",
Short: "rewrites all config files",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {

Loading…
Cancel
Save