Skip to content

Commit

Permalink
make draconctl able to log JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole authored and ptzianos committed Aug 5, 2024
1 parent 1286a5c commit d8262d6
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions cmd/draconctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"log"
"log/slog"
"os"
"slices"
"strings"

"github.com/go-errors/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -16,14 +17,18 @@ import (
"github.com/ocurity/dracon/cmd/draconctl/pipelines"
)

var rootCmd = &cobra.Command{
Use: "draconctl",
Short: "A CLI to manage all things related to Dracon",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// You can bind cobra and viper in a few locations, but PersistencePreRunE on the root command works well
return initializeConfig(cmd)
},
}
var (
rootCmd = &cobra.Command{
Use: "draconctl",
Short: "A CLI to manage all things related to Dracon",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// You can bind cobra and viper in a few locations, but PersistencePreRunE on the root command works well
return initializeConfig(cmd)
},
}
loggingFormat string
supportedLogFormats = []string{"text", "json"}
)

func main() {
rootCmd.AddGroup(&cobra.Group{
Expand All @@ -40,6 +45,10 @@ func main() {
}
}

func init() {
rootCmd.PersistentFlags().StringVar(&loggingFormat, "log-format", "text", "The log format to use (supports 'text' for plain text and 'json' for JSON)")
}

func initializeConfig(cmd *cobra.Command) error {
v := viper.New()

Expand All @@ -58,13 +67,19 @@ func initializeConfig(cmd *cobra.Command) error {
// like --favorite-color which we fix in the bindFlags function
v.AutomaticEnv()

if !slices.Contains(supportedLogFormats, strings.ToLower(loggingFormat)) {
return errors.Errorf("unsupported log format %s, supported formats are %s", loggingFormat, supportedLogFormats)
} else if strings.ToLower(loggingFormat) == "json" {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{})))
}

// Bind the current command's flags to viper
cmd.Flags().VisitAll(func(f *pflag.Flag) {
// Apply the viper config value to the flag when the flag is not set and viper has a value
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
log.Println(err)
slog.Error(err.Error())
}
}
})
Expand Down

0 comments on commit d8262d6

Please sign in to comment.