refactor: switch flag-parsing library
4 files changed, 45 insertions(+), 40 deletions(-)
changed files
M cmd/searchix-web/main.go → cmd/searchix-web/main.go
@@ -3,14 +3,13 @@ import ( "context" "errors" - "flag" "fmt" "os" "os/signal" "runtime/pprof" "sync" - "badc0de.net/pkg/flagutil" + flags "github.com/jessevdk/go-flags" "alin.ovh/searchix/frontend" "alin.ovh/searchix/internal/components"@@ -25,40 +24,46 @@ "alin.ovh/searchix/web" "alin.ovh/x/log" ) -var ( - configFile = flag.String("config", "config.toml", "config `file` to use") - printDefaultConfig = flag.Bool( - "print-default-config", - false, - "print default configuration and exit", - ) - generateErrorPage = flag.Bool("generate-error-page", false, "generate error page and exit") - dev = flag.Bool("dev", false, "enable live reloading and nicer logging") - prefetch = flag.Bool("fetch", false, "pre-fetch data and exit") - replace = flag.Bool("replace", false, "replace existing storage and exit") - reindex = flag.Bool("reindex", false, "reindex existing index and exit") - offline = flag.Bool("offline", false, "run in offline mode") - version = flag.Bool("version", false, "print version information") - cpuprofile = flag.String("cpuprofile", "", "enable CPU profiling and save to `file`") -) +var options struct { + Config flags.Filename `long:"config" default:"config.toml" description:"config file to use"` + PrintDefaultConfig bool `long:"print-default-config" description:"print default configuration and exit"` + GenerateErrorPage bool `long:"generate-error-page" description:"generate error page and exit"` + Dev bool `long:"dev" description:"enable live reloading and nicer logging"` + Prefetch bool `long:"prefetch" description:"pre-fetch data and exit"` + Replace bool `long:"replace" description:"replace existing storage and exit"` + Reindex bool `long:"reindex" description:"reindex existing index and exit"` + Rebuild bool `long:"rebuild" description:"rebuild existing index and exit"` + Offline bool `long:"offline" description:"run in offline mode"` + Version bool `long:"version" description:"print version information"` + CPUProfile flags.Filename `long:"cpuprofile" description:"output CPU profile to FILE" value-name:"FILE"` +} + +var parser = flags.NewParser(&options, flags.Default) func main() { - flagutil.Parse() - if *version { + _, err := parser.Parse() + if err != nil { + os.Exit(1) + } + if flags.WroteHelp(err) { + return + } + + if options.Version { _, err := fmt.Fprintf(os.Stderr, "searchix %s\n", config.Version) if err != nil { panic("can't write to standard error?!") } os.Exit(0) } - if *printDefaultConfig { + if options.PrintDefaultConfig { _, err := fmt.Print(config.GetDefaultConfig()) if err != nil { panic("can't write to standard output?!") } os.Exit(0) } - if *generateErrorPage { + if options.GenerateErrorPage { assets, err := frontend.New() if err != nil { panic("failed to create assets: " + err.Error())@@ -79,9 +84,9 @@ } os.Exit(0) } - if *cpuprofile != "" { + if options.CPUProfile != "" { //nolint:forbidigo // admin specifies profile file location - f, err := os.Create(*cpuprofile) + f, err := os.Create(string(options.CPUProfile)) if err != nil { panic("can't create CPU profile: " + err.Error()) }@@ -92,9 +97,9 @@ } defer pprof.StopCPUProfile() } - logger := log.Configure(!*dev) + logger := log.Configure(!options.Dev) - cfg, err := config.GetConfig(*configFile, logger) + cfg, err := config.GetConfig(string(options.Config), logger) if err != nil { logger.Fatal("Failed to parse config file", "error", err) }@@ -122,7 +127,7 @@ read, write, exists, err := index.OpenOrCreate( &index.Options{ Config: cfg, - Force: *reindex, + Force: options.Reindex, LowMemory: cfg.Importer.LowMemory, BatchSize: cfg.Importer.BatchSize, Logger: logger.Named("index"),@@ -155,24 +160,24 @@ LowMemory: cfg.Importer.LowMemory, Logger: logger.Named("importer"), Manpages: mdb, Root: root, - Offline: *offline, + Offline: options.Offline, }) if err != nil { logger.Fatal("Failed to create importer", "error", err) } - if !exists || *replace || *prefetch { - err := imp.Start(ctx, true, *prefetch, nil) + if !exists || options.Replace || options.Prefetch { + err := imp.Start(ctx, true, options.Prefetch, nil) if err != nil { logger.Fatal("Failed to start importer", "error", err) } - if *replace || *prefetch { + if options.Replace || options.Prefetch { return } } - if !exists || *reindex { + if !exists || options.Reindex { for _, source := range cfg.Importer.Sources { hadErrors, err := importer.ImportSource( ctx,@@ -190,7 +195,7 @@ logger.Warn("Imported source encountered errors", "source", source.Name) } } - if *reindex { + if options.Reindex { return } }@@ -206,7 +211,7 @@ go func() { sCtx, cancel := context.WithCancel(ctx) defer wg.Done() defer cancel() - err := s.Start(sCtx, *dev) + err := s.Start(sCtx, options.Dev) if err != nil && !errors.Is(err, context.Canceled) { // Error starting or closing listener: logger.Fatal("error", "error", err)
M go.mod → go.mod
@@ -5,7 +5,6 @@ require ( alin.ovh/gomponents v1.6.0 alin.ovh/x v1.0.0 - badc0de.net/pkg/flagutil v1.0.1 github.com/Southclaws/fault v0.8.2 github.com/andybalholm/brotli v1.1.1 github.com/asdine/storm/v3 v3.2.1@@ -16,6 +15,7 @@ github.com/crewjam/csp v0.0.2 github.com/dustin/go-humanize v1.0.1 github.com/fsnotify/fsnotify v1.9.0 github.com/getsentry/sentry-go v0.33.0 + github.com/jessevdk/go-flags v1.6.1 github.com/mitchellh/mapstructure v1.5.0 github.com/osdevisnot/sorvor v0.4.4 github.com/pelletier/go-toml/v2 v2.2.4
M go.sum → go.sum
@@ -2,8 +2,6 @@ alin.ovh/gomponents v1.6.0 h1:ymFYykOwMSG63dQ8bQZ4U0611zkDEzVNMOpKQSwwFjE= alin.ovh/gomponents v1.6.0/go.mod h1:RQtPwKzGB0Nt1JTBDSqRzH2w/mg/6PkuhnNvQMV0/AQ= alin.ovh/x v1.0.0 h1:ePw0pO8YPLxKKJyxoF1vh2ZedE+qKkzzCQRxgaLsrc8= alin.ovh/x v1.0.0/go.mod h1:ivPzLVkiYeXGmUITqx4KLoNikO1URKJXYPPo4yCxuhY= -badc0de.net/pkg/flagutil v1.0.1 h1:0ZgBzd3FehDUA8DJ70/phsnDH61/3aYMyx8Wd84KqQo= -badc0de.net/pkg/flagutil v1.0.1/go.mod h1:HwwkfbImu+u288bnLaYDGqBxkJzvqi5YzKofmgkMLvk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Code-Hex/dd v1.1.0 h1:VEtTThnS9l7WhpKUIpdcWaf0B8Vp0LeeSEsxA1DZseI= github.com/Code-Hex/dd v1.1.0/go.mod h1:VaMyo/YjTJ3d4qm/bgtrUkT2w+aYwJ07Y7eCWyrJr1w=@@ -92,6 +90,8 @@ github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4= +github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
M gomod2nix.toml → gomod2nix.toml
@@ -7,9 +7,6 @@ hash = "sha256-vJLon9M6x/akEnQu+vK0RuzVyy/9vOnWGzp5DSS6brU=" [mod."alin.ovh/x"] version = "v1.0.0" hash = "sha256-9VbqLrFz3dy34Uo62JnDQyVp50wpsoq/pJrbAvbiuM0=" - [mod."badc0de.net/pkg/flagutil"] - version = "v1.0.1" - hash = "sha256-0LRWL5DUHW3gXQhPAhUCxnUCN7HN1qKI2yZp8MrDN6M=" [mod."github.com/Code-Hex/dd"] version = "v1.1.0" hash = "sha256-9aoekzjMXuJmR0/7bfu4y3SfcWBgdfYybB7gt4sNKfk="@@ -109,6 +106,9 @@ hash = "sha256-o/S91mX4ysjuG/oRHLs8qSyfb1Risf/YzMuW5u7d3U4=" [mod."github.com/google/uuid"] version = "v1.6.0" hash = "sha256-VWl9sqUzdOuhW0KzQlv0gwwUQClYkmZwSydHG2sALYw=" + [mod."github.com/jessevdk/go-flags"] + version = "v1.6.1" + hash = "sha256-Q5WFTgRxYio0+ay3sbQeBPKeJAFvOdiDVkaTVn3hoTA=" [mod."github.com/json-iterator/go"] version = "v1.1.12" hash = "sha256-To8A0h+lbfZ/6zM+2PpRpY3+L6725OPC66lffq6fUoM="