gotools/formatting/cmd/path2tree/main.go

44 lines
879 B
Go
Raw Normal View History

2018-12-19 14:45:37 +01:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"git.lattuga.net/blallo/gotools/formatting"
)
func showExamples() {
enc := json.NewEncoder(os.Stderr)
for i, example := range examples {
2018-12-19 14:51:54 +01:00
fmt.Fprintf(os.Stderr, "\nExample %d\n\nInput:\n\n", i)
formatting.PrettyPrint(example)
fmt.Fprintf(os.Stderr, "\nOutput:\n\n")
2018-12-19 14:45:37 +01:00
formatting.EncodeJSON(example, enc)
}
}
func main() {
filepath := flag.String("file", "", "Path to a file of paths with descriptions")
flag.Parse()
var input map[string]string
if *filepath != "" {
filecont, err := ioutil.ReadFile(*filepath)
if err != nil {
panic(err)
}
err = json.Unmarshal(filecont, &input)
if err != nil {
panic(err)
}
} else {
fmt.Fprintf(os.Stderr, "Missing input file!\n")
showExamples()
os.Exit(1)
}
enc := json.NewEncoder(os.Stdout)
formatting.EncodeJSON(input, enc)
}