• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

a1y-developer / doc-formatter / 20685185169

04 Jan 2026 12:40AM UTC coverage: 84.097%. First build
20685185169

Pull #30

github

web-flow
Merge 640add189 into 22dfaab32
Pull Request #30: ci: goreleaser

277 of 340 new or added lines in 5 files covered. (81.47%)

1560 of 1855 relevant lines covered (84.1%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.59
/pkg/version/types.go
1
//go:generate go run scripts/gen/gen.go -o z_update_version.go
2
//go:generate go fmt
3

4
package version
5

6
import (
7
        "encoding/json"
8
        "fmt"
9
        "runtime"
10
        "runtime/debug"
11
        "time"
12

13
        goversion "github.com/hashicorp/go-version"
14
        "gopkg.in/yaml.v3"
15

16
        git "github.com/a1y/doc-formatter/pkg/util/gitutil"
17
)
18

19
// refer to https://github.com/KusionStack/kusion/blob/main/pkg/version/types.go
20
var info = NewMainOrDefaultVersionInfo()
21

22
func NewMainOrDefaultVersionInfo() *Info {
1✔
23
        v := NewDefaultVersionInfo()
1✔
24

1✔
25
        if i, ok := debug.ReadBuildInfo(); ok {
2✔
26
                mod := &i.Main
1✔
27
                if mod.Replace != nil {
1✔
NEW
28
                        mod = mod.Replace
×
NEW
29
                }
×
30

31
                if mod.Version != "(devel)" && mod.Version != "" {
1✔
NEW
32
                        v.ReleaseVersion = mod.Version
×
NEW
33
                }
×
34
        }
35

36
        return v
1✔
37
}
38

39
func NewDefaultVersionInfo() *Info {
1✔
40
        return &Info{
1✔
41
                ReleaseVersion: "default-version",
1✔
42
                GitInfo: &GitInfo{
1✔
43
                        LatestTag: "",
1✔
44
                        Commit:    "",
1✔
45
                        TreeState: "",
1✔
46
                },
1✔
47
                BuildInfo: &BuildInfo{
1✔
48
                        GoVersion: runtime.Version(),
1✔
49
                        GOOS:      runtime.GOOS,
1✔
50
                        GOARCH:    runtime.GOARCH,
1✔
51
                        NumCPU:    numCPU(),
1✔
52
                        Compiler:  runtime.Compiler,
1✔
53
                        BuildTime: time.Now().Format("2006-01-02 15:04:05"),
1✔
54
                },
1✔
55
        }
1✔
56
}
1✔
57

58
// Info contains versioning information.
59
// following attributes:
60
//
61
//        ReleaseVersion - "X.Y.Z-00000000" used to indicate the last release version,
62
//        containing GitVersion and GitCommitShort.
63
type Info struct {
64
        ReleaseVersion string     `json:"releaseVersion" yaml:"releaseVersion"` // Such as "1.2.3-3836f877"
65
        GitInfo        *GitInfo   `json:"gitInfo,omitempty" yaml:"gitInfo,omitempty"`
66
        BuildInfo      *BuildInfo `json:"buildInfo,omitempty" yaml:"buildInfo,omitempty"`
67
}
68

69
// GitInfo contains git information.
70
// following attributes:
71
//
72
//        LatestTag - "vX.Y.Z" used to indicate the last git tag.
73
//        Commit - The git commit id corresponding to this source code.
74
//        TreeState - "clean" indicates no changes since the git commit id,
75
//        "dirty" indicates source code changes after the git commit id
76
type GitInfo struct {
77
        LatestTag string `json:"latestTag,omitempty" yaml:"latestTag,omitempty"` // Such as "v1.2.3"
78
        Commit    string `json:"commit,omitempty" yaml:"commit,omitempty"`       // Such as "3836f8770ab8f488356b2129f42f2ae5c1134bb0"
79
        TreeState string `json:"treeState,omitempty" yaml:"treeState,omitempty"` // Such as "clean", "dirty"
80
}
81

82
type BuildInfo struct {
83
        GoVersion string `json:"goVersion,omitempty" yaml:"goVersion,omitempty"`
84
        GOOS      string `json:"GOOS,omitempty" yaml:"GOOS,omitempty"`
85
        GOARCH    string `json:"GOARCH,omitempty" yaml:"GOARCH,omitempty"`
86
        NumCPU    int    `json:"numCPU,omitempty" yaml:"numCPU,omitempty"`
87
        Compiler  string `json:"compiler,omitempty" yaml:"compiler,omitempty"`
88
        BuildTime string `json:"buildTime,omitempty" yaml:"buildTime,omitempty"` // Such as "2021-10-20 18:24:03"
89
}
90

91
func NewInfo() (*Info, error) {
1✔
92
        var (
1✔
93
                isHeadAtTag    bool
1✔
94
                headHash       string
1✔
95
                headHashShort  string
1✔
96
                latestTag      string
1✔
97
                gitVersion     *goversion.Version
1✔
98
                releaseVersion string
1✔
99
                isDirty        bool
1✔
100
                gitTreeState   string
1✔
101
                err            error
1✔
102
        )
1✔
103

1✔
104
        // Get git info
1✔
105
        if headHash, err = git.GetHeadHash(); err != nil {
2✔
106
                return nil, err
1✔
107
        }
1✔
108

109
        if headHashShort, err = git.GetHeadHashShort(); err != nil {
2✔
110
                return nil, err
1✔
111
        }
1✔
112

113
        if latestTag, err = git.GetLatestTag(); err != nil {
2✔
114
                return nil, err
1✔
115
        }
1✔
116

117
        if gitVersion, err = goversion.NewVersion(latestTag); err != nil {
2✔
118
                return nil, err
1✔
119
        }
1✔
120

121
        if isHeadAtTag, err = git.IsHeadAtTag(latestTag); err != nil {
2✔
122
                return nil, err
1✔
123
        }
1✔
124

125
        if isDirty, err = git.IsDirty(); err != nil {
2✔
126
                return nil, err
1✔
127
        }
1✔
128

129
        // Get git tree state
130
        if isDirty {
1✔
NEW
131
                gitTreeState = "dirty"
×
132
        } else {
1✔
133
                gitTreeState = "clean"
1✔
134
        }
1✔
135

136
        // Get release version
137
        if isHeadAtTag {
2✔
138
                releaseVersion = gitVersion.Original()[1:]
1✔
139
        } else {
2✔
140
                releaseVersion = fmt.Sprintf("%s+%s", gitVersion.Original()[1:], headHashShort)
1✔
141
        }
1✔
142

143
        return &Info{
1✔
144
                ReleaseVersion: releaseVersion,
1✔
145
                GitInfo: &GitInfo{
1✔
146
                        LatestTag: gitVersion.Original(),
1✔
147
                        Commit:    headHash,
1✔
148
                        TreeState: gitTreeState,
1✔
149
                },
1✔
150
                BuildInfo: &BuildInfo{
1✔
151
                        GoVersion: runtime.Version(),
1✔
152
                        GOOS:      runtime.GOOS,
1✔
153
                        GOARCH:    runtime.GOARCH,
1✔
154
                        NumCPU:    numCPU(),
1✔
155
                        Compiler:  runtime.Compiler,
1✔
156
                        BuildTime: time.Now().Format("2006-01-02 15:04:05"),
1✔
157
                },
1✔
158
        }, nil
1✔
159
}
160

161
func (v *Info) String() string {
1✔
162
        return v.YAML()
1✔
163
}
1✔
164

165
func (v *Info) JSON() string {
1✔
166
        data, err := json.MarshalIndent(v, "", "    ")
1✔
167
        if err != nil {
1✔
NEW
168
                return ""
×
NEW
169
        }
×
170

171
        return string(data)
1✔
172
}
173

174
func (v *Info) YAML() string {
1✔
175
        data, err := yaml.Marshal(v)
1✔
176
        if err != nil {
1✔
NEW
177
                return ""
×
NEW
178
        }
×
179

180
        return string(data)
1✔
181
}
182

183
// numCpu returns runtime.NumCPU() due to the function line flaw of "bytedance/mockey"
184
// refer to https://github.com/bytedance/mockey
185
func numCPU() int {
1✔
186
        cpu := runtime.NumCPU()
1✔
187
        return cpu
1✔
188
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc