-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (42 loc) · 1.02 KB
/
main.go
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
44
45
46
47
48
49
50
51
package main
import (
"context"
"fmt"
"os"
"github.com/fatih/color"
"go.octolab.org/errors"
"go.octolab.org/safe"
"go.octolab.org/toolkit/cli/cobra"
"go.octolab.org/unsafe"
"go.octolab.org/template/tool/internal/command"
"go.octolab.org/template/tool/internal/config"
)
const unknown = "unknown"
var (
commit = unknown
date = unknown
version = "dev"
exit = os.Exit
stderr = color.Error
stdout = color.Output
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
root := command.New()
root.SetErr(stderr)
root.SetOut(stdout)
root.AddCommand(
cobra.NewVersionCommand(version, date, commit, config.Features...),
)
safe.Do(func() error { return root.ExecuteContext(ctx) }, shutdown)
}
func shutdown(err error) {
var recovered errors.Recovered
if errors.As(err, &recovered) {
unsafe.DoSilent(fmt.Fprintf(stderr, "recovered: %+v\n", recovered.Cause()))
unsafe.DoSilent(fmt.Fprintln(stderr, "---"))
unsafe.DoSilent(fmt.Fprintf(stderr, "%+v\n", err))
}
exit(1)
}