-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexec.go
73 lines (68 loc) · 1.91 KB
/
exec.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
package cmd
import (
"fmt"
"github.com/Mirantis/launchpad/pkg/config"
"github.com/kballard/go-shellquote"
"github.com/urfave/cli/v2"
)
// NewExecCommand creates new exec command to be called from cli.
func NewExecCommand() *cli.Command {
return &cli.Command{
Name: "exec",
Usage: "Exec a command on a host",
ArgsUsage: "[COMMAND ..]",
Flags: append(GlobalFlags, []cli.Flag{
configFlag,
confirmFlag,
redactFlag,
&cli.StringSliceFlag{
Name: "target",
Usage: "Target (example: address[:port]) (can be given multiple times)",
Aliases: []string{"t"},
},
&cli.BoolFlag{
Name: "interactive",
Usage: "Run interactive",
Aliases: []string{"i"},
},
&cli.BoolFlag{
Name: "first",
Usage: "Use the first target found in configuration",
Aliases: []string{"f"},
},
&cli.BoolFlag{
Name: "all",
Usage: "Run on all matching targets",
Aliases: []string{"A"},
},
&cli.BoolFlag{
Name: "parallel",
Usage: "Run parallelly",
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "role",
Usage: "Use targets having this role in configuration",
Aliases: []string{"r"},
},
&cli.StringFlag{
Name: "os",
Usage: "Use targets running this OS ('linux', 'windows', os ID)",
Aliases: []string{"o"},
},
}...),
Before: actions(initLogger, checkLicense, initExec),
Action: func(ctx *cli.Context) error {
product, err := config.ProductFromFile(ctx.String("config"))
if err != nil {
return fmt.Errorf("failed to load product configuration: %w", err)
}
args := ctx.Args().Slice()
err = product.Exec(ctx.StringSlice("target"), ctx.Bool("interactive"), ctx.Bool("first"), ctx.Bool("all"), ctx.Bool("parallel"), ctx.String("role"), ctx.String("os"), shellquote.Join(args...))
if err != nil {
return fmt.Errorf("failed to execute command: %w", err)
}
return nil
},
}
}