-
Notifications
You must be signed in to change notification settings - Fork 384
/
main.go
151 lines (132 loc) · 3 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"flag"
"fmt"
mrand "math/rand"
"os"
"strings"
"github.com/oklog/run"
ff "github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"moul.io/srand"
"berty.tech/berty/v2/go/internal/initutil"
"berty.tech/berty/v2/go/pkg/errcode"
)
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.MustSecure())
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
// init manager
{
var err error
manager, err = initutil.New(nil)
if err != nil {
return errcode.TODO.Wrap(err)
}
defer manager.Close(nil)
}
guiCmd, guiInit := guiCommand()
// root command
var root *ffcli.Command
{
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 },
UsageFunc: usageFunc,
Subcommands: []*ffcli.Command{
daemonCommand(),
accountDaemonCommand(),
miniCommand(),
bannerCommand(),
versionCommand(),
systemInfoCommand(),
groupinitCommand(),
shareInviteCommand(),
tokenServerCommand(),
replicationServerCommand(),
peersCommand(),
exportCommand(),
remoteLogsCommand(),
serviceKeyCommand(),
pushServerCommand(),
p2pCommand(),
relayServerCommand(),
vcIssuerCommand(),
directoryServiceCommand(),
},
}
if guiCmd != nil {
root.Subcommands = append(root.Subcommands, guiCmd)
}
}
run := func() error {
// 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) {
fmt.Println("interrupted, closing...")
ctxCancel()
})
}
// start the run.Group process
{
err := process.Run()
if err == context.Canceled {
return nil
}
return err
}
}
if guiInit != nil && len(args) > 0 && args[0] == "gui" { // can't check subcommand after Parse
ech := make(chan error)
defer close(ech)
go func() {
ech <- run()
}()
if err := guiInit(); err != nil {
return err
}
return <-ech
}
return run()
}
func usageFunc(c *ffcli.Command) string {
advanced := manager.AdvancedHelp()
if advanced == "" {
return ffcli.DefaultUsageFunc(c)
}
return ffcli.DefaultUsageFunc(c) + "\n\n" + advanced
}
func ffSubcommandOptions() []ff.Option {
return []ff.Option{
ff.WithEnvVarPrefix("BERTY"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
}
}