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

@ -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))

View file

@ -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
}

View file

@ -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

View file

@ -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 {

View file

@ -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,
}