Switched to using cobra for better cli support
This commit is contained in:
+30
-8
@@ -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
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user