2020-04-15 13:12:43 +02:00
|
|
|
package versioning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-05-27 17:20:34 +02:00
|
|
|
// YAMLDescriptor holds the unique identifier combination for an artifact
|
|
|
|
type YAMLDescriptor struct {
|
|
|
|
ArtifactID string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// YAMLfile defines an artifact using a yaml file for versioning
|
|
|
|
type YAMLfile struct {
|
2020-05-27 17:20:34 +02:00
|
|
|
path string
|
|
|
|
content map[string]interface{}
|
|
|
|
versionField string
|
|
|
|
artifactIDField string
|
|
|
|
readFile func(string) ([]byte, error)
|
|
|
|
writeFile func(string, []byte, os.FileMode) error
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (y *YAMLfile) init() {
|
|
|
|
if len(y.versionField) == 0 {
|
|
|
|
y.versionField = "version"
|
|
|
|
}
|
2020-05-27 17:20:34 +02:00
|
|
|
if len(y.artifactIDField) == 0 {
|
|
|
|
y.artifactIDField = "ID"
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
if y.readFile == nil {
|
|
|
|
y.readFile = ioutil.ReadFile
|
|
|
|
}
|
|
|
|
if y.writeFile == nil {
|
|
|
|
y.writeFile = ioutil.WriteFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:20:34 +02:00
|
|
|
func (y *YAMLfile) readContent() error {
|
2020-04-15 13:12:43 +02:00
|
|
|
y.init()
|
2020-05-27 17:20:34 +02:00
|
|
|
if y.content != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
content, err := y.readFile(y.path)
|
|
|
|
if err != nil {
|
2020-05-27 17:20:34 +02:00
|
|
|
return errors.Wrapf(err, "failed to read file '%v'", y.path)
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(content, &y.content)
|
|
|
|
if err != nil {
|
2020-05-27 17:20:34 +02:00
|
|
|
return errors.Wrapf(err, "failed to read yaml content of file '%v'", y.content)
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
2020-05-27 17:20:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
|
2020-05-27 17:20:34 +02:00
|
|
|
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
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:20:34 +02:00
|
|
|
// 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) {
|
2020-04-15 13:12:43 +02:00
|
|
|
y.init()
|
2020-05-27 17:20:34 +02:00
|
|
|
return y.readField(y.artifactIDField)
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
|
2020-05-27 17:20:34 +02:00
|
|
|
// 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")
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
2020-05-27 17:20:34 +02:00
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
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
|
|
|
|
}
|
2020-05-25 19:48:59 +02:00
|
|
|
|
|
|
|
// GetCoordinates returns the coordinates
|
|
|
|
func (y *YAMLfile) GetCoordinates() (Coordinates, error) {
|
2020-05-27 17:20:34 +02:00
|
|
|
result := &YAMLDescriptor{}
|
|
|
|
var err error
|
|
|
|
result.ArtifactID, err = y.GetArtifactID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Version, err = y.GetVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-25 19:48:59 +02:00
|
|
|
}
|