/
cli.go
169 lines (134 loc) · 3.79 KB
/
cli.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2022, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package builder
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/choria-io/fisk"
"github.com/sirupsen/logrus"
)
func RunTaskCLI(ctx context.Context, watchInterrupts bool, opts ...Option) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if opts == nil {
opts = []Option{}
}
bldr, err := createBuilder(ctx, os.Args[0], nil, opts...)
if err != nil {
return err
}
if watchInterrupts {
go interruptWatcher(ctx, cancel, bldr.log)
}
requireDescription = false
defaultUsageTemplate = fisk.CompactUsageTemplate
descriptionFmt = `%s
Help: https://choria-io.github.io/appbuilder`
defaultDescription = "App Builder Task"
err = bldr.runTaskCLI()
if errors.Is(err, ErrDefinitionNotfound) {
return fmt.Errorf("could not find a valid task file called any of %s", strings.Join(taskFileNames, ", "))
}
return err
}
func RunBuilderCLI(ctx context.Context, watchInterrupts bool, opts ...Option) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if opts == nil {
opts = []Option{}
}
bldr, err := createBuilder(ctx, os.Args[0], nil, opts...)
if err != nil {
return err
}
if watchInterrupts {
go interruptWatcher(ctx, cancel, bldr.log)
}
return bldr.RunBuilderCLI()
}
// MountAsCommand takes the given definition and mounts it on app using name
func MountAsCommand(ctx context.Context, app KingpinCommand, definition []byte, log Logger) error {
bldr, err := createBuilder(ctx, "builder", log, WithAppDefinitionBytes(definition))
if err != nil {
return err
}
bldr.cfg, err = bldr.LoadConfig()
if err != nil && !errors.Is(err, ErrConfigNotFound) {
return err
}
return bldr.registerCommands(app, bldr.def.commands...)
}
// RunStandardCLI runs a standard command line instance with shutdown watchers etc. If log is nil a logger will be created
func RunStandardCLI(ctx context.Context, name string, watchInterrupts bool, log Logger, opts ...Option) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if opts == nil {
opts = []Option{}
}
bldr, err := createBuilder(ctx, name, log, opts...)
if err != nil {
return err
}
if watchInterrupts {
go interruptWatcher(ctx, cancel, bldr.log)
}
if !bldr.HasDefinition() {
return fmt.Errorf("%w: %s", ErrDefinitionNotfound, name)
}
return bldr.RunCommand()
}
func createBuilder(ctx context.Context, name string, log Logger, opts ...Option) (*AppBuilder, error) {
if log == nil {
logger := logrus.New()
log = logrus.NewEntry(logger).WithField("app", name)
if os.Getenv("BUILDER_DEBUG") != "" || os.Getenv("BUILDER_DRY_RUN") != "" {
logger.SetLevel(logrus.DebugLevel)
} else {
logger.SetLevel(logrus.WarnLevel)
}
}
if len(opts) == 0 {
if cfg := os.Getenv("BUILDER_CONFIG"); cfg != "" {
opts = append(opts, WithConfigPaths(cfg))
}
if cfg := os.Getenv("BUILDER_APP"); cfg != "" {
opts = append(opts, WithAppDefinitionFile(cfg))
}
}
// we set the logger option first, if we made a new logger above
// it will be set, if the user supplied one, it will be set
//
// but if a user later pass WithLogger() also that one will win
// as options are processed in array order
if log != nil {
opts = append([]Option{WithLogger(log)}, opts...)
}
return New(ctx, name, opts...)
}
func interruptWatcher(ctx context.Context, cancel context.CancelFunc, log Logger) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
select {
case sig := <-sigs:
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
go func() {
<-time.After(2 * time.Second)
os.Exit(1)
}()
}
log.Infof("Shutting down on %s", sig)
cancel()
case <-ctx.Done():
return
}
}
}