-
Notifications
You must be signed in to change notification settings - Fork 191
/
cliutil.go
149 lines (125 loc) · 3.52 KB
/
cliutil.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
// Package cliutil provides some util functions for CLI
package cliutil
import (
"strings"
"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
)
// SplitMulti split multi string by sep string.
func SplitMulti(ss []string, sep string) []string {
ns := make([]string, 0, len(ss)+1)
for _, s := range ss {
ns = append(ns, strings.Split(s, sep)...)
}
return ns
}
// LineBuild build command line string by given args.
func LineBuild(binFile string, args []string) string {
return cmdline.NewBuilder(binFile, args...).String()
}
// BuildLine build command line string by given args.
func BuildLine(binFile string, args []string) string {
return cmdline.NewBuilder(binFile, args...).String()
}
// String2OSArgs parse input command line text to os.Args
func String2OSArgs(line string) []string {
return cmdline.NewParser(line).Parse()
}
// StringToOSArgs parse input command line text to os.Args
func StringToOSArgs(line string) []string {
return cmdline.NewParser(line).Parse()
}
// ParseLine input command line text. alias of the StringToOSArgs()
func ParseLine(line string) []string {
return cmdline.NewParser(line).Parse()
}
// QuickExec quick exec a simple command line
func QuickExec(cmdLine string, workDir ...string) (string, error) {
return sysutil.ExecLine(cmdLine, workDir...)
}
// ExecLine quick exec an command line string
func ExecLine(cmdLine string, workDir ...string) (string, error) {
return sysutil.ExecLine(cmdLine, workDir...)
}
// ExecCmd a CLI bin file and return output.
//
// Usage:
//
// ExecCmd("ls", []string{"-al"})
func ExecCmd(binName string, args []string, workDir ...string) (string, error) {
return comfunc.ExecCmd(binName, args, workDir...)
}
// ExecCommand alias of the ExecCmd()
func ExecCommand(binName string, args []string, workDir ...string) (string, error) {
return comfunc.ExecCmd(binName, args, workDir...)
}
// ShellExec exec command by shell
//
// Usage:
// ret, err := cliutil.ShellExec("ls -al")
func ShellExec(cmdLine string, shells ...string) (string, error) {
return comfunc.ShellExec(cmdLine, shells...)
}
// CurrentShell get current used shell env file. eg "/bin/zsh" "/bin/bash"
func CurrentShell(onlyName bool) (path string) {
return comfunc.CurrentShell(onlyName)
}
// HasShellEnv has shell env check.
//
// Usage:
//
// HasShellEnv("sh")
// HasShellEnv("bash")
func HasShellEnv(shell string) bool {
return comfunc.HasShellEnv(shell)
}
// BuildOptionHelpName for render flag help
func BuildOptionHelpName(names []string) string {
var sb strings.Builder
size := len(names) - 1
for i, name := range names {
sb.WriteByte('-')
if len(name) > 1 {
sb.WriteByte('-')
}
sb.WriteString(name)
if i < size {
sb.WriteString(", ")
}
}
return sb.String()
}
// ShellQuote quote a string on contains ', ", SPACE
func ShellQuote(s string) string {
var quote byte
if strings.ContainsRune(s, '"') {
quote = '\''
} else if s == "" || strings.ContainsRune(s, '\'') || strings.ContainsRune(s, ' ') {
quote = '"'
}
if quote > 0 {
ln := len(s) + 2
bs := make([]byte, ln)
bs[0] = quote
bs[ln-1] = quote
if ln > 2 {
copy(bs[1:ln-1], s)
}
s = string(bs)
}
return s
}
// OutputLines split output to lines
func OutputLines(output string) []string {
output = strings.TrimSuffix(output, "\n")
if output == "" {
return nil
}
return strings.Split(output, "\n")
}
// FirstLine from command output
//
// Deprecated: please use strutil.FirstLine
var FirstLine = strutil.FirstLine