This commit is contained in:
2026-06-12 14:47:58 +08:00
commit edd950f38d
6 changed files with 74 additions and 0 deletions

14
, Executable file
View 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
View File

@@ -0,0 +1 @@
*~

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module commago
go 1.26.4

30
main.go Normal file
View 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
View 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)
}
}

6
readme.md Normal file
View File

@@ -0,0 +1,6 @@
## commago
Personal tools set written in Golang.
To use it just clone and add the directory to *PATH*.
Then type "," command, all commands will appear.