repo-sync/flags.go
James Coleman 6782f33778
Some checks are pending
Go package / build (push) Waiting to run
first commit
2026-07-27 15:24:18 -05:00

84 lines
2.8 KiB
Go

package main
import (
"fmt"
"runtime/debug"
"strings"
"time"
"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. The global flags override
// the configuration file for every command, so a sync run and the server
// honor the same settings; each is empty when unset so the configured
// value stands.
type Flags struct {
ConfigPath string `help:"The path to the config file." optional:"" type:"existingfile"`
LogLevel string `help:"Override the configured log level." enum:",debug,info,warn,error" default:""`
UserAgent string `help:"Override the configured user agent sent to upstreams." optional:""`
RequestTimeout time.Duration `help:"Override the configured upstream response timeout (e.g. 2m)." optional:""`
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."`
Service ServiceCmd `cmd:"" name:"service" help:"Manage the mirror server system service."`
}
// 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,
}),
kong.Vars{
"serviceActions": strings.Join(ServiceAction, ","),
},
)
}