repo-sync/flags.go
James Coleman 14cf482549
Some checks are pending
Go package / build (push) Waiting to run
first commit
2026-07-27 14:16:32 -05:00

72 lines
2.1 KiB
Go

package main
import (
"fmt"
"runtime/debug"
"github.com/alecthomas/kong"
cfg "github.com/grmrgecko/repo-sync/config"
)
// VersionFlag prints build information and exits.
type VersionFlag bool
// Decode satisfies kong.MapperValue. The flag is treated as a boolean toggle.
func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
// IsBool reports the flag as a boolean for kong's parser.
func (v VersionFlag) IsBool() bool { return true }
// BeforeApply emits version information then exits before the rest of the
// command is executed.
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
fmt.Printf("%s: %s (%s)\n", cfg.Name, cfg.Version, cfg.Mode)
if cfg.Commit != "" {
fmt.Printf(" commit: %s\n", cfg.Commit)
}
if cfg.Date != "" {
fmt.Printf(" built: %s\n", cfg.Date)
}
if cfg.Commit == "" {
if bi, ok := debug.ReadBuildInfo(); ok {
for _, s := range bi.Settings {
switch s.Key {
case "vcs.revision":
fmt.Printf(" commit: %s\n", s.Value)
case "vcs.time":
fmt.Printf(" built: %s\n", s.Value)
}
}
}
}
app.Exit(0)
return nil
}
// Flags is the root command line configuration.
type Flags struct {
LogLevel string `help:"Log level (debug, info, warn, error)." enum:"debug,info,warn,error" default:"info"`
Version VersionFlag `name:"version" help:"Print version information and quit"`
RPM RPMCmd `cmd:"" name:"rpm" help:"Synchronize RPM (yum/dnf) repositories."`
Deb DebCmd `cmd:"" name:"deb" help:"Synchronize DEB (apt) repositories."`
Arch ArchCmd `cmd:"" name:"arch" help:"Synchronize Arch Linux (pacman) repositories."`
Apk ApkCmd `cmd:"" name:"apk" help:"Synchronize Alpine Linux (apk) repositories."`
Server ServerCmd `cmd:"" name:"server" help:"Run the caching mirror server."`
}
// flags holds the parsed command line configuration.
var flags Flags
// ParseFlags parses the command line, exiting with usage information on
// error.
func ParseFlags() *kong.Context {
return kong.Parse(&flags,
kong.Name(cfg.Name),
kong.Description(cfg.Description),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
)
}