cmd/simplexcalc/ now holds only main.go: it passes the version the linker sets to cli.Main and exits with the status that returns. root.go and run.go move to internal/cli with their comments, changed only to take the version as a parameter. Main reads the process's arguments and output; execute takes them as parameters so the new tests can check the version line and the non-zero status of an unknown command. Commands, flags, output, log lines and exit status are unchanged, and the -X main.version flag in the Dockerfile still applies. Model: opus-5-5
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sneak.berlin/go/simplexcalc/internal/bot"
|
||||
"sneak.berlin/go/simplexcalc/internal/config"
|
||||
"sneak.berlin/go/simplexcalc/internal/logger"
|
||||
)
|
||||
|
||||
func runCmd(version string) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "run",
|
||||
Short: "run the bot",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
return run(cmd.Context(), version)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// run reads the configuration and runs the bot until SIGINT or SIGTERM,
|
||||
// or until it fails. A failure is the process's exit status, so a
|
||||
// supervisor restarts it.
|
||||
func run(ctx context.Context, version string) error {
|
||||
cfg, err := config.New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading configuration: %w", err)
|
||||
}
|
||||
|
||||
log := logger.New(os.Stdout, cfg.Debug)
|
||||
log.Info("starting",
|
||||
"appname", appname,
|
||||
"version", version,
|
||||
"arch", runtime.GOARCH,
|
||||
"data_dir", cfg.DataDir,
|
||||
)
|
||||
|
||||
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
err = bot.Run(ctx, log, cfg.DataDir)
|
||||
if err != nil {
|
||||
log.Error("stopped", "error", err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("stopped")
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user