github.com/linuxboot/fiano@v1.2.0/pkg/visitors/json.go (about)

     1  // Copyright 2018 the LinuxBoot Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package visitors
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  
    13  	"github.com/linuxboot/fiano/pkg/uefi"
    14  )
    15  
    16  // JSON prints any Firmware node as JSON.
    17  type JSON struct {
    18  	// JSON is written to this writer.
    19  	W io.Writer
    20  }
    21  
    22  // Run wraps Visit and performs some setup and teardown tasks.
    23  func (v *JSON) Run(f uefi.Firmware) error {
    24  	return f.Apply(v)
    25  }
    26  
    27  // Visit applies the JSON visitor to any Firmware type.
    28  func (v *JSON) Visit(f uefi.Firmware) error {
    29  	b, err := json.MarshalIndent(f, "", "\t")
    30  	if err != nil {
    31  		return err
    32  	}
    33  	fmt.Fprintln(v.W, string(b))
    34  	return nil
    35  }
    36  
    37  func init() {
    38  	RegisterCLI("json", "produce JSON for the full firmware volume", 0, func(args []string) (uefi.Visitor, error) {
    39  		return &JSON{
    40  			W: os.Stdout,
    41  		}, nil
    42  	})
    43  }