Switched to using cobra for better cli support
This commit is contained in:
@@ -15,7 +15,7 @@ help:
|
|||||||
@echo " make clean - Remove built artifacts"
|
@echo " make clean - Remove built artifacts"
|
||||||
|
|
||||||
run:
|
run:
|
||||||
go run $(CMD_DIR) -config $(CONFIG)
|
go run $(CMD_DIR) --config $(CONFIG)
|
||||||
|
|
||||||
build:
|
build:
|
||||||
mkdir -p $(BIN_DIR)
|
mkdir -p $(BIN_DIR)
|
||||||
|
|||||||
+30
-8
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"scratchbox/internal/cleanup"
|
"scratchbox/internal/cleanup"
|
||||||
"scratchbox/internal/config"
|
"scratchbox/internal/config"
|
||||||
httpapi "scratchbox/internal/http"
|
httpapi "scratchbox/internal/http"
|
||||||
@@ -17,24 +19,43 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configPath := flag.String("config", "", "path to config file")
|
cmd := newRootCmd()
|
||||||
flag.Parse()
|
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 {
|
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)
|
logger := log.New(os.Stdout, "[pastebin] ", log.LstdFlags|log.LUTC)
|
||||||
|
|
||||||
store, err := storage.NewFilesystemStore(cfg.Storage.DataDir)
|
store, err := storage.NewFilesystemStore(cfg.Storage.DataDir)
|
||||||
if err != nil {
|
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)
|
server, err := httpapi.NewServer(cfg, store, logger)
|
||||||
if err != nil {
|
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)
|
cleanupWorker := cleanup.NewWorker(store, cfg.Storage.CleanupIntervalParsed, logger)
|
||||||
@@ -60,6 +81,7 @@ func main() {
|
|||||||
|
|
||||||
logger.Printf("listening on %s", cfg.Server.ListenAddr)
|
logger.Printf("listening on %s", cfg.Server.ListenAddr)
|
||||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -167,8 +166,6 @@ func TestMainHelperProcess(t *testing.T) {
|
|||||||
if os.Getenv("GO_WANT_MAIN_HELPER") != "1" {
|
if os.Getenv("GO_WANT_MAIN_HELPER") != "1" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
|
||||||
mode := os.Getenv("MAIN_HELPER_MODE")
|
mode := os.Getenv("MAIN_HELPER_MODE")
|
||||||
switch mode {
|
switch mode {
|
||||||
case "success":
|
case "success":
|
||||||
@@ -183,16 +180,16 @@ func TestMainHelperProcess(t *testing.T) {
|
|||||||
_ = proc.Signal(os.Interrupt)
|
_ = proc.Signal(os.Interrupt)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
os.Args = []string{"pastebin", "-config", cfgPath}
|
os.Args = []string{"pastebin", "--config", cfgPath}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case "bad-config":
|
case "bad-config":
|
||||||
os.Args = []string{"pastebin", "-config", "/no/such/config.yaml"}
|
os.Args = []string{"pastebin", "--config", "/no/such/config.yaml"}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case "storage-fail":
|
case "storage-fail":
|
||||||
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
|
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
|
||||||
os.Args = []string{"pastebin", "-config", cfgPath}
|
os.Args = []string{"pastebin", "--config", cfgPath}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case "http-fail":
|
case "http-fail":
|
||||||
@@ -200,7 +197,7 @@ func TestMainHelperProcess(t *testing.T) {
|
|||||||
if err := os.Chdir(t.TempDir()); err != nil {
|
if err := os.Chdir(t.TempDir()); err != nil {
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
os.Args = []string{"pastebin", "-config", cfgPath}
|
os.Args = []string{"pastebin", "--config", cfgPath}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ module scratchbox
|
|||||||
go 1.25.4
|
go 1.25.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/spf13/cobra v1.10.1
|
||||||
golang.org/x/time v0.15.0
|
golang.org/x/time v0.15.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.9 // indirect
|
||||||
|
)
|
||||||
|
|||||||
@@ -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 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
|||||||
Reference in New Issue
Block a user