2020-04-15 13:12:43 +02:00
|
|
|
package versioning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
2020-11-06 10:20:08 +02:00
|
|
|
// INIfile defines an artifact using a ini file for versioning
|
2020-04-15 13:12:43 +02:00
|
|
|
type INIfile struct {
|
2020-04-24 20:52:16 +02:00
|
|
|
path string
|
|
|
|
content *ini.File
|
|
|
|
versioningScheme string
|
|
|
|
versionSection string
|
|
|
|
versionField string
|
|
|
|
readFile func(string) ([]byte, error)
|
|
|
|
writeFile func(string, []byte, os.FileMode) error
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *INIfile) init() error {
|
|
|
|
if len(i.versionField) == 0 {
|
|
|
|
i.versionField = "version"
|
|
|
|
}
|
|
|
|
if i.readFile == nil {
|
|
|
|
i.readFile = ioutil.ReadFile
|
|
|
|
}
|
|
|
|
if i.writeFile == nil {
|
|
|
|
i.writeFile = ioutil.WriteFile
|
|
|
|
}
|
|
|
|
if i.content == nil {
|
|
|
|
conf, err := i.readFile(i.path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read file '%v'", i.path)
|
|
|
|
}
|
|
|
|
i.content, err = ini.Load(conf)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to load content from file '%v'", i.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersioningScheme returns the relevant versioning scheme
|
|
|
|
func (i *INIfile) VersioningScheme() string {
|
2020-04-24 20:52:16 +02:00
|
|
|
if len(i.versioningScheme) == 0 {
|
|
|
|
return "semver2"
|
|
|
|
}
|
|
|
|
return i.versioningScheme
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersion returns the current version of the artifact with a ini-file-based build descriptor
|
|
|
|
func (i *INIfile) GetVersion() (string, error) {
|
|
|
|
if i.content == nil {
|
|
|
|
err := i.init()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
section := i.content.Section(i.versionSection)
|
|
|
|
if section.HasKey(i.versionField) {
|
|
|
|
return section.Key(i.versionField).String(), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("field '%v' not found in section '%v'", i.versionField, i.versionSection)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVersion updates the version of the artifact with a ini-file-based build descriptor
|
|
|
|
func (i *INIfile) SetVersion(version string) error {
|
|
|
|
if i.content == nil {
|
|
|
|
err := i.init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
section := i.content.Section(i.versionSection)
|
|
|
|
section.Key(i.versionField).SetValue(version)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
i.content.WriteTo(&buf)
|
|
|
|
err := i.writeFile(i.path, buf.Bytes(), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to write file '%v'", i.path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-25 19:48:59 +02:00
|
|
|
|
|
|
|
// GetCoordinates returns the coordinates
|
|
|
|
func (i *INIfile) GetCoordinates() (Coordinates, error) {
|
2021-02-10 17:18:00 +02:00
|
|
|
return Coordinates{}, nil
|
2020-05-25 19:48:59 +02:00
|
|
|
}
|