Compare commits
4 Commits
fa641b06b2
...
0.2.1-dev.
Author | SHA1 | Date | |
---|---|---|---|
13831390bd | |||
457a6c8742 | |||
b37a283dfb | |||
ee7cb7e422 |
@ -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
|
||||
|
@ -50,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.Value || cmd.CommandPath() == strings.Join([]string{appName, 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
|
||||
|
@ -8,8 +8,12 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
quietFlagShortCode = "q"
|
||||
verboseFlagShortCode = "v"
|
||||
jsonOutputFlagDefault = false
|
||||
noColorFlagDefault = false
|
||||
quietFlagDefault = false
|
||||
quietFlagShortCode = "q"
|
||||
verboseFlagDefault = false
|
||||
verboseFlagShortCode = "v"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -20,19 +24,19 @@ var (
|
||||
)
|
||||
|
||||
func addJsonOutputFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().BoolVarP(&jsonOutputFlag.Value, jsonOutputFlag.Name(), "", false, jsonOutputFlag.Usage())
|
||||
cmd.PersistentFlags().BoolVarP(&jsonOutputFlag.Value, jsonOutputFlag.Name(), "", jsonOutputFlagDefault, jsonOutputFlag.Usage())
|
||||
}
|
||||
|
||||
func addNoColorFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().BoolVarP(&noColorFlag.Value, noColorFlag.Name(), "", false, noColorFlag.Usage())
|
||||
cmd.PersistentFlags().BoolVarP(&noColorFlag.Value, noColorFlag.Name(), "", noColorFlagDefault, noColorFlag.Usage())
|
||||
}
|
||||
|
||||
func addQuietFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().BoolVarP(&quietFlag.Value, quietFlag.Name(), quietFlagShortCode, false, quietFlag.Usage())
|
||||
cmd.PersistentFlags().BoolVarP(&quietFlag.Value, quietFlag.Name(), quietFlagShortCode, quietFlagDefault, quietFlag.Usage())
|
||||
}
|
||||
|
||||
func addVerboseFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().BoolVarP(&verboseFlag.Value, verboseFlag.Name(), verboseFlagShortCode, false, verboseFlag.Usage())
|
||||
cmd.PersistentFlags().BoolVarP(&verboseFlag.Value, verboseFlag.Name(), verboseFlagShortCode, verboseFlagDefault, verboseFlag.Usage())
|
||||
}
|
||||
|
||||
func configureOutputFlags(cmd *cobra.Command) {
|
||||
|
@ -3,6 +3,7 @@ package application
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/Oudwins/zog"
|
||||
"github.com/spf13/cobra"
|
||||
@ -11,19 +12,25 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
versionName = "version"
|
||||
versionFlagName = "version"
|
||||
versionFlagShortCode = "V"
|
||||
versionUsage = "Show version information"
|
||||
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(versionName, zog.Bool(), versionUsage)
|
||||
versionFlag = flagzog.NewBoolFlag(versionFlagName, zog.Bool(), versionFlagUsage)
|
||||
version Version
|
||||
versionCmd = &cobra.Command{
|
||||
Use: versionName,
|
||||
Short: versionUsage,
|
||||
Use: versionFlagName,
|
||||
Short: versionFlagUsage,
|
||||
RunE: versionRunFuncE,
|
||||
}
|
||||
|
||||
regexSemver = regexp.MustCompile(validSemVer)
|
||||
)
|
||||
|
||||
type Version struct {
|
||||
@ -39,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.Value, versionFlag.Name(), versionFlagShortCode, false, versionFlag.Usage())
|
||||
cmd.PersistentFlags().BoolVarP(&versionFlag.Value, versionFlag.Name(), versionFlagShortCode, versionFlagDefault, versionFlag.Usage())
|
||||
}
|
||||
|
||||
func configureVersionFlag(cmd *cobra.Command, v Version) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user