main.go 879 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "git.lattuga.net/blallo/gotools/formatting"
  9. )
  10. func showExamples() {
  11. enc := json.NewEncoder(os.Stderr)
  12. for i, example := range examples {
  13. fmt.Fprintf(os.Stderr, "\nExample %d\n\nInput:\n\n", i)
  14. formatting.PrettyPrint(example)
  15. fmt.Fprintf(os.Stderr, "\nOutput:\n\n")
  16. formatting.EncodeJSON(example, enc)
  17. }
  18. }
  19. func main() {
  20. filepath := flag.String("file", "", "Path to a file of paths with descriptions")
  21. flag.Parse()
  22. var input map[string]string
  23. if *filepath != "" {
  24. filecont, err := ioutil.ReadFile(*filepath)
  25. if err != nil {
  26. panic(err)
  27. }
  28. err = json.Unmarshal(filecont, &input)
  29. if err != nil {
  30. panic(err)
  31. }
  32. } else {
  33. fmt.Fprintf(os.Stderr, "Missing input file!\n")
  34. showExamples()
  35. os.Exit(1)
  36. }
  37. enc := json.NewEncoder(os.Stdout)
  38. formatting.EncodeJSON(input, enc)
  39. }