github.com/linuxboot/fiano@v1.2.0/pkg/visitors/save.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  	"os"
     9  
    10  	"github.com/linuxboot/fiano/pkg/uefi"
    11  )
    12  
    13  // Save calls Assemble, then outputs the top image to a file.
    14  type Save struct {
    15  	DirPath string
    16  }
    17  
    18  // Run just applies the visitor.
    19  func (v *Save) Run(f uefi.Firmware) error {
    20  	return f.Apply(v)
    21  }
    22  
    23  // Visit calls the assemble visitor to make sure everything is reconstructed.
    24  // It then outputs the top level buffer to a file.
    25  func (v *Save) Visit(f uefi.Firmware) error {
    26  	a := &Assemble{}
    27  	// Assemble the binary to make sure the top level buffer is correct
    28  	if err := f.Apply(a); err != nil {
    29  		return err
    30  	}
    31  	return os.WriteFile(v.DirPath, f.Buf(), 0666)
    32  }
    33  
    34  func init() {
    35  	RegisterCLI("save", "assemble a firmware volume from a directory tree", 1, func(args []string) (uefi.Visitor, error) {
    36  		return &Save{
    37  			DirPath: args[0],
    38  		}, nil
    39  	})
    40  }