go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/roller-configurator/jiri/manifest.go (about)

     1  // Copyright 2023 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  // Package jiri contains jiri manifest schema definitions copied from Jiri's
     6  // source code.
     7  //
     8  // Only fields relevant to roller configuration are included, others are
     9  // omitted.
    10  package jiri
    11  
    12  import (
    13  	"encoding/xml"
    14  	"errors"
    15  	"fmt"
    16  	"io"
    17  	"os"
    18  )
    19  
    20  type Manifest struct {
    21  	Projects []Project `xml:"projects>project"`
    22  	Packages []Package `xml:"packages>package"`
    23  	XMLName  struct{}  `xml:"manifest"`
    24  }
    25  
    26  // Project represents a jiri project.
    27  type Project struct {
    28  	// Name is the project name.
    29  	Name string `xml:"name,attr,omitempty"`
    30  
    31  	// Remote is the project remote.
    32  	Remote string `xml:"remote,attr,omitempty"`
    33  
    34  	// RemoteBranch is the name of the remote branch to track.
    35  	RemoteBranch string `xml:"remotebranch,attr,omitempty"`
    36  }
    37  
    38  // Package struct represents the <package> tag in manifest files.
    39  type Package struct {
    40  	// Name represents the remote cipd path of the package.
    41  	Name string `xml:"name,attr"`
    42  
    43  	// Version represents the version tag of the cipd package.
    44  	Version string `xml:"version,attr"`
    45  }
    46  
    47  // LoadManifest reads a Jiri manifest from disk.
    48  func LoadManifest(path string) (*Manifest, error) {
    49  	b, err := os.ReadFile(path)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	var manifest Manifest
    54  	if err := xml.Unmarshal(b, &manifest); err != nil {
    55  		if errors.Is(err, io.EOF) {
    56  			return nil, fmt.Errorf("invalid XML in %s", path)
    57  		}
    58  		return nil, err
    59  	}
    60  	return &manifest, nil
    61  }