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. 5
      cmd/jpictl/dump.go
  2. 5
      cmd/jpictl/env.go
  3. 5
      cmd/jpictl/gateway.go
  4. 6
      cmd/jpictl/log.go
  5. 26
      cmd/jpictl/main.go
  6. 5
      cmd/jpictl/write.go

5
cmd/jpictl/dump.go

@ -52,8 +52,9 @@ const encoding = YAMLEncoding
// Command
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "generates a text representation of the config",
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

5
cmd/jpictl/env.go

@ -8,8 +8,9 @@ import (
// Command
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
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 {

5
cmd/jpictl/gateway.go

@ -19,8 +19,9 @@ var gatewayCmd = &cobra.Command{
// gateway set
var gatewaySetCmd = &cobra.Command{
Use: "set",
Short: "gateway set sets machines as gateways",
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()

26
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{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
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))
}

5
cmd/jpictl/write.go

@ -6,8 +6,9 @@ import (
// Command
var writeCmd = &cobra.Command{
Use: "write",
Short: "rewrites all config files",
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