diff --git a/main.go b/main.go index 6bd3c00..828b25d 100644 --- a/main.go +++ b/main.go @@ -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) }