-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
82 lines (73 loc) · 2.14 KB
/
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
package clifx
import (
"github.com/urfave/cli/v2"
"go.uber.org/fx"
)
const (
RootCommandGroup = "cli_root_commands"
RootFlagGroup = "cli_root_flags"
)
type AppIn struct {
fx.In
AppConfig *AppConfig `optional:"true"`
AppDeps AppDeps
}
type AppConfig struct {
// The name of the program. Defaults to path.Base(os.Args[0])
Name string
// Full name of command for help, defaults to Name
HelpName string
// Description of the program.
Usage string
// Text to override the USAGE section of help
UsageText string
// Description of the program argument format.
ArgsUsage string
// Version of the program
Version string
// Description of the program
Description string
// An action to execute before any subcommands are run, but after the context is ready
// If a non-nil error is returned, no subcommands are run
Before cli.BeforeFunc
// An action to execute after any subcommands are run, but after the subcommand has finished
// It is run even if Action() panics
After cli.AfterFunc
// Execute this function if the proper command cannot be found
CommandNotFound cli.CommandNotFoundFunc
// Execute this function if a usage error occurs
OnUsageError cli.OnUsageErrorFunc
}
type CommandResult struct {
fx.Out
Command *cli.Command `group:"cli_root_commands"`
}
type AppDeps struct {
fx.In
// List of commands to execute
Commands []*cli.Command `group:"cli_root_commands"`
// List of flags to parse
Flags []cli.Flag `group:"cli_root_flags"`
Action cli.ActionFunc `optional:"true"`
}
func NewApp(in AppIn) *cli.App {
a := cli.NewApp()
// The name of the program. Defaults to path.Base(os.Args[0])
if in.AppConfig != nil {
a.Name = in.AppConfig.Name
a.HelpName = in.AppConfig.HelpName
a.Usage = in.AppConfig.Usage
a.UsageText = in.AppConfig.UsageText
a.ArgsUsage = in.AppConfig.ArgsUsage
a.Version = in.AppConfig.Version
a.Description = in.AppConfig.Description
a.Before = in.AppConfig.Before
a.After = in.AppConfig.After
a.CommandNotFound = in.AppConfig.CommandNotFound
a.OnUsageError = in.AppConfig.OnUsageError
}
a.Action = in.AppDeps.Action
a.Commands = in.AppDeps.Commands
a.Flags = in.AppDeps.Flags
return a
}