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/properties.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

88 lines
2.1 KiB
Go

package versioning
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"github.com/magiconair/properties"
"github.com/pkg/errors"
)
// PropertiesFile defines an artifact using a properties file for versioning
type PropertiesFile struct {
path string
content *properties.Properties
versioningScheme string
versionField string
writeFile func(string, []byte, os.FileMode) error
}
func (p *PropertiesFile) init() error {
if len(p.versionField) == 0 {
p.versionField = "version"
}
if p.writeFile == nil {
p.writeFile = ioutil.WriteFile
}
if p.content == nil {
var err error
p.content, err = properties.LoadFile(p.path, properties.UTF8)
if err != nil {
return errors.Wrapf(err, "failed to load file %v", p.path)
}
}
return nil
}
// VersioningScheme returns the relevant versioning scheme
func (p *PropertiesFile) VersioningScheme() string {
if len(p.versioningScheme) == 0 {
return "semver2"
}
return p.versioningScheme
}
// GetVersion returns the current version of the artifact with a ini-file-based build descriptor
func (p *PropertiesFile) GetVersion() (string, error) {
err := p.init()
if err != nil {
return "", err
}
version := p.content.GetString(p.versionField, "")
if len(version) == 0 {
return "", fmt.Errorf("no version found in field %v", p.versionField)
}
return version, nil
}
// SetVersion updates the version of the artifact with a ini-file-based build descriptor
func (p *PropertiesFile) SetVersion(version string) error {
err := p.init()
if err != nil {
return err
}
err = p.content.SetValue(p.versionField, version)
if err != nil {
return errors.Wrapf(err, "failed to set version")
}
var propsContent bytes.Buffer
_, err = p.content.Write(&propsContent, properties.UTF8)
if err != nil {
return errors.Wrap(err, "failed to write version")
}
err = p.writeFile(p.path, propsContent.Bytes(), 0666)
if err != nil {
return errors.Wrap(err, "failed to write file")
}
return nil
}
// GetCoordinates returns the coordinates
func (p *PropertiesFile) GetCoordinates() (Coordinates, error) {
result := Coordinates{}
return result, nil
}