github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/bootconfig/manifest.go (about)

     1  // Copyright 2017-2019 the u-root 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 bootconfig
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  )
    11  
    12  // global variables
    13  var (
    14  	CurrentManifestVersion = 1
    15  )
    16  
    17  // Manifest is a list of BootConfig objects. The goal is to provide  multiple
    18  // configurations to choose from.
    19  type Manifest struct {
    20  	// Version is a positive integer that determines the version of the Manifest
    21  	// structure. This will be used when introducing breaking changes in the
    22  	// Manifest interface.
    23  	Version int          `json:"version"`
    24  	Configs []BootConfig `json:"configs"`
    25  }
    26  
    27  // NewManifest returns a new empty Manifest structure with the current version
    28  // field populated.
    29  func NewManifest() *Manifest {
    30  	return &Manifest{
    31  		Version: CurrentManifestVersion,
    32  	}
    33  }
    34  
    35  // ManifestFromBytes parses a manifest configuration, i.e. a list of boot
    36  // configurations, in JSON format and returns a Manifest object.
    37  func ManifestFromBytes(data []byte) (*Manifest, error) {
    38  	var manifest Manifest
    39  	if err := json.Unmarshal(data, &manifest); err != nil {
    40  		return nil, err
    41  	}
    42  	return &manifest, nil
    43  }
    44  
    45  // GetBootConfig returns the i-th boot configuration from the manifest, or an
    46  // error if an invalid index is passed.
    47  func (mc *Manifest) GetBootConfig(idx int) (*BootConfig, error) {
    48  	if idx < 0 || idx >= len(mc.Configs) {
    49  		return nil, fmt.Errorf("invalid index: not in range: %d", idx)
    50  	}
    51  	return &mc.Configs[idx], nil
    52  }