-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscmd.go
126 lines (113 loc) · 2.95 KB
/
syscmd.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
package syscmd
import (
"fmt"
"runtime"
"strings"
"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/show"
"github.com/gookit/gitw"
"github.com/gookit/goutil/sysutil"
"github.com/inhere/kite-go/internal/app"
)
// SysCmd command
var SysCmd = &gcli.Command{
Name: "sys",
Aliases: []string{"os", "system"},
Desc: "provide some useful system commands",
Subs: []*gcli.Command{
SearchExeCmd,
WhichExeCmd,
SysInfoCmd,
NewQuickOpenCmd(),
NewBatchRunCmd(),
NewEnvInfoCmd(),
NewClipboardCmd(),
},
}
// WhichExeCmd command
var WhichExeCmd = &gcli.Command{
Name: "which",
Aliases: []string{"whereis", "type"},
Desc: "show full path for the executable name",
Config: func(c *gcli.Command) {
c.AddArg("name", "executable name for match", true)
},
Func: func(c *gcli.Command, _ []string) error {
name := c.Arg("name").String()
file, err := sysutil.Executable(name)
if err != nil {
return err
}
fmt.Println(file)
return nil
},
}
// SearchExeCmd command
var SearchExeCmd = &gcli.Command{
Name: "find-bin",
Aliases: []string{"find-exe", "search"},
Desc: "search executable file in system PATH",
Config: func(c *gcli.Command) {
c.AddArg("keyword", "keywords for search in PATH dirs", true)
},
Func: func(c *gcli.Command, _ []string) error {
files := sysutil.SearchPath(c.Arg("keyword").String(), 10)
show.AList("Matched exe files:", files)
return nil
},
}
// SysInfoCmd command
var SysInfoCmd = &gcli.Command{
Name: "info",
// Aliases: []string{"i"},
Desc: "display current operation system information",
Config: func(c *gcli.Command) {
// c.AddArg("keyword", "keywords for search in PATH dirs", true)
},
Func: func(c *gcli.Command, _ []string) error {
info := map[string]any{
"os platform": runtime.GOOS,
"os arch": runtime.GOARCH,
}
if sysutil.IsWindows() {
if sysutil.IsMSys() {
info["hosts file1"] = "/etc/hosts"
info["hosts file"] = "/c/Windows/System32/drivers/etc/hosts"
} else {
info["hosts file"] = "C:\\Windows\\System32\\drivers\\etc\\hosts"
}
} else {
info["hosts file"] = "/etc/hosts"
}
show.AList("System info:", info)
return nil
},
}
// NewQuickOpenCmd command
func NewQuickOpenCmd() *gcli.Command {
return &gcli.Command{
Name: "open",
Aliases: []string{"open-exe"},
Desc: "open input file or dir or remote URL address",
Config: func(c *gcli.Command) {
c.AddArg("name", "bin name or URL address", true)
},
Func: func(c *gcli.Command, _ []string) error {
name := c.Arg("name").String()
var dstFile = name
if strings.Contains(name, "/") {
// special github url
if strings.HasPrefix(name, gitw.GitHubHost) {
dstFile = "https://" + name
// } else if fsutil.PathExists(name) {
// // nothing ...
// } else if validate.IsURL(name) {
}
} else if app.OpenMap.HasAlias(name) {
dstFile = app.OpenMap.ResolveAlias(name)
}
c.Infoln("Will Open the:", dstFile)
return sysutil.Open(dstFile)
},
}
}