-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_command.go
61 lines (53 loc) · 1.02 KB
/
shell_command.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
package rove
import (
"fmt"
"strings"
"github.com/alessio/shellescape"
)
type ShellArg struct {
Check bool
Value string
}
func (arg ShellArg) String() string {
if !arg.Check || arg.Value == "" {
return ""
}
return arg.Value
}
type ShellCommand struct {
Name string
Flags []ShellFlag
Args []ShellArg
}
func (cmd ShellCommand) String() string {
parts := []string{cmd.Name}
for _, flag := range cmd.Flags {
if str := flag.String(); str != "" {
parts = append(parts, str)
}
}
for _, arg := range cmd.Args {
if str := arg.String(); str != "" {
parts = append(parts, str)
}
}
return strings.Join(parts, " ")
}
type ShellFlag struct {
AllowEmpty bool
Check bool
Name string
Value string
}
func (flag ShellFlag) String() string {
if !flag.Check || flag.Name == "" {
return ""
}
if flag.Value == "" {
if flag.AllowEmpty {
return fmt.Sprintf("--%s ''", flag.Name)
}
return fmt.Sprint("--", flag.Name)
}
return fmt.Sprintf("--%s %s", flag.Name, shellescape.Quote(flag.Value))
}