This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_info_read.go
109 lines (83 loc) · 2.02 KB
/
version_info_read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ver
import (
"fmt"
"io/ioutil"
"os"
log "github.com/Sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
const (
VersionInfoYamlFilename = "version-info.yml"
)
type versionInfoYAML struct {
Version string `yaml:"version"`
Product struct {
Name string `yaml:"name"`
Description string `yaml:"desc"`
License string `yaml:"license"`
} `yaml:"product"`
Author struct {
Name string `yaml:"name"`
Email string `yaml:"email"`
Website string `yaml:"url"`
} `yaml:"author"`
}
func TryReadFromVersionInfoYAML(maxDepth int) (*VersionInformation, error) {
// gather version information
path := VersionInfoYamlFilename
var found []*VersionInformation
for i := 0; i <= maxDepth; i++ {
vii, err := tryReadVersionInfoYAMLInternal(path)
if err != nil {
return nil, err
}
if vii == nil {
break
}
log.Debugf("Found version information: %v", path)
found = append([]*VersionInformation{vii}, found...)
path = fmt.Sprintf("../%v", path)
}
// nothing found?
if len(found) <= 0 {
return nil, nil
}
// merge the results
vi := MakeVersionInformation()
for _, vii := range found {
vi.CopyMissingFrom(vii)
}
return vi, nil
}
func tryReadVersionInfoYAMLInternal(filename string) (*VersionInformation, error) {
// check if the file exists
if _, err := os.Stat(filename); err != nil {
log.Debugf("No %v found", filename)
return nil, nil
}
// read the file
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Errorf("Failed to read %v file: %v", filename, err)
return nil, err
}
// parse the file
viy := versionInfoYAML{}
err = yaml.Unmarshal(data, &viy)
if err != nil {
log.Errorf("Failed to parse %v file: %v", filename, err)
return nil, err
}
// copy data
vi := MakeVersionInformation()
if viy.Version != "" {
vi.SetSemVersion(viy.Version)
}
vi.Author = viy.Author.Name
vi.Email = viy.Author.Email
vi.Website = viy.Author.Website
vi.Name = viy.Product.Name
vi.Description = viy.Product.Description
vi.License = viy.Product.License
return vi, nil
}