From 9b665d2df6e475834105d2cc9b111ddc105b5844 Mon Sep 17 00:00:00 2001 From: Justin Harms Date: Sun, 31 May 2026 17:04:39 -0500 Subject: [PATCH] Switched to using cobra for better cli support --- Makefile | 2 +- cmd/pastebin/main.go | 38 ++++++++++++++++++++++++++++++-------- cmd/pastebin/main_test.go | 11 ++++------- go.mod | 6 ++++++ go.sum | 8 ++++++++ 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 1378592..6451e72 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ help: @echo " make clean - Remove built artifacts" run: - go run $(CMD_DIR) -config $(CONFIG) + go run $(CMD_DIR) --config $(CONFIG) build: mkdir -p $(BIN_DIR) diff --git a/cmd/pastebin/main.go b/cmd/pastebin/main.go index 672fc8f..6ff4b7a 100644 --- a/cmd/pastebin/main.go +++ b/cmd/pastebin/main.go @@ -2,7 +2,7 @@ package main import ( "context" - "flag" + "fmt" "log" "net/http" "os" @@ -10,6 +10,8 @@ import ( "syscall" "time" + "github.com/spf13/cobra" + "scratchbox/internal/cleanup" "scratchbox/internal/config" httpapi "scratchbox/internal/http" @@ -17,24 +19,43 @@ import ( ) func main() { - configPath := flag.String("config", "", "path to config file") - flag.Parse() + cmd := newRootCmd() + if err := cmd.Execute(); err != nil { + log.Fatalf("command failed: %v", err) + } +} - cfg, err := config.Load(*configPath) +func newRootCmd() *cobra.Command { + var configPath string + + cmd := &cobra.Command{ + Use: "pastebin", + Short: "Run the scratchbox service", + SilenceUsage: true, + RunE: func(_ *cobra.Command, _ []string) error { + return run(configPath) + }, + } + cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file") + return cmd +} + +func run(configPath string) error { + cfg, err := config.Load(configPath) if err != nil { - log.Fatalf("config load failed: %v", err) + return fmt.Errorf("config load failed: %w", err) } logger := log.New(os.Stdout, "[pastebin] ", log.LstdFlags|log.LUTC) store, err := storage.NewFilesystemStore(cfg.Storage.DataDir) if err != nil { - logger.Fatalf("storage init failed: %v", err) + return fmt.Errorf("storage init failed: %w", err) } server, err := httpapi.NewServer(cfg, store, logger) if err != nil { - logger.Fatalf("http init failed: %v", err) + return fmt.Errorf("http init failed: %w", err) } cleanupWorker := cleanup.NewWorker(store, cfg.Storage.CleanupIntervalParsed, logger) @@ -60,6 +81,7 @@ func main() { logger.Printf("listening on %s", cfg.Server.ListenAddr) if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - logger.Fatalf("http server exited with error: %v", err) + return fmt.Errorf("http server exited with error: %w", err) } + return nil } diff --git a/cmd/pastebin/main_test.go b/cmd/pastebin/main_test.go index a4622b8..c5aecef 100644 --- a/cmd/pastebin/main_test.go +++ b/cmd/pastebin/main_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "flag" "io" "log" "net" @@ -167,8 +166,6 @@ func TestMainHelperProcess(t *testing.T) { if os.Getenv("GO_WANT_MAIN_HELPER") != "1" { return } - - flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) mode := os.Getenv("MAIN_HELPER_MODE") switch mode { case "success": @@ -183,16 +180,16 @@ func TestMainHelperProcess(t *testing.T) { _ = proc.Signal(os.Interrupt) } }() - os.Args = []string{"pastebin", "-config", cfgPath} + os.Args = []string{"pastebin", "--config", cfgPath} main() os.Exit(0) case "bad-config": - os.Args = []string{"pastebin", "-config", "/no/such/config.yaml"} + os.Args = []string{"pastebin", "--config", "/no/such/config.yaml"} main() os.Exit(0) case "storage-fail": cfgPath := os.Getenv("MAIN_HELPER_CONFIG") - os.Args = []string{"pastebin", "-config", cfgPath} + os.Args = []string{"pastebin", "--config", cfgPath} main() os.Exit(0) case "http-fail": @@ -200,7 +197,7 @@ func TestMainHelperProcess(t *testing.T) { if err := os.Chdir(t.TempDir()); err != nil { os.Exit(2) } - os.Args = []string{"pastebin", "-config", cfgPath} + os.Args = []string{"pastebin", "--config", cfgPath} main() os.Exit(0) default: diff --git a/go.mod b/go.mod index 3c5dad4..6348d70 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,12 @@ module scratchbox go 1.25.4 require ( + github.com/spf13/cobra v1.10.1 golang.org/x/time v0.15.0 gopkg.in/yaml.v3 v3.0.1 ) + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.9 // indirect +) diff --git a/go.sum b/go.sum index 6b41d6b..4c987fb 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,11 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s= +github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= +github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=