-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
158 lines (130 loc) · 4.28 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
package command
import (
"errors"
"github.com/urfave/cli/v2" // imports as package "cli"
)
type commandHandler func(chc commandHandleProjectConfig) string
type cliCommand struct {
usage map[string]string
aliases map[string]string
args map[string][]string
command map[string]func(commandHandleProjectConfig) string
}
func (cli *cliCommand) GetCommand(cmd string, cfg commandHandleProjectConfig) string {
command := cli.command[cmd]
return command(cfg)
}
func (cli *cliCommand) GetArgs() map[string][]string {
return cli.args
}
type cliCommandHandleProjectConfig interface {
GetProjectMainContainer() string
SaveContainerNameToProjectConfig(string) error
GetShell() string
}
type callCliCommandDialog interface {
SetMainContaner([]string) (int, string, error)
}
type commandHandleProjectConfig interface {
GetShell() string
}
type callCliCommandOptions interface {
GetInitFunction() func(bool) string
GetContainerList() ([]string, error)
GetExecCommand() func(ExecOptions, *cli.App) error
}
// CallCliCommand calls a range of differnt cli commands
func CallCliCommand(commandName string, cfg cliCommandHandleProjectConfig, d callCliCommandDialog, options callCliCommandOptions) *cli.Command {
initf := options.GetInitFunction()
execCommand := options.GetExecCommand()
clic := &cliCommand{
usage: map[string]string{
"cli": "Runs cli command in conatiner: {docker exec main_conatain} [command] [custom parameters]",
"sh": "Runs cli sh command in conatiner: {docker exec main_conatain {shell_type}} [custom parameters]",
"clinotty": "Runs command {docker exec -t main_container} [command] [custom parameters]",
"cliroot": "Runs command {docker exec -u root main_container} [command] [custom parameters]",
"clirootnotty": "Runs command {docker exec -u root -t main_container} [command] [custom parameters]",
},
aliases: map[string]string{
"cli": "c",
"sh": "sh",
"clinotty": "cnt",
"cliroot": "cr",
"clirootnotty": "crnt",
},
args: map[string][]string{
"cli": []string{"-it"},
"sh": []string{"-it"},
"cliroot": []string{"-u", "root", "-it"},
"clinotty": []string{"-i"},
"clirootnotty": []string{"-u", "root", "-i"},
},
command: map[string]func(commandHandleProjectConfig) string{
"cli": func(c commandHandleProjectConfig) string {
return ""
},
"sh": func(c commandHandleProjectConfig) string {
return c.GetShell()
},
"clinotty": func(c commandHandleProjectConfig) string {
return ""
},
"cliroot": func(c commandHandleProjectConfig) string {
return ""
},
"clirootnotty": func(c commandHandleProjectConfig) string {
return ""
},
},
}
return &cli.Command{
Name: commandName,
Aliases: []string{clic.aliases[commandName]},
Usage: clic.usage[commandName],
SkipFlagParsing: true,
Action: func(c *cli.Context) (err error) {
initf(true)
var args []string
if args, err = cliCommandHandle(commandName, cfg, d, clic, options, c.Args()); err != nil {
return err
}
eo := ExecOptions{
command: "docker",
args: args,
tty: true,
detach: true,
}
return execCommand(eo, c.App)
},
}
}
type cliCommandInterface interface {
GetCommand(string, commandHandleProjectConfig) string
GetArgs() map[string][]string
}
type cliCommandHandleDialog interface {
SetMainContaner([]string) (int, string, error)
}
func cliCommandHandle(index string, cfg cliCommandHandleProjectConfig, d cliCommandHandleDialog, c cliCommandInterface, options containerlist, a cli.Args) ([]string, error) {
var err error
var cl []string
if cl, err = options.GetContainerList(); err != nil {
return []string{}, err
}
if err = defineProjectMainContainer(cfg, d, cl); err != nil {
return []string{}, err
}
var initArgs = []string{"exec"}
extraInitArgs := c.GetArgs()[index]
if len(extraInitArgs) > 0 {
initArgs = append(initArgs, extraInitArgs...)
}
initArgs = append(initArgs, cfg.GetProjectMainContainer())
if c.GetCommand(index, cfg) != "" {
initArgs = append(initArgs, c.GetCommand(index, cfg))
}
if c.GetCommand(index, cfg) == "" && a.Get(0) == "" {
return []string{}, errors.New("Please specify a CLI command (ex. ls)")
}
return append(initArgs, a.Slice()...), nil
}