generated from octomation/go-tool
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (49 loc) · 1.23 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
52
53
54
55
56
57
58
59
package main
import (
"context"
"fmt"
"os"
"runtime/debug"
"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/toolset/maintainer/internal/command"
"go.octolab.org/toolset/maintainer/internal/config"
)
const unknown = "unknown"
var (
commit = unknown
date = unknown
version = "dev"
exit = os.Exit
stderr = color.Error
stdout = color.Output
)
func init() {
if info, available := debug.ReadBuildInfo(); available && commit == unknown {
version = info.Main.Version
commit = fmt.Sprintf("%s, mod sum: %s", commit, info.Main.Sum)
}
}
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)
}