8 Commits

Author SHA1 Message Date
13831390bd Add function "AddToCommandFlags" to FlagValidator interface
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 33s
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-12 17:01:05 +02:00
457a6c8742 Merge pull request '0.2.0-dev' (#2) from 0.2.0-dev into main
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 11m2s
Reviewed-on: #2
2025-06-09 20:38:18 +00:00
b37a283dfb Refactor flags
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-09 22:36:45 +02:00
ee7cb7e422 Remove dependency on semver package
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-09 22:36:32 +02:00
fa641b06b2 Refactor flags to use flagzog package
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 35s
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-09 22:18:47 +02:00
9adebbf0e2 Add go.mod for simple example
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-09 22:18:25 +02:00
c2b17924e2 Add go.mod for simple example
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-09 22:18:07 +02:00
e63b41c5ff Update dependencies
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
2025-06-09 21:39:26 +02:00
11 changed files with 145 additions and 49 deletions

View File

@ -8,8 +8,6 @@ import (
"time"
"github.com/spf13/cobra"
"git.flexabyte.io/flexabyte/go-kit/semver"
)
type Config struct {
@ -99,8 +97,7 @@ func (c Config) Validate() error {
return errors.New("logger is required")
}
var err error
if _, err = semver.Parse(c.Version.Full); err != nil {
if !c.Version.IsValid() {
return fmt.Errorf("invalid version: %s", c.Version)
}
return nil

View File

@ -7,6 +7,7 @@ import (
"os"
"reflect"
"runtime"
"strings"
"syscall"
"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 {
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)
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")
// Make sure we can always get the version
if versionFlag || cmd.Use == versionName {
if versionFlag.Value || cmd.CommandPath() == strings.Join([]string{appName, versionFlagName}, " ") {
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
return nil
@ -64,7 +65,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
}
// TODO move to front??
if quietFlag {
if quietFlag.Value {
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "activating quiet mode")
outWriter = io.Discard
}

View File

@ -1,38 +1,47 @@
package application
import (
"fmt"
"log/slog"
"github.com/Oudwins/zog"
"github.com/spf13/cobra"
"git.flexabyte.io/flexabyte/go-kit/flagzog"
"git.flexabyte.io/flexabyte/go-kit/slogd"
)
const (
LogOutputStdOut = "stdout"
LogOutputStdErr = "stderr"
LogOutputFile = "file"
logLevelTrace = "trace"
logLevelDebug = "debug"
logLevelInfo = "info"
logLevelWarn = "warn"
logLevelError = "error"
logLevelFatal = "fatal"
logOutputStdout = "stdout"
logOutputStderr = "stderr"
logOutputFile = "file"
logTypeText = "text"
logTypeJson = "json"
logTypeColor = "color"
)
var (
logLevelFlagName string = "log-level"
logLevelFlag string
logOutputFlagName string = "log-output"
logOutputFlag string
logTypeFlagName = "log-type"
logTypeFlag string
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))
logOutputFlag = flagzog.NewStringFlag("log-output", zog.String().OneOf([]string{logOutputStdout, logOutputStderr, logOutputFile}), fmt.Sprintf("Set log output (%s, %s, %s)", logOutputStdout, logOutputStderr, logOutputFile))
logTypeFlag = flagzog.NewStringFlag("log-type", zog.String().OneOf([]string{logTypeText, logTypeJson, logTypeColor}), fmt.Sprintf("Set log type (%s, %s, %s)", logTypeText, logTypeJson, logTypeColor))
)
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) {
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) {
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) {
@ -40,7 +49,7 @@ func configureLoggingFlags(cmd *cobra.Command) {
addLogOutputFlag(cmd)
addLogTypeFlag(cmd)
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
cmd.MarkFlagsMutuallyExclusive("no-color", logTypeFlag.Name())
}
func GetLogLevelFromArgs(args []string) slog.Level {

View File

@ -1,26 +1,42 @@
package application
import "github.com/spf13/cobra"
import (
"github.com/Oudwins/zog"
"github.com/spf13/cobra"
var jsonOutputFlag bool
var noColorFlag bool
var quietFlag bool
var verboseFlag bool
"git.flexabyte.io/flexabyte/go-kit/flagzog"
)
const (
jsonOutputFlagDefault = false
noColorFlagDefault = false
quietFlagDefault = false
quietFlagShortCode = "q"
verboseFlagDefault = false
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) {
cmd.PersistentFlags().BoolVarP(&jsonOutputFlag, "json", "", false, "Enable JSON outWriter")
cmd.PersistentFlags().BoolVarP(&jsonOutputFlag.Value, jsonOutputFlag.Name(), "", jsonOutputFlagDefault, jsonOutputFlag.Usage())
}
func addNoColorFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&noColorFlag, "no-color", "", false, "Disable color outWriter")
cmd.PersistentFlags().BoolVarP(&noColorFlag.Value, noColorFlag.Name(), "", noColorFlagDefault, noColorFlag.Usage())
}
func addQuietFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Enable quiet mode")
cmd.PersistentFlags().BoolVarP(&quietFlag.Value, quietFlag.Name(), quietFlagShortCode, quietFlagDefault, quietFlag.Usage())
}
func addVerboseFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Enable verbose outWriter")
cmd.PersistentFlags().BoolVarP(&verboseFlag.Value, verboseFlag.Name(), verboseFlagShortCode, verboseFlagDefault, verboseFlag.Usage())
}
func configureOutputFlags(cmd *cobra.Command) {
@ -29,8 +45,7 @@ func configureOutputFlags(cmd *cobra.Command) {
addVerboseFlag(cmd)
addQuietFlag(cmd)
cmd.MarkFlagsMutuallyExclusive("verbose", "quiet", "json")
cmd.MarkFlagsMutuallyExclusive("json", "no-color")
cmd.MarkFlagsMutuallyExclusive("quiet", "no-color")
cmd.MarkFlagsMutuallyExclusive(verboseFlag.Name(), quietFlag.Name(), jsonOutputFlag.Name())
cmd.MarkFlagsMutuallyExclusive(jsonOutputFlag.Name(), noColorFlag.Name())
cmd.MarkFlagsMutuallyExclusive(quietFlag.Name(), noColorFlag.Name())
}

View File

@ -3,24 +3,34 @@ package application
import (
"encoding/json"
"fmt"
"regexp"
"github.com/Oudwins/zog"
"github.com/spf13/cobra"
"git.flexabyte.io/flexabyte/go-kit/flagzog"
)
const (
versionName = "version"
versionShortHand = "V"
versionUsage = "Show version information"
versionFlagName = "version"
versionFlagShortCode = "V"
versionFlagUsage = "Show version information"
versionFlagDefault = false
// https://semver.org/ && https://regex101.com/r/Ly7O1x/3/
validSemVer = `^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`
)
var (
versionFlag = flagzog.NewBoolFlag(versionFlagName, zog.Bool(), versionFlagUsage)
version Version
versionFlag bool
versionCmd = &cobra.Command{
Use: versionName,
Short: versionUsage,
Use: versionFlagName,
Short: versionFlagUsage,
RunE: versionRunFuncE,
}
regexSemver = regexp.MustCompile(validSemVer)
)
type Version struct {
@ -36,8 +46,12 @@ type Version struct {
PreRelease string
}
func (v Version) IsValid() bool {
return regexSemver.MatchString(v.Full)
}
func addVersionFlag(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
cmd.PersistentFlags().BoolVarP(&versionFlag.Value, versionFlag.Name(), versionFlagShortCode, versionFlagDefault, versionFlag.Usage())
}
func configureVersionFlag(cmd *cobra.Command, v Version) {
@ -48,11 +62,11 @@ func configureVersionFlag(cmd *cobra.Command, v Version) {
func printVersion(v Version) string {
var output string
if !verboseFlag {
if !verboseFlag.Value {
output = v.Full
}
if jsonOutputFlag {
if jsonOutputFlag.Value {
var b []byte
b, _ = json.Marshal(v)
output = string(b)

View File

@ -0,0 +1,19 @@
module simple
go 1.24.2
toolchain go1.24.4
require (
git.flexabyte.io/flexabyte/go-kit v0.1.1-0.20250609193926-e63b41c5ff45
github.com/spf13/cobra v1.9.1
)
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/samber/lo v1.50.0 // indirect
github.com/samber/slog-formatter v1.2.0 // indirect
github.com/samber/slog-multi v1.4.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
golang.org/x/text v0.26.0 // indirect
)

View File

@ -0,0 +1,27 @@
git.flexabyte.io/flexabyte/go-kit v0.1.1-0.20250609193926-e63b41c5ff45 h1:VA4K969jlxvppt+WYHIMwTt5TnE9zgEynMu1reTFujQ=
git.flexabyte.io/flexabyte/go-kit v0.1.1-0.20250609193926-e63b41c5ff45/go.mod h1:GTo5se9ocEJfsdlpClpZMD8+eXsHdD5it35IILezNUc=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.50.0 h1:XrG0xOeHs+4FQ8gJR97zDz5uOFMW7OwFWiFVzqopKgY=
github.com/samber/lo v1.50.0/go.mod h1:RjZyNk6WSnUFRKK6EyOhsRJMqft3G+pg7dCWHQCWvsc=
github.com/samber/slog-formatter v1.2.0 h1:gTSHm4CxyySyhcxRkzk21CSKbGCdZVipbRMhINkNtQU=
github.com/samber/slog-formatter v1.2.0/go.mod h1:hgjhSd5Vf69XCOnVp0UW0QHCxJ8iDEm/qASjji6FNoI=
github.com/samber/slog-multi v1.4.0 h1:pwlPMIE7PrbTHQyKWDU+RIoxP1+HKTNOujk3/kdkbdg=
github.com/samber/slog-multi v1.4.0/go.mod h1:FsQ4Uv2L+E/8TZt+/BVgYZ1LoDWCbfCU21wVIoMMrO8=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/Oudwins/zog"
"github.com/spf13/pflag"
)
func NewBoolFlag(name string, schema *zog.BoolSchema[bool], usage string) BoolFlag {
@ -40,6 +41,10 @@ func (f BoolFlag) Validate() ([]string, error) {
return messages, nil
}
func (f BoolFlag) AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{}) {
flagset.BoolVarP(&f.Value, f.Name(), shorthand, value.(bool), f.usage)
}
func NewInt64Flag(name string, schema *zog.NumberSchema[int64], usage string) Int64Flag {
return Int64Flag{
name: name,
@ -74,6 +79,10 @@ func (f Int64Flag) Validate() ([]string, error) {
return messages, nil
}
func (f Int64Flag) AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{}) {
flagset.Int64VarP(&f.Value, f.Name(), shorthand, value.(int64), f.usage)
}
func NewStringFlag(name string, schema *zog.StringSchema[string], usage string) StringFlag {
return StringFlag{
name: name,
@ -107,3 +116,7 @@ func (f StringFlag) Validate() ([]string, error) {
}
return messages, nil
}
func (f StringFlag) AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{}) {
flagset.StringVarP(&f.Value, f.Name(), shorthand, value.(string), f.usage)
}

View File

@ -3,12 +3,15 @@ package flagzog
import (
"context"
"log/slog"
"github.com/spf13/pflag"
)
type FlagValidator interface {
Name() string
Validate() ([]string, error)
Usage() string
AddToCommandFlags(flagset *pflag.FlagSet, shorthand string, value interface{})
}
func ValidateFlags(ctx context.Context, logger *slog.Logger, flags []FlagValidator) ([]string, error) {

4
go.mod
View File

@ -3,6 +3,7 @@ module git.flexabyte.io/flexabyte/go-kit
go 1.24.2
require (
github.com/Oudwins/zog v0.21.1
github.com/samber/slog-formatter v1.2.0
github.com/samber/slog-multi v1.4.0
github.com/spf13/cobra v1.9.1
@ -10,9 +11,8 @@ require (
)
require (
github.com/Oudwins/zog v0.21.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/samber/lo v1.50.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 // indirect
golang.org/x/text v0.26.0 // indirect
)

6
go.sum
View File

@ -22,10 +22,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 h1:bsqhLWFR6G6xiQcb+JoGqdKdRU6WzPWmK8E0jxTjzo4=
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=