package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"banger/internal/cli"
	"banger/internal/cli/style"
)

func main() {
	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stop()

	cmd := cli.NewBangerCommand()
	if err := cmd.ExecuteContext(ctx); err != nil {
		var exitErr cli.ExitCodeError
		if errors.As(err, &exitErr) {
			os.Exit(exitErr.Code)
		}
		// Render the failure through the CLI's translator so RPC
		// codes become friendly text, op_ids land in parens for
		// journalctl grepping, and the "banger:" prefix turns red
		// on a TTY.
		prefix := style.Fail(os.Stderr, "banger:")
		fmt.Fprintf(os.Stderr, "%s %s\n", prefix, cli.TranslateError(os.Stderr, err))
		os.Exit(1)
	}
}
