You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
3.3 KiB
Go
148 lines
3.3 KiB
Go
1 year ago
|
package ncli
|
||
|
|
||
|
import (
|
||
|
"git.noahlan.cn/noahlan/ntool/internal/common"
|
||
|
"git.noahlan.cn/noahlan/ntool/ncli/cmdline"
|
||
|
"git.noahlan.cn/noahlan/ntool/nstr"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// 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 ExecLine(cmdLine, workDir...)
|
||
|
}
|
||
|
|
||
|
// ExecLine quick exec an command line string
|
||
|
func ExecLine(cmdLine string, workDir ...string) (string, error) {
|
||
|
p := cmdline.NewParser(cmdLine)
|
||
|
|
||
|
// create a new Cmd instance
|
||
|
cmd := p.NewExecCmd()
|
||
|
if len(workDir) > 0 {
|
||
|
cmd.Dir = workDir[0]
|
||
|
}
|
||
|
|
||
|
bs, err := cmd.Output()
|
||
|
return string(bs), err
|
||
|
}
|
||
|
|
||
|
// ExecCommand alias of the ExecCmd()
|
||
|
func ExecCommand(binName string, args []string, workDir ...string) (string, error) {
|
||
|
return ExecCmd(binName, args, workDir...)
|
||
|
}
|
||
|
|
||
|
// ExecCmd a command and return output.
|
||
|
//
|
||
|
// Usage:
|
||
|
//
|
||
|
// ExecCmd("ls", []string{"-al"})
|
||
|
func ExecCmd(binName string, args []string, workDir ...string) (string, error) {
|
||
|
return common.ExecCmd(binName, args, workDir...)
|
||
|
}
|
||
|
|
||
|
// ShellExec exec command by shell
|
||
|
// cmdLine e.g. "ls -al"
|
||
|
func ShellExec(cmdLine string, shells ...string) (string, error) {
|
||
|
return common.ShellExec(cmdLine, shells...)
|
||
|
}
|
||
|
|
||
|
// CurrentShell get current used shell env file.
|
||
|
//
|
||
|
// eg "/bin/zsh" "/bin/bash".
|
||
|
// if onlyName=true, will return "zsh", "bash"
|
||
|
func CurrentShell(onlyName bool) (binPath string) {
|
||
|
return common.CurrentShell(onlyName)
|
||
|
}
|
||
|
|
||
|
// HasShellEnv has shell env check.
|
||
|
//
|
||
|
// Usage:
|
||
|
//
|
||
|
// HasShellEnv("sh")
|
||
|
// HasShellEnv("bash")
|
||
|
func HasShellEnv(shell string) bool {
|
||
|
return common.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 nstr.FirstLine
|
||
|
var FirstLine = nstr.FirstLine
|