0.2.0-dev #2

Merged
jantytgat merged 9 commits from 0.2.0-dev into main 2025-06-09 20:38:21 +00:00
15 changed files with 304 additions and 102 deletions
Showing only changes of commit ee7cb7e422 - Show all commits

View File

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

View File

@ -3,6 +3,7 @@ package application
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp"
"github.com/Oudwins/zog" "github.com/Oudwins/zog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -11,19 +12,25 @@ import (
) )
const ( const (
versionName = "version" versionFlagName = "version"
versionFlagShortCode = "V" 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 ( var (
versionFlag = flagzog.NewBoolFlag(versionName, zog.Bool(), versionUsage) versionFlag = flagzog.NewBoolFlag(versionFlagName, zog.Bool(), versionFlagUsage)
version Version version Version
versionCmd = &cobra.Command{ versionCmd = &cobra.Command{
Use: versionName, Use: versionFlagName,
Short: versionUsage, Short: versionFlagUsage,
RunE: versionRunFuncE, RunE: versionRunFuncE,
} }
regexSemver = regexp.MustCompile(validSemVer)
) )
type Version struct { type Version struct {
@ -39,8 +46,12 @@ type Version struct {
PreRelease string PreRelease string
} }
func (v Version) IsValid() bool {
return regexSemver.MatchString(v.Full)
}
func addVersionFlag(cmd *cobra.Command) { 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) { func configureVersionFlag(cmd *cobra.Command, v Version) {