mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 15:03:41 +00:00
main: add -json flag support to go env
This is needed for VS Code support.
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -765,8 +766,10 @@ func main() {
|
|||||||
heapSize := flag.String("heap-size", "1M", "default heap size in bytes (only supported by WebAssembly)")
|
heapSize := flag.String("heap-size", "1M", "default heap size in bytes (only supported by WebAssembly)")
|
||||||
|
|
||||||
var flagJSON, flagDeps *bool
|
var flagJSON, flagDeps *bool
|
||||||
if command == "list" {
|
if command == "list" || command == "env" {
|
||||||
flagJSON = flag.Bool("json", false, "print data in JSON format")
|
flagJSON = flag.Bool("json", false, "print data in JSON format")
|
||||||
|
}
|
||||||
|
if command == "list" {
|
||||||
flagDeps = flag.Bool("deps", false, "")
|
flagDeps = flag.Bool("deps", false, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -960,15 +963,31 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
|
fmt.Printf("tinygo version %s %s/%s (using go version %s and LLVM version %s)\n", version, runtime.GOOS, runtime.GOARCH, goversion, llvm.Version)
|
||||||
case "env":
|
case "env":
|
||||||
if flag.NArg() == 0 {
|
if *flagJSON {
|
||||||
// Show all environment variables.
|
keys := goenv.Keys
|
||||||
for _, key := range goenv.Keys {
|
if flag.NArg() != 0 {
|
||||||
fmt.Printf("%s=%#v\n", key, goenv.Get(key))
|
// Show only one (or a few) environment variables.
|
||||||
|
keys = flag.Args()
|
||||||
}
|
}
|
||||||
|
// Show environment variables in JSON format.
|
||||||
|
env := make(map[string]string)
|
||||||
|
for _, key := range keys {
|
||||||
|
env[key] = goenv.Get(key)
|
||||||
|
}
|
||||||
|
encoder := json.NewEncoder(os.Stdout)
|
||||||
|
encoder.SetIndent("", "\t")
|
||||||
|
encoder.Encode(env)
|
||||||
} else {
|
} else {
|
||||||
// Show only one (or a few) environment variables.
|
if flag.NArg() == 0 {
|
||||||
for i := 0; i < flag.NArg(); i++ {
|
// Show all environment variables.
|
||||||
fmt.Println(goenv.Get(flag.Arg(i)))
|
for _, key := range goenv.Keys {
|
||||||
|
fmt.Printf("%s=%#v\n", key, goenv.Get(key))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Show only one (or a few) environment variables.
|
||||||
|
for i := 0; i < flag.NArg(); i++ {
|
||||||
|
fmt.Println(goenv.Get(flag.Arg(i)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user