From edd950f38dd883e23e46b8025c9a0a3e14970981 Mon Sep 17 00:00:00 2001 From: lixulun Date: Fri, 12 Jun 2026 14:47:58 +0800 Subject: [PATCH] init --- , | 14 ++++++++++++++ .gitignore | 1 + go.mod | 3 +++ main.go | 30 ++++++++++++++++++++++++++++++ path.go | 20 ++++++++++++++++++++ readme.md | 6 ++++++ 6 files changed, 74 insertions(+) create mode 100755 , create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 main.go create mode 100644 path.go create mode 100644 readme.md diff --git a/, b/, new file mode 100755 index 0000000..b764dd7 --- /dev/null +++ b/, @@ -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 $@ + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5d91f1f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module commago + +go 1.26.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..32aaaa1 --- /dev/null +++ b/main.go @@ -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()) + } + +} diff --git a/path.go b/path.go new file mode 100644 index 0000000..ce2c886 --- /dev/null +++ b/path.go @@ -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) + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..36e15bb --- /dev/null +++ b/readme.md @@ -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.