main: add -json flag support to go env

This is needed for VS Code support.
This commit is contained in:
Ayke van Laethem
2020-03-06 11:07:15 +01:00
parent 25e13d887f
commit 4e07fc569c
+20 -1
View File
@@ -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,6 +963,21 @@ 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 *flagJSON {
keys := goenv.Keys
if flag.NArg() != 0 {
// 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 {
if flag.NArg() == 0 { if flag.NArg() == 0 {
// Show all environment variables. // Show all environment variables.
for _, key := range goenv.Keys { for _, key := range goenv.Keys {
@@ -971,6 +989,7 @@ func main() {
fmt.Println(goenv.Get(flag.Arg(i))) fmt.Println(goenv.Get(flag.Arg(i)))
} }
} }
}
default: default:
fmt.Fprintln(os.Stderr, "Unknown command:", command) fmt.Fprintln(os.Stderr, "Unknown command:", command)
usage() usage()