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
106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package versioning
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
|
|
"golang.org/x/mod/modfile"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// GoMod utility to interact with Go Modules specific versioning
|
|
type GoMod struct {
|
|
path string
|
|
readFile func(string) ([]byte, error)
|
|
writeFile func(string, []byte, os.FileMode) error
|
|
fileExists func(string) (bool, error)
|
|
buildDescriptorContent string
|
|
}
|
|
|
|
func (m *GoMod) init() error {
|
|
if m.readFile == nil {
|
|
m.readFile = ioutil.ReadFile
|
|
}
|
|
if m.writeFile == nil {
|
|
m.writeFile = ioutil.WriteFile
|
|
}
|
|
if len(m.buildDescriptorContent) == 0 {
|
|
content, err := m.readFile(m.path)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to read file '%v'", m.path)
|
|
}
|
|
m.buildDescriptorContent = string(content)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetVersion returns the go.mod descriptor version property
|
|
func (m *GoMod) GetVersion() (string, error) {
|
|
buildDescriptorFilePath := m.path
|
|
var err error
|
|
if strings.Contains(m.path, "go.mod") {
|
|
buildDescriptorFilePath, err = searchDescriptor([]string{"version.txt", "VERSION"}, m.fileExists)
|
|
if err != nil {
|
|
err = m.init()
|
|
if err != nil {
|
|
return "", errors.Wrapf(err, "failed to read file '%v'", m.path)
|
|
}
|
|
|
|
parsed, err := modfile.Parse(m.path, []byte(m.buildDescriptorContent), nil)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to parse go.mod file")
|
|
}
|
|
if parsed.Module.Mod.Version != "" {
|
|
return parsed.Module.Mod.Version, nil
|
|
}
|
|
|
|
return "", errors.Wrap(err, "failed to retrieve version")
|
|
}
|
|
}
|
|
artifact := &Versionfile{
|
|
path: buildDescriptorFilePath,
|
|
versioningScheme: m.VersioningScheme(),
|
|
}
|
|
return artifact.GetVersion()
|
|
}
|
|
|
|
// SetVersion sets the go.mod descriptor version property
|
|
func (m *GoMod) SetVersion(v string) error {
|
|
return nil
|
|
}
|
|
|
|
// VersioningScheme returns the relevant versioning scheme
|
|
func (m *GoMod) VersioningScheme() string {
|
|
return "semver2"
|
|
}
|
|
|
|
// GetCoordinates returns the go.mod build descriptor coordinates
|
|
func (m *GoMod) GetCoordinates() (Coordinates, error) {
|
|
result := Coordinates{}
|
|
err := m.init()
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
parsed, err := modfile.Parse(m.path, []byte(m.buildDescriptorContent), nil)
|
|
if err != nil {
|
|
return result, errors.Wrap(err, "failed to parse go.mod file")
|
|
}
|
|
|
|
if parsed.Module == nil {
|
|
return result, errors.Wrap(err, "failed to parse go.mod file")
|
|
}
|
|
if parsed.Module.Mod.Path != "" {
|
|
artifactSplit := strings.Split(parsed.Module.Mod.Path, "/")
|
|
artifactID := artifactSplit[len(artifactSplit)-1]
|
|
result.ArtifactID = artifactID
|
|
}
|
|
result.Version = parsed.Module.Mod.Version
|
|
if result.Version == "" {
|
|
result.Version = "unspecified"
|
|
}
|
|
return result, nil
|
|
}
|