-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
79 lines (69 loc) · 1.33 KB
/
runner.go
File metadata and controls
79 lines (69 loc) · 1.33 KB
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
package cli
import (
"context"
"fmt"
"os"
"github.com/ake-persson/mapslice-json"
)
// Must be created by Run(), handles the running cli application
type Runner struct {
builder *Builder
ctx context.Context
cancelFunc context.CancelFunc
flags Flags
args Args
isMain bool
config interface{}
flatConfig mapslice.MapSlice
}
// Application context
func (r *Runner) Context() context.Context {
if r == nil {
panic("runner is nil")
}
return r.ctx
}
// Returns true if no command or sub command has been invoked
func (r *Runner) IsMain() bool {
return r.isMain
}
// Gracefully stops by cancelling application context
func (r *Runner) Stop() {
if r == nil {
return
}
r.cancelFunc()
}
// Forcefully exists application with os.Exit(exitCode)
func (r *Runner) Exit(errors ...error) {
var hasErr bool
for _, err := range errors {
if err == nil {
continue
}
hasErr = true
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
if hasErr {
os.Exit(1)
}
os.Exit(0)
}
// Application global flags
func (r *Runner) Flags() Flags {
if r == nil {
return nil
}
return r.flags
}
// Application wide arguments
func (r *Runner) Args() Args {
if r == nil {
return nil
}
return r.args
}
// Returns the prased configuration that was set before .Run()
func (r *Runner) Config() interface{} {
return r.config
}