-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsetup_app.go
99 lines (82 loc) · 2.6 KB
/
setup_app.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
package cmdr
import (
"os"
"github.com/hedzr/cmdr/v2/builder"
"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/cli/worker"
"github.com/hedzr/cmdr/v2/pkg/logz"
"github.com/hedzr/store"
)
// Create provides a concise interface to create an cli app easily.
func Create(appName, version, author, desc string, opts ...cli.Opt) Creator {
s := &cs{}
return s.Create(appName, version, author, desc, opts...)
}
type Creator interface {
WithOpts(opts ...cli.Opt) Creator
// WithAdders receives a couple of cli.CmdAdder adders
// which can initialize a command standalone.
WithAdders(adders ...cli.CmdAdder) Creator
// WithBuilders receives a couple of callbacks of cli.CommandBuilder
// which can initialize command and flag.
WithBuilders(builders ...func(b cli.CommandBuilder)) Creator
// With a callback, you can initialize commands and
// flags by app object directly.
With(cb func(app cli.App)) Creator
// OnAction apply the root-level onAction handler to the app object.
OnAction(handler cli.OnInvokeHandler) Creator
// Build creates the final app object and stop the
// building sequence of a builder pattern.
Build() (app cli.App)
}
type cs struct {
app cli.App
}
func (s *cs) OnAction(handler cli.OnInvokeHandler) Creator {
s.app.OnAction(handler)
return s
}
func (s *cs) Create(appName, version, author, desc string, opts ...cli.Opt) Creator {
// A cmdr app will close all peripherals in basics.Closers() at exiting.
// So you could always register the objects which wanna be cleanup at
// app terminating, by [basics.RegisterPeripheral(...)].
// See also: https://github.com/hedzr/is/blob/master/basics/ and
_ = os.Setenv("CMDR_VERSION", Version)
logz.Verbose("setup env-var at earlier time", "CMDR_VERSION", Version)
cfg := cli.NewConfig(append([]cli.Opt{WithStore(store.New())}, opts...)...)
w := worker.New(cfg)
s.app = builder.New(w)
// s.app = New(
// // use an option store explicitly, or a dummy store by default
// append([]cli.Opt{WithStore(store.New())}, opts...)...,
// )
s.app.
Info(appName, version, desc).
Author(author) // .Description(``).Header(``).Footer(``)
return s
}
func (s *cs) WithOpts(opts ...cli.Opt) Creator {
s.app.WithOpts(opts...)
return s
}
func (s *cs) WithBuilders(builders ...func(b cli.CommandBuilder)) Creator {
for _, cb := range builders {
s.app.RootBuilder(cb)
}
return s
}
func (s *cs) With(cb func(app cli.App)) Creator {
if cb != nil {
cb(s.app)
}
return s
}
func (s *cs) WithAdders(adders ...cli.CmdAdder) Creator {
for _, adder := range adders {
adder.Add(s.app)
}
return s
}
func (s *cs) Build() (app cli.App) {
return s.app
}