Add version flag

This commit is contained in:
Daniel 2024-10-17 11:28:43 -04:00
parent 0407672a23
commit c1e3eece4d
7 changed files with 50 additions and 19 deletions

View file

@ -1,5 +1,8 @@
VER!=git describe --tags --always
HASH!=git describe --always
LDFLAGS=-ldflags="-X 'main.Version=${VER}' -X 'main.Commit=${HASH}'"
all: checkcalls
go build -o gordio ./cmd/gordio/
go build -o gordio ${LDFLAGS} ./cmd/gordio/
go build -o calls ./cmd/calls/
clean:

View file

@ -1,7 +1,9 @@
package main
import (
"fmt"
"os"
"runtime"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@ -13,15 +15,30 @@ import (
"github.com/spf13/cobra"
)
var (
Version = "unset"
Commit = "unset"
)
func version() {
fmt.Printf("gordio %s (%s)\nbuilt for %s-%s\n",
Version, Commit, runtime.GOOS, runtime.GOARCH)
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
rootCmd := &cobra.Command{
Use: gordio.AppName,
}
rootCmd.PersistentFlags().BoolP("version", "V", false, "show version")
cfg := config.New(rootCmd)
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
return cfg.ReadConfig()
rootCmd.Run = func(cmd *cobra.Command, args []string) {
v, _ := rootCmd.PersistentFlags().GetBool("version")
if v {
version()
os.Exit(0)
}
}
cmds := append([]*cobra.Command{gordio.Command(cfg)}, admin.Command(cfg)...)

View file

@ -120,6 +120,7 @@ func Command(cfg *config.Config) []*cobra.Command {
Use: "users",
Aliases: []string{"u"},
Short: "administers the server",
PersistentPreRunE: cfg.PreRunE(),
}
userCmd.AddCommand(addUserCommand(cfg), passwdCommand(cfg))

View file

@ -39,9 +39,17 @@ type Logger struct {
Level *string `yaml:"level"`
}
func New(cmd *cobra.Command) *Config {
func (c *Config) PreRunE() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
return c.ReadConfig()
}
}
func New(rootCommand *cobra.Command) *Config {
c := &Config{}
cmd.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
rootCommand.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
return c
}

View file

@ -19,6 +19,7 @@ func Command(cfg *config.Config) *cobra.Command {
serveCmd := &cobra.Command{
Use: "serve",
Short: "starts the" + AppName + " server",
PersistentPreRunE: cfg.PreRunE(),
RunE: common.RunE(opts),
}

View file

@ -27,6 +27,7 @@ func NewLogger(cfg *config.Config) (*Logger, error) {
l := &Logger{
console: &zerolog.ConsoleWriter{Out: os.Stderr},
}
l.hup = make(chan os.Signal, 1)
go func() {
for sig := range l.hup {