github.com/linuxboot/fiano@v1.2.0/cmds/guid2english/main.go (about)

     1  // Copyright 2019 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  // guid2english replace GUIDs with their English representation.
     6  //
     7  // Synopsis:
     8  //     guid2english [-t TEMPLATE] [FILE]
     9  //
    10  // Options:
    11  //     -t TEMPLATE:
    12  //         A template used to replace GUIDS. The template can refer to the
    13  //         following variables:
    14  //             * {{.Guid}}: The GUID being mapped
    15  //             * {{.Name}}: The English name of the GUID or "UNKNOWN"
    16  //             * {{.IsKnown}}: Set to true when the English name is not known
    17  //         The default template is "{{.GUID}} ({{.Name}})".
    18  //
    19  // Description:
    20  //     If FILE is not specified, stdin is used.
    21  package main
    22  
    23  import (
    24  	"flag"
    25  	"io"
    26  	"os"
    27  	"text/template"
    28  
    29  	"github.com/linuxboot/fiano/pkg/guid2english"
    30  	"github.com/linuxboot/fiano/pkg/log"
    31  	"golang.org/x/text/transform"
    32  )
    33  
    34  var (
    35  	tmpl = flag.String("t", "{{.GUID}} ({{.Name}})", "template string")
    36  )
    37  
    38  func main() {
    39  	flag.Parse()
    40  	r := os.Stdin
    41  	switch flag.NArg() {
    42  	case 0:
    43  	case 1:
    44  		var err error
    45  		r, err = os.Open(flag.Arg(0))
    46  		if err != nil {
    47  			log.Fatalf("Error opening file: %v", err)
    48  		}
    49  		defer r.Close()
    50  	default:
    51  		log.Fatalf("At most 1 positional arguments expected")
    52  	}
    53  
    54  	t, err := template.New("guid2english").Parse(*tmpl)
    55  	if err != nil {
    56  		log.Fatalf("Template not valid: %v", err)
    57  	}
    58  
    59  	trans := guid2english.New(guid2english.NewTemplateMapper(t))
    60  
    61  	_, err = io.Copy(os.Stdout, transform.NewReader(r, trans))
    62  	if err != nil {
    63  		log.Fatalf("Error copying buffer: %v", err)
    64  	}
    65  }