1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/versioning/versionfile.go
Oliver Nocon d47a17c8fc
feat(whitesource): consolidated reporting and versioning alignment (#2571)
* 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
2021-02-10 16:18:00 +01:00

69 lines
1.4 KiB
Go

package versioning
import (
"io/ioutil"
"os"
"strings"
"github.com/pkg/errors"
)
// Versionfile defines an artifact containing the version in a file, e.g. VERSION
type Versionfile struct {
path string
readFile func(string) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
versioningScheme string
}
func (v *Versionfile) init() {
if len(v.path) == 0 {
v.path = "VERSION"
}
if v.readFile == nil {
v.readFile = ioutil.ReadFile
}
if v.writeFile == nil {
v.writeFile = ioutil.WriteFile
}
}
// VersioningScheme returns the relevant versioning scheme
func (v *Versionfile) VersioningScheme() string {
if len(v.versioningScheme) == 0 {
return "semver2"
}
return v.versioningScheme
}
// GetVersion returns the current version of the artifact
func (v *Versionfile) GetVersion() (string, error) {
v.init()
content, err := v.readFile(v.path)
if err != nil {
return "", errors.Wrapf(err, "failed to read file '%v'", v.path)
}
return strings.TrimSpace(string(content)), nil
}
// SetVersion updates the version of the artifact
func (v *Versionfile) SetVersion(version string) error {
v.init()
err := v.writeFile(v.path, []byte(version), 0700)
if err != nil {
return errors.Wrapf(err, "failed to write file '%v'", v.path)
}
return nil
}
// GetCoordinates returns the coordinates
func (v *Versionfile) GetCoordinates() (Coordinates, error) {
result := Coordinates{}
return result, nil
}