remove shell scripts as "," command and auto rebuild
The "go build" is fast but sticky when hit "," command, so I remove it. I think auto rebuild is not that useful, just fancy.
This commit is contained in:
31
main.go
31
main.go
@@ -1,29 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var commaSh = `#!/bin/sh
|
|
||||||
|
|
||||||
script_path=$(realpath $0)
|
|
||||||
script_dir=$(dirname $script_path)
|
|
||||||
|
|
||||||
cd $script_dir
|
|
||||||
|
|
||||||
output=/tmp/commandgo
|
|
||||||
|
|
||||||
# why not just using "go run ." ?
|
|
||||||
# Because "go run ." will inject GOBIN into PATH while compiling,
|
|
||||||
# and modifed PATH will pass into actual program.
|
|
||||||
# So the program doesn't have the same PATH as outside.
|
|
||||||
# It's not a acceptable behavior.
|
|
||||||
|
|
||||||
go build -o $output . && $output $@
|
|
||||||
`
|
|
||||||
|
|
||||||
func commandsUsage() string {
|
func commandsUsage() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
`commands:
|
`commands:
|
||||||
@@ -32,18 +13,6 @@ func commandsUsage() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("detect current directory failed! %w", err))
|
|
||||||
}
|
|
||||||
target := pwd + string(os.PathSeparator) + ","
|
|
||||||
_, err = os.Stat(target)
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
err := os.WriteFile(target, []byte(commaSh), 0755)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("write sh failed: %w", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(os.Args) >= 2 {
|
if len(os.Args) >= 2 {
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "path":
|
case "path":
|
||||||
|
|||||||
13
readme.md
13
readme.md
@@ -7,16 +7,5 @@ Personal tools set written in Golang.
|
|||||||
1. Clone this repository to somewhere
|
1. Clone this repository to somewhere
|
||||||
1. Add the root directory to *PATH*
|
1. Add the root directory to *PATH*
|
||||||
1. Change directory to the root directory
|
1. Change directory to the root directory
|
||||||
1. Run `go run .` to generate "," command
|
1. Run `go build -o "," .` to generate "," command
|
||||||
1. Run "," command to see all commands
|
1. Run "," command to see all commands
|
||||||
|
|
||||||
"," command can auto rebuild if you change the code in this repository.
|
|
||||||
Actually it always builds each time you type it! Because `go build` is
|
|
||||||
very fast and it has cache, it's not a problem in terms of speed.
|
|
||||||
|
|
||||||
## Q&A
|
|
||||||
|
|
||||||
Q: Why generating "," shell script instead of putting it in repository?
|
|
||||||
|
|
||||||
A: I like the repository keeps 100% Go source code. I tried writting ","
|
|
||||||
shell script as a Go source code with shebang, but it didn't work.
|
|
||||||
|
|||||||
Reference in New Issue
Block a user