github.com/SAP/jenkins-library@v1.362.0/pkg/versioning/yamlfile.go (about)

     1  package versioning
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/ghodss/yaml"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // YAMLDescriptor holds the unique identifier combination for an artifact
    13  type YAMLDescriptor struct {
    14  	GroupID    string
    15  	ArtifactID string
    16  	Version    string
    17  }
    18  
    19  // YAMLfile defines an artifact using a yaml file for versioning
    20  type YAMLfile struct {
    21  	path            string
    22  	content         map[string]interface{}
    23  	versionField    string
    24  	artifactIDField string
    25  	readFile        func(string) ([]byte, error)
    26  	writeFile       func(string, []byte, os.FileMode) error
    27  }
    28  
    29  func (y *YAMLfile) init() {
    30  	if len(y.versionField) == 0 {
    31  		y.versionField = "version"
    32  	}
    33  	if len(y.artifactIDField) == 0 {
    34  		y.artifactIDField = "ID"
    35  	}
    36  	if y.readFile == nil {
    37  		y.readFile = os.ReadFile
    38  	}
    39  	if y.writeFile == nil {
    40  		y.writeFile = os.WriteFile
    41  	}
    42  }
    43  
    44  func (y *YAMLfile) readContent() error {
    45  	y.init()
    46  	if y.content != nil {
    47  		return nil
    48  	}
    49  	content, err := y.readFile(y.path)
    50  	if err != nil {
    51  		return errors.Wrapf(err, "failed to read file '%v'", y.path)
    52  	}
    53  	err = yaml.Unmarshal(content, &y.content)
    54  	if err != nil {
    55  		return errors.Wrapf(err, "failed to read yaml content of file '%v'", y.content)
    56  	}
    57  	return nil
    58  }
    59  
    60  func (y *YAMLfile) readField(key string) (string, error) {
    61  	err := y.readContent()
    62  	if err != nil {
    63  		return "", errors.Wrapf(err, "failed to get key %s", key)
    64  	}
    65  	return strings.TrimSpace(fmt.Sprint(y.content[key])), nil
    66  }
    67  
    68  // VersioningScheme returns the relevant versioning scheme
    69  func (y *YAMLfile) VersioningScheme() string {
    70  	return "semver2"
    71  }
    72  
    73  // GetArtifactID returns the current ID of the artifact
    74  func (y *YAMLfile) GetArtifactID() (string, error) {
    75  	y.init()
    76  	return y.readField(y.artifactIDField)
    77  }
    78  
    79  // GetVersion returns the current version of the artifact with a YAML-based build descriptor
    80  func (y *YAMLfile) GetVersion() (string, error) {
    81  	y.init()
    82  	return y.readField(y.versionField)
    83  }
    84  
    85  // SetVersion updates the version of the artifact with a YAML-based build descriptor
    86  func (y *YAMLfile) SetVersion(version string) error {
    87  	err := y.readContent()
    88  	if err != nil {
    89  		return errors.Wrapf(err, "failed to set version")
    90  	}
    91  
    92  	y.content[y.versionField] = version
    93  
    94  	content, err := yaml.Marshal(y.content)
    95  	if err != nil {
    96  		return errors.Wrapf(err, "failed to create yaml content for '%v'", y.path)
    97  	}
    98  	err = y.writeFile(y.path, content, 0700)
    99  	if err != nil {
   100  		return errors.Wrapf(err, "failed to write file '%v'", y.path)
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  // GetCoordinates returns the coordinates
   107  func (y *YAMLfile) GetCoordinates() (Coordinates, error) {
   108  	result := Coordinates{}
   109  	var err error
   110  	result.ArtifactID, err = y.GetArtifactID()
   111  	if err != nil {
   112  		return result, err
   113  	}
   114  	result.Version, err = y.GetVersion()
   115  	if err != nil {
   116  		return result, err
   117  	}
   118  	return result, nil
   119  }