-
Notifications
You must be signed in to change notification settings - Fork 383
/
main.go
96 lines (85 loc) · 1.88 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"flag"
"fmt"
mrand "math/rand"
"os"
"strings"
"berty.tech/berty/v2/go/internal/initutil"
"berty.tech/berty/v2/go/pkg/errcode"
"github.com/oklog/run"
ff "github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"moul.io/srand"
)
var manager *initutil.Manager
func main() {
err := runMain(os.Args[1:])
switch {
case err == nil:
// noop
case err == flag.ErrHelp || strings.Contains(err.Error(), flag.ErrHelp.Error()):
os.Exit(2)
default:
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
os.Exit(1)
}
}
func runMain(args []string) error {
mrand.Seed(srand.Secure())
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
// init manager
{
var err error
manager, err = initutil.New(ctx)
if err != nil {
return errcode.TODO.Wrap(err)
}
defer manager.Close()
}
// root command
var root *ffcli.Command
{
var fs = flag.NewFlagSet("berty", flag.ContinueOnError)
manager.SetupLoggingFlags(fs)
root = &ffcli.Command{
ShortUsage: "berty [global flags] <subcommand> [flags] [args...]",
FlagSet: fs,
Options: []ff.Option{ff.WithEnvVarPrefix("BERTY")},
Exec: func(context.Context, []string) error { return flag.ErrHelp },
Subcommands: []*ffcli.Command{
daemonCommand(),
miniCommand(),
bannerCommand(),
versionCommand(),
systemInfoCommand(),
groupinitCommand(),
shareInviteCommand(),
tokenServerCommand(),
},
}
}
// create run.Group
var process run.Group
{
// handle close signal
execute, interrupt := run.SignalHandler(ctx, os.Interrupt)
process.Add(execute, interrupt)
// add root command to process
process.Add(func() error {
return root.ParseAndRun(ctx, args)
}, func(error) {
ctxCancel()
})
}
// start the run.Group process
{
err := process.Run()
if err == context.Canceled {
return nil
}
return err
}
}