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 all: checkcalls
go build -o gordio ./cmd/gordio/ go build -o gordio ${LDFLAGS} ./cmd/gordio/
go build -o calls ./cmd/calls/ go build -o calls ./cmd/calls/
clean: clean:

View file

@ -1,7 +1,9 @@
package main package main
import ( import (
"fmt"
"os" "os"
"runtime"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -13,15 +15,30 @@ import (
"github.com/spf13/cobra" "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() { func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: gordio.AppName, Use: gordio.AppName,
} }
rootCmd.PersistentFlags().BoolP("version", "V", false, "show version")
cfg := config.New(rootCmd) cfg := config.New(rootCmd)
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { rootCmd.Run = func(cmd *cobra.Command, args []string) {
return cfg.ReadConfig() v, _ := rootCmd.PersistentFlags().GetBool("version")
if v {
version()
os.Exit(0)
}
} }
cmds := append([]*cobra.Command{gordio.Command(cfg)}, admin.Command(cfg)...) cmds := append([]*cobra.Command{gordio.Command(cfg)}, admin.Command(cfg)...)

View file

@ -117,9 +117,10 @@ func readPassword(prompt string) (string, error) {
// Command is the users command. // Command is the users command.
func Command(cfg *config.Config) []*cobra.Command { func Command(cfg *config.Config) []*cobra.Command {
userCmd := &cobra.Command{ userCmd := &cobra.Command{
Use: "users", Use: "users",
Aliases: []string{"u"}, Aliases: []string{"u"},
Short: "administers the server", Short: "administers the server",
PersistentPreRunE: cfg.PreRunE(),
} }
userCmd.AddCommand(addUserCommand(cfg), passwdCommand(cfg)) userCmd.AddCommand(addUserCommand(cfg), passwdCommand(cfg))

View file

@ -35,13 +35,21 @@ type DB struct {
} }
type Logger struct { type Logger struct {
File *string `yaml:"file"` File *string `yaml:"file"`
Level *string `yaml:"level"` 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{} 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 return c
} }

View file

@ -17,9 +17,10 @@ type ServeOptions struct {
func Command(cfg *config.Config) *cobra.Command { func Command(cfg *config.Config) *cobra.Command {
opts := makeOptions(cfg) opts := makeOptions(cfg)
serveCmd := &cobra.Command{ serveCmd := &cobra.Command{
Use: "serve", Use: "serve",
Short: "starts the" + AppName + " server", Short: "starts the" + AppName + " server",
RunE: common.RunE(opts), PersistentPreRunE: cfg.PreRunE(),
RunE: common.RunE(opts),
} }
return serveCmd return serveCmd

View file

@ -20,13 +20,14 @@ const (
type Logger struct { type Logger struct {
console io.Writer console io.Writer
writers []io.Writer writers []io.Writer
hup chan os.Signal hup chan os.Signal
} }
func NewLogger(cfg *config.Config) (*Logger, error) { func NewLogger(cfg *config.Config) (*Logger, error) {
l := &Logger{ l := &Logger{
console: &zerolog.ConsoleWriter{Out: os.Stderr}, console: &zerolog.ConsoleWriter{Out: os.Stderr},
} }
l.hup = make(chan os.Signal, 1) l.hup = make(chan os.Signal, 1)
go func() { go func() {
for sig := range l.hup { for sig := range l.hup {

View file

@ -40,11 +40,11 @@ func New(cfg *config.Config) (*Server, error) {
r := chi.NewRouter() r := chi.NewRouter()
authenticator := auth.NewAuthenticator(cfg.Auth) authenticator := auth.NewAuthenticator(cfg.Auth)
srv := &Server{ srv := &Server{
auth: authenticator, auth: authenticator,
conf: cfg, conf: cfg,
db: db, db: db,
r: r, r: r,
nex: nexus.New(), nex: nexus.New(),
logger: logger, logger: logger,
} }