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
import (
"encoding/json"
"fmt"
"io"
"os"
@ -15,10 +16,18 @@ var args struct {
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:""`
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`
ExposeDeps bool `arg:"--expose-deps" help:"expose dependencies to program" 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"`
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() {
@ -55,6 +64,24 @@ func main() {
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")
if len(codelines) < 2 {
@ -70,10 +97,5 @@ func main() {
}
gen := shebang + "\n\n" + gencode(found) + "\n\n" + strings.Join(codelines[1:], "\n")
if args.Outfile == "" {
fmt.Printf("%s", gen)
} else {
os.WriteFile(args.Outfile, []byte(gen), os.FileMode(0o755))
}
emit(gen)
}