examples/cmd/examples2json/examples2json.go

41 lines
899 B
Go
Raw Normal View History

2022-04-16 20:46:35 +02:00
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
2022-04-23 15:01:17 +02:00
"git.kotmisia.pl/Mm2PL/examples"
2022-04-16 20:46:35 +02:00
)
func main() {
files := flag.String("files", "", "Comma separated list of files to convert to JSON.")
flag.Parse()
if *files == "" {
fmt.Fprintf(os.Stderr, "The -files flag is required.")
return
}
2022-04-23 15:01:17 +02:00
var allExamples []examples.Example
2022-04-16 20:46:35 +02:00
for _, file := range strings.Split(*files, ",") {
file = strings.Trim(file, " ")
f, err := os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open %s for reading: %s\n", file, err)
continue
}
2022-04-23 15:01:17 +02:00
exs, err := examples.ReadFile(f)
2022-04-16 20:46:35 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse %s: %s\n", file, err)
continue
}
2022-04-23 15:01:17 +02:00
allExamples = append(allExamples, exs...)
2022-04-16 20:46:35 +02:00
}
err := json.NewEncoder(os.Stdout).Encode(&allExamples)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to encode JSON: %s\n", err)
return
}
}