~emersion/drmdb

987847d3395ee31371fcc226278c3df2d2fcaad7 — Simon Ser 2 years ago 7eee002
cmd/drmdump: add a mode to read JSON
1 files changed, 20 insertions(+), 11 deletions(-)

M cmd/drmdump/main.go
M cmd/drmdump/main.go => cmd/drmdump/main.go +20 -11
@@ 593,26 593,35 @@ func printNode(tf treefmt.Formatter, path string, n *drmtree.Node) {
func main() {
	var (
		outputJSON bool
		input bool
	)
	flag.BoolVar(&outputJSON, "j", false, "Enable JSON output")
	flag.BoolVar(&input, "i", false, "Read JSON data from stdin")
	flag.Parse()

	paths, err := filepath.Glob(drm.NodePatternPrimary)
	if err != nil {
		log.Fatalf("Failed to list DRM nodes: %v", err)
	}

	nodes := make(map[string]*drmtree.Node)
	for _, p := range paths {
		n, err := node(p)
	var nodes map[string]*drmtree.Node
	if input {
		if err := json.NewDecoder(os.Stdin).Decode(&nodes); err != nil {
			log.Fatalf("Failed to read JSON: %v", err)
		}
	} else {
		paths, err := filepath.Glob(drm.NodePatternPrimary)
		if err != nil {
			log.Fatal(err)
			log.Fatalf("Failed to list DRM nodes: %v", err)
		}

		nodes = make(map[string]*drmtree.Node)
		for _, p := range paths {
			n, err := node(p)
			if err != nil {
				log.Fatal(err)
			}
			nodes[p] = n
		}
		nodes[p] = n
	}

	if outputJSON {
		err = json.NewEncoder(os.Stdout).Encode(nodes)
		err := json.NewEncoder(os.Stdout).Encode(nodes)
		if err != nil {
			log.Fatalf("Failed to write JSON: %v", err)
		}