mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
d47a17c8fc
* update reporting and add todo comments * enhance reporting, allow directory creation for reports * properly pass reports * update templating and increase verbosity of errors * add todo * add detail table * update sorting * add test and improve error message * fix error message in test * extend tests * enhance tests * enhance versioning behavior accoring to #1846 * create markdown overview report * small fix * fix small issue * make sure that report directory exists * align reporting directory with default directory from UA * add missing comments * add policy check incl. tests * enhance logging and tests * update versioning to allow custom version usage properly * fix report paths and golang image * update styling of md * update test
121 lines
2.8 KiB
Go
121 lines
2.8 KiB
Go
package versioning
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/ghodss/yaml"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// YAMLDescriptor holds the unique identifier combination for an artifact
|
|
type YAMLDescriptor struct {
|
|
GroupID string
|
|
ArtifactID string
|
|
Version string
|
|
}
|
|
|
|
// YAMLfile defines an artifact using a yaml file for versioning
|
|
type YAMLfile struct {
|
|
path string
|
|
content map[string]interface{}
|
|
versionField string
|
|
artifactIDField string
|
|
readFile func(string) ([]byte, error)
|
|
writeFile func(string, []byte, os.FileMode) error
|
|
}
|
|
|
|
func (y *YAMLfile) init() {
|
|
if len(y.versionField) == 0 {
|
|
y.versionField = "version"
|
|
}
|
|
if len(y.artifactIDField) == 0 {
|
|
y.artifactIDField = "ID"
|
|
}
|
|
if y.readFile == nil {
|
|
y.readFile = ioutil.ReadFile
|
|
}
|
|
if y.writeFile == nil {
|
|
y.writeFile = ioutil.WriteFile
|
|
}
|
|
}
|
|
|
|
func (y *YAMLfile) readContent() error {
|
|
y.init()
|
|
if y.content != nil {
|
|
return nil
|
|
}
|
|
content, err := y.readFile(y.path)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to read file '%v'", y.path)
|
|
}
|
|
err = yaml.Unmarshal(content, &y.content)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to read yaml content of file '%v'", y.content)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (y *YAMLfile) readField(key string) (string, error) {
|
|
err := y.readContent()
|
|
if err != nil {
|
|
return "", errors.Wrapf(err, "failed to get key %s", key)
|
|
}
|
|
return strings.TrimSpace(fmt.Sprint(y.content[key])), nil
|
|
}
|
|
|
|
// VersioningScheme returns the relevant versioning scheme
|
|
func (y *YAMLfile) VersioningScheme() string {
|
|
return "semver2"
|
|
}
|
|
|
|
// GetArtifactID returns the current ID of the artifact
|
|
func (y *YAMLfile) GetArtifactID() (string, error) {
|
|
y.init()
|
|
return y.readField(y.artifactIDField)
|
|
}
|
|
|
|
// GetVersion returns the current version of the artifact with a YAML-based build descriptor
|
|
func (y *YAMLfile) GetVersion() (string, error) {
|
|
y.init()
|
|
return y.readField(y.versionField)
|
|
}
|
|
|
|
// SetVersion updates the version of the artifact with a YAML-based build descriptor
|
|
func (y *YAMLfile) SetVersion(version string) error {
|
|
err := y.readContent()
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to set version")
|
|
}
|
|
|
|
y.content[y.versionField] = version
|
|
|
|
content, err := yaml.Marshal(y.content)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to create yaml content for '%v'", y.path)
|
|
}
|
|
err = y.writeFile(y.path, content, 0700)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to write file '%v'", y.path)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetCoordinates returns the coordinates
|
|
func (y *YAMLfile) GetCoordinates() (Coordinates, error) {
|
|
result := Coordinates{}
|
|
var err error
|
|
result.ArtifactID, err = y.GetArtifactID()
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
result.Version, err = y.GetVersion()
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
return result, nil
|
|
}
|