From c1e3eece4d80aa0d35365c29ad4619a7cff9c2a4 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Thu, 17 Oct 2024 11:28:43 -0400 Subject: [PATCH] Add version flag --- Makefile | 5 ++++- cmd/gordio/main.go | 21 +++++++++++++++++++-- pkg/gordio/admin/admin.go | 7 ++++--- pkg/gordio/config/config.go | 16 ++++++++++++---- pkg/gordio/gordio.go | 7 ++++--- pkg/gordio/server/logging.go | 3 ++- pkg/gordio/server/server.go | 10 +++++----- 7 files changed, 50 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index a0ff802..9bb10ab 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/cmd/gordio/main.go b/cmd/gordio/main.go index 7521331..bcb2758 100644 --- a/cmd/gordio/main.go +++ b/cmd/gordio/main.go @@ -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)...) diff --git a/pkg/gordio/admin/admin.go b/pkg/gordio/admin/admin.go index f81f7e4..04255cb 100644 --- a/pkg/gordio/admin/admin.go +++ b/pkg/gordio/admin/admin.go @@ -117,9 +117,10 @@ func readPassword(prompt string) (string, error) { // Command is the users command. func Command(cfg *config.Config) []*cobra.Command { userCmd := &cobra.Command{ - Use: "users", - Aliases: []string{"u"}, - Short: "administers the server", + Use: "users", + Aliases: []string{"u"}, + Short: "administers the server", + PersistentPreRunE: cfg.PreRunE(), } userCmd.AddCommand(addUserCommand(cfg), passwdCommand(cfg)) diff --git a/pkg/gordio/config/config.go b/pkg/gordio/config/config.go index a33c4ef..e7c758e 100644 --- a/pkg/gordio/config/config.go +++ b/pkg/gordio/config/config.go @@ -35,13 +35,21 @@ type DB struct { } type Logger struct { - File *string `yaml:"file"` - Level *string `yaml:"level"` + File *string `yaml:"file"` + 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 } diff --git a/pkg/gordio/gordio.go b/pkg/gordio/gordio.go index 693c395..de7837d 100644 --- a/pkg/gordio/gordio.go +++ b/pkg/gordio/gordio.go @@ -17,9 +17,10 @@ type ServeOptions struct { func Command(cfg *config.Config) *cobra.Command { opts := makeOptions(cfg) serveCmd := &cobra.Command{ - Use: "serve", - Short: "starts the" + AppName + " server", - RunE: common.RunE(opts), + Use: "serve", + Short: "starts the" + AppName + " server", + PersistentPreRunE: cfg.PreRunE(), + RunE: common.RunE(opts), } return serveCmd diff --git a/pkg/gordio/server/logging.go b/pkg/gordio/server/logging.go index 03d9930..9ff4c4f 100644 --- a/pkg/gordio/server/logging.go +++ b/pkg/gordio/server/logging.go @@ -20,13 +20,14 @@ const ( type Logger struct { console io.Writer writers []io.Writer - hup chan os.Signal + hup chan os.Signal } 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 { diff --git a/pkg/gordio/server/server.go b/pkg/gordio/server/server.go index aace9c0..0b870b0 100644 --- a/pkg/gordio/server/server.go +++ b/pkg/gordio/server/server.go @@ -40,11 +40,11 @@ func New(cfg *config.Config) (*Server, error) { r := chi.NewRouter() authenticator := auth.NewAuthenticator(cfg.Auth) srv := &Server{ - auth: authenticator, - conf: cfg, - db: db, - r: r, - nex: nexus.New(), + auth: authenticator, + conf: cfg, + db: db, + r: r, + nex: nexus.New(), logger: logger, }