summaryrefslogtreecommitdiff
path: root/cmd/geheim/main.go
blob: 1d5f6e6cf316361428524e5100efeb8d70492429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// main is the thin entry point for the geheim binary.
// It handles the -version flag, sets up a signal-cancellable context,
// initialises the CLI, and exits with the code returned by Run.
// All command logic lives in internal/cli.
package main

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

	"codeberg.org/snonux/geheim/internal/cli"
	"codeberg.org/snonux/geheim/internal/version"
)

func main() {
	// -version prints the build version and exits immediately.
	versionFlag := flag.Bool("version", false, "print version and exit")
	flag.Parse()
	if *versionFlag {
		fmt.Println(version.Version)
		os.Exit(0)
	}

	// Cancel the context on SIGINT or SIGTERM so that long-running operations
	// (fzf, external editors) terminate gracefully rather than being killed hard.
	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()

	c, err := cli.New(ctx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FATAL %v\n", err)
		os.Exit(3)
	}

	// flag.Args() returns arguments after flags, so flag-aware invocations
	// like `geheim -version` work while plain `geheim cat foo` still passes
	// all args through unchanged.
	os.Exit(c.Run(ctx, flag.Args()))
}