github.com/jaylevin/jenkins-library@v1.230.4/pkg/versioning/versionfile.go (about) 1 package versioning 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 8 "github.com/pkg/errors" 9 ) 10 11 // Versionfile defines an artifact containing the version in a file, e.g. VERSION 12 type Versionfile struct { 13 path string 14 readFile func(string) ([]byte, error) 15 writeFile func(string, []byte, os.FileMode) error 16 versioningScheme string 17 } 18 19 func (v *Versionfile) init() { 20 if len(v.path) == 0 { 21 v.path = "VERSION" 22 } 23 if v.readFile == nil { 24 v.readFile = ioutil.ReadFile 25 } 26 27 if v.writeFile == nil { 28 v.writeFile = ioutil.WriteFile 29 } 30 } 31 32 // VersioningScheme returns the relevant versioning scheme 33 func (v *Versionfile) VersioningScheme() string { 34 if len(v.versioningScheme) == 0 { 35 return "semver2" 36 } 37 return v.versioningScheme 38 } 39 40 // GetVersion returns the current version of the artifact 41 func (v *Versionfile) GetVersion() (string, error) { 42 v.init() 43 44 content, err := v.readFile(v.path) 45 if err != nil { 46 return "", errors.Wrapf(err, "failed to read file '%v'", v.path) 47 } 48 49 return strings.TrimSpace(string(content)), nil 50 } 51 52 // SetVersion updates the version of the artifact 53 func (v *Versionfile) SetVersion(version string) error { 54 v.init() 55 56 err := v.writeFile(v.path, []byte(version), 0700) 57 if err != nil { 58 return errors.Wrapf(err, "failed to write file '%v'", v.path) 59 } 60 61 return nil 62 } 63 64 // GetCoordinates returns the coordinates 65 func (v *Versionfile) GetCoordinates() (Coordinates, error) { 66 result := Coordinates{} 67 return result, nil 68 }