0.2.0-dev #2
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -38,9 +39,9 @@ func normalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
||||||
slogd.SetLevel(slogd.Level(logLevelFlag))
|
slogd.SetLevel(slogd.Level(logLevelFlag.Value))
|
||||||
|
|
||||||
if slogd.ActiveHandler() != slogd.HandlerJSON && noColorFlag {
|
if slogd.ActiveHandler() != slogd.HandlerJSON && noColorFlag.Value {
|
||||||
slogd.UseHandler(slogd.HandlerText)
|
slogd.UseHandler(slogd.HandlerText)
|
||||||
cmd.SetContext(slogd.WithContext(cmd.Context()))
|
cmd.SetContext(slogd.WithContext(cmd.Context()))
|
||||||
}
|
}
|
||||||
@ -49,7 +50,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
|||||||
slogd.FromContext(cmd.Context()).Log(cmd.Context(), slogd.LevelTrace, "executing PersistentPreRun")
|
slogd.FromContext(cmd.Context()).Log(cmd.Context(), slogd.LevelTrace, "executing PersistentPreRun")
|
||||||
|
|
||||||
// Make sure we can always get the version
|
// Make sure we can always get the version
|
||||||
if versionFlag || cmd.Use == versionName {
|
if versionFlag.Value || cmd.CommandPath() == strings.Join([]string{appName, versionName}, " ") {
|
||||||
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "overriding command", slog.String("old_function", runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name()), slog.String("new_function", runtime.FuncForPC(reflect.ValueOf(versionRunFuncE).Pointer()).Name()))
|
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "overriding command", slog.String("old_function", runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name()), slog.String("new_function", runtime.FuncForPC(reflect.ValueOf(versionRunFuncE).Pointer()).Name()))
|
||||||
cmd.RunE = versionRunFuncE
|
cmd.RunE = versionRunFuncE
|
||||||
return nil
|
return nil
|
||||||
@ -64,7 +65,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO move to front??
|
// TODO move to front??
|
||||||
if quietFlag {
|
if quietFlag.Value {
|
||||||
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "activating quiet mode")
|
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "activating quiet mode")
|
||||||
outWriter = io.Discard
|
outWriter = io.Discard
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,47 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/Oudwins/zog"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-kit/flagzog"
|
||||||
"git.flexabyte.io/flexabyte/go-kit/slogd"
|
"git.flexabyte.io/flexabyte/go-kit/slogd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LogOutputStdOut = "stdout"
|
logLevelTrace = "trace"
|
||||||
LogOutputStdErr = "stderr"
|
logLevelDebug = "debug"
|
||||||
LogOutputFile = "file"
|
logLevelInfo = "info"
|
||||||
|
logLevelWarn = "warn"
|
||||||
|
logLevelError = "error"
|
||||||
|
logLevelFatal = "fatal"
|
||||||
|
logOutputStdout = "stdout"
|
||||||
|
logOutputStderr = "stderr"
|
||||||
|
logOutputFile = "file"
|
||||||
|
logTypeText = "text"
|
||||||
|
logTypeJson = "json"
|
||||||
|
logTypeColor = "color"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logLevelFlagName string = "log-level"
|
logLevelFlag = flagzog.NewStringFlag("log-level", zog.String().OneOf([]string{logLevelTrace, logLevelDebug, logLevelInfo, logLevelWarn, logLevelError, logLevelFatal}), fmt.Sprintf("Set log level (%s, %s, %s, %s, %s, %s)", logLevelTrace, logLevelDebug, logLevelInfo, logLevelWarn, logLevelError, logLevelFatal))
|
||||||
logLevelFlag string
|
logOutputFlag = flagzog.NewStringFlag("log-output", zog.String().OneOf([]string{logOutputStdout, logOutputStderr, logOutputFile}), fmt.Sprintf("Set log output (%s, %s, %s)", logOutputStdout, logOutputStderr, logOutputFile))
|
||||||
logOutputFlagName string = "log-output"
|
logTypeFlag = flagzog.NewStringFlag("log-type", zog.String().OneOf([]string{logTypeText, logTypeJson, logTypeColor}), fmt.Sprintf("Set log type (%s, %s, %s)", logTypeText, logTypeJson, logTypeColor))
|
||||||
logOutputFlag string
|
|
||||||
logTypeFlagName = "log-type"
|
|
||||||
logTypeFlag string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func addLogLevelFlag(cmd *cobra.Command) {
|
func addLogLevelFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logLevelFlag, logLevelFlagName, "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
cmd.PersistentFlags().StringVarP(&logLevelFlag.Value, logLevelFlag.Name(), "", logLevelInfo, logLevelFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLogOutputFlag(cmd *cobra.Command) {
|
func addLogOutputFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logOutputFlag, logOutputFlagName, "", "stderr", "Set log output (stdout, stderr, file)")
|
cmd.PersistentFlags().StringVarP(&logOutputFlag.Value, logOutputFlag.Name(), "", logOutputStderr, logOutputFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLogTypeFlag(cmd *cobra.Command) {
|
func addLogTypeFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logTypeFlag, logTypeFlagName, "", "text", "Set log type (text, json, color)")
|
cmd.PersistentFlags().StringVarP(&logTypeFlag.Value, logTypeFlag.Name(), "", logTypeText, logTypeFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureLoggingFlags(cmd *cobra.Command) {
|
func configureLoggingFlags(cmd *cobra.Command) {
|
||||||
@ -40,7 +49,7 @@ func configureLoggingFlags(cmd *cobra.Command) {
|
|||||||
addLogOutputFlag(cmd)
|
addLogOutputFlag(cmd)
|
||||||
addLogTypeFlag(cmd)
|
addLogTypeFlag(cmd)
|
||||||
|
|
||||||
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
|
cmd.MarkFlagsMutuallyExclusive("no-color", logTypeFlag.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLogLevelFromArgs(args []string) slog.Level {
|
func GetLogLevelFromArgs(args []string) slog.Level {
|
||||||
|
@ -1,26 +1,38 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import "github.com/spf13/cobra"
|
import (
|
||||||
|
"github.com/Oudwins/zog"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
var jsonOutputFlag bool
|
"git.flexabyte.io/flexabyte/go-kit/flagzog"
|
||||||
var noColorFlag bool
|
)
|
||||||
var quietFlag bool
|
|
||||||
var verboseFlag bool
|
const (
|
||||||
|
quietFlagShortCode = "q"
|
||||||
|
verboseFlagShortCode = "v"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
jsonOutputFlag = flagzog.NewBoolFlag("json", zog.Bool(), "Enable JSON output")
|
||||||
|
noColorFlag = flagzog.NewBoolFlag("no-color", zog.Bool(), "Disable colored output")
|
||||||
|
quietFlag = flagzog.NewBoolFlag("quiet", zog.Bool(), "Suppress output")
|
||||||
|
verboseFlag = flagzog.NewBoolFlag("verbose", zog.Bool(), "Enable verbose output")
|
||||||
|
)
|
||||||
|
|
||||||
func addJsonOutputFlag(cmd *cobra.Command) {
|
func addJsonOutputFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&jsonOutputFlag, "json", "", false, "Enable JSON outWriter")
|
cmd.PersistentFlags().BoolVarP(&jsonOutputFlag.Value, jsonOutputFlag.Name(), "", false, jsonOutputFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func addNoColorFlag(cmd *cobra.Command) {
|
func addNoColorFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&noColorFlag, "no-color", "", false, "Disable color outWriter")
|
cmd.PersistentFlags().BoolVarP(&noColorFlag.Value, noColorFlag.Name(), "", false, noColorFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func addQuietFlag(cmd *cobra.Command) {
|
func addQuietFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Enable quiet mode")
|
cmd.PersistentFlags().BoolVarP(&quietFlag.Value, quietFlag.Name(), quietFlagShortCode, false, quietFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func addVerboseFlag(cmd *cobra.Command) {
|
func addVerboseFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Enable verbose outWriter")
|
cmd.PersistentFlags().BoolVarP(&verboseFlag.Value, verboseFlag.Name(), verboseFlagShortCode, false, verboseFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureOutputFlags(cmd *cobra.Command) {
|
func configureOutputFlags(cmd *cobra.Command) {
|
||||||
@ -29,8 +41,7 @@ func configureOutputFlags(cmd *cobra.Command) {
|
|||||||
addVerboseFlag(cmd)
|
addVerboseFlag(cmd)
|
||||||
addQuietFlag(cmd)
|
addQuietFlag(cmd)
|
||||||
|
|
||||||
cmd.MarkFlagsMutuallyExclusive("verbose", "quiet", "json")
|
cmd.MarkFlagsMutuallyExclusive(verboseFlag.Name(), quietFlag.Name(), jsonOutputFlag.Name())
|
||||||
cmd.MarkFlagsMutuallyExclusive("json", "no-color")
|
cmd.MarkFlagsMutuallyExclusive(jsonOutputFlag.Name(), noColorFlag.Name())
|
||||||
cmd.MarkFlagsMutuallyExclusive("quiet", "no-color")
|
cmd.MarkFlagsMutuallyExclusive(quietFlag.Name(), noColorFlag.Name())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,18 +4,21 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Oudwins/zog"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-kit/flagzog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
versionName = "version"
|
versionName = "version"
|
||||||
versionShortHand = "V"
|
versionFlagShortCode = "V"
|
||||||
versionUsage = "Show version information"
|
versionUsage = "Show version information"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
versionFlag = flagzog.NewBoolFlag(versionName, zog.Bool(), versionUsage)
|
||||||
version Version
|
version Version
|
||||||
versionFlag bool
|
|
||||||
versionCmd = &cobra.Command{
|
versionCmd = &cobra.Command{
|
||||||
Use: versionName,
|
Use: versionName,
|
||||||
Short: versionUsage,
|
Short: versionUsage,
|
||||||
@ -37,7 +40,7 @@ type Version struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addVersionFlag(cmd *cobra.Command) {
|
func addVersionFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
|
cmd.PersistentFlags().BoolVarP(&versionFlag.Value, versionFlag.Name(), versionFlagShortCode, false, versionFlag.Usage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureVersionFlag(cmd *cobra.Command, v Version) {
|
func configureVersionFlag(cmd *cobra.Command, v Version) {
|
||||||
@ -48,11 +51,11 @@ func configureVersionFlag(cmd *cobra.Command, v Version) {
|
|||||||
|
|
||||||
func printVersion(v Version) string {
|
func printVersion(v Version) string {
|
||||||
var output string
|
var output string
|
||||||
if !verboseFlag {
|
if !verboseFlag.Value {
|
||||||
output = v.Full
|
output = v.Full
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonOutputFlag {
|
if jsonOutputFlag.Value {
|
||||||
var b []byte
|
var b []byte
|
||||||
b, _ = json.Marshal(v)
|
b, _ = json.Marshal(v)
|
||||||
output = string(b)
|
output = string(b)
|
||||||
|
Reference in New Issue
Block a user