feat: implement --yield-deps-only and fix tag issues

This commit is contained in:
b1ek 2024-09-07 17:06:06 +10:00
parent 35ed194361
commit 388ab8f020
Signed by: blek
GPG Key ID: A622C22C9BC616B2
1 changed files with 31 additions and 9 deletions

40
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -15,10 +16,18 @@ var args struct {
File string `arg:"positional" help:"if not specified, will read from stdin" default:""` File string `arg:"positional" help:"if not specified, will read from stdin" default:""`
Outfile string `arg:"positional" help:"if not specified, will emit to stdout" default:""` Outfile string `arg:"positional" help:"if not specified, will emit to stdout" default:""`
Version bool `arg:"-v" help:"print version and exit"` Version bool `arg:"-v" help:"print version and exit"`
YieldDepsOnly bool `arg:"--yield-deps-only" help:"print dependencies as a JSON array and exit" default:false` YieldDepsOnly bool `arg:"--yield-deps-only" help:"print dependencies as a JSON array and exit" default:"false"`
ExposeDeps bool `arg:"--expose-deps" help:"expose dependencies to program" default:false` ExposeDeps bool `arg:"--expose-deps" help:"expose dependencies to program" default:"false"`
DepsVarName string `arg:"--deps-var-name" help:"override deps variable name" default:"deps"` DepsVarName string `arg:"--deps-var-name" help:"override deps variable name" default:"deps"`
IgnoreShebang bool `arg:"--ignore-shebang" help:"ignore shebang requirement" default:false` IgnoreShebang bool `arg:"--ignore-shebang" help:"ignore shebang requirement" default:"false"`
}
func emit(code string) {
if args.Outfile == "" {
fmt.Printf("%s", code)
} else {
os.WriteFile(args.Outfile, []byte(code), os.FileMode(0o755))
}
} }
func main() { func main() {
@ -55,6 +64,24 @@ func main() {
panic(err) panic(err)
} }
if args.YieldDepsOnly {
marshaled, err := json.Marshal(found)
if err != nil {
panic(err)
}
if string(marshaled) == "null" {
emit("[]")
} else {
emit(string(marshaled))
}
os.Exit(0)
}
if len(found) == 0 {
emit(code)
os.Exit(0)
}
codelines := strings.Split(code, "\n") codelines := strings.Split(code, "\n")
if len(codelines) < 2 { if len(codelines) < 2 {
@ -70,10 +97,5 @@ func main() {
} }
gen := shebang + "\n\n" + gencode(found) + "\n\n" + strings.Join(codelines[1:], "\n") gen := shebang + "\n\n" + gencode(found) + "\n\n" + strings.Join(codelines[1:], "\n")
emit(gen)
if args.Outfile == "" {
fmt.Printf("%s", gen)
} else {
os.WriteFile(args.Outfile, []byte(gen), os.FileMode(0o755))
}
} }