init
This commit is contained in:
14
,
Executable file
14
,
Executable file
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
script_path=$(realpath $0)
|
||||||
|
script_dir=$(dirname $script_path)
|
||||||
|
|
||||||
|
cd $script_dir
|
||||||
|
|
||||||
|
output=/tmp/commandgo
|
||||||
|
|
||||||
|
# 为什么不直接使用 go run . 呢
|
||||||
|
# 因为 go run 编译时会往 PATH 里注入 Go 的 bin 目录,这个修改过后的 PATH 会传递给子进程
|
||||||
|
# 于是程序运行起来时获取的 PATH 环境变量和外界不一致,因此不用 go run . 编译运行
|
||||||
|
go build -o $output . && $output $@
|
||||||
|
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*~
|
||||||
30
main.go
Normal file
30
main.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func commandsUsage() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
`commands:
|
||||||
|
path %v
|
||||||
|
`, commandPathDescription)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) >= 2 {
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "path":
|
||||||
|
commandPath(os.Args[1:])
|
||||||
|
default:
|
||||||
|
fmt.Printf("unknow command `%v`\n\n", os.Args[1])
|
||||||
|
fmt.Printf("%v", commandsUsage())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("Personal scripts set written in Golang\n")
|
||||||
|
fmt.Printf("%v", commandsUsage())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
20
path.go
Normal file
20
path.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var commandPathDescription = "list paths"
|
||||||
|
|
||||||
|
func commandPath(args []string) {
|
||||||
|
fs := flag.NewFlagSet("path", flag.ContinueOnError)
|
||||||
|
fs.Parse(args)
|
||||||
|
rawPath := os.Getenv("PATH")
|
||||||
|
paths := strings.Split(rawPath, string(os.PathListSeparator))
|
||||||
|
for _, item := range paths {
|
||||||
|
fmt.Println(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user