Skip to content

Commit 10d62a1

Browse files
committed
MINOR: add version info to binary
1 parent c2b1c89 commit 10d62a1

File tree

3 files changed

+142
-52
lines changed

3 files changed

+142
-52
lines changed

check.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ import (
99
"io"
1010
"log"
1111
"os"
12-
"path"
1312
"regexp"
1413
"strconv"
1514
"strings"
1615
"unicode"
1716
"unicode/utf8"
1817

19-
"github.com/haproxytech/check-commit/v5/aspell"
20-
2118
"github.com/google/go-github/v56/github"
2219

2320
"github.com/xanzy/go-gitlab"
@@ -542,52 +539,3 @@ func (c CommitPolicyConfig) CheckSubjectList(subjects []string) error {
542539
}
543540

544541
const requiredCmdlineArgs = 2
545-
546-
func main() {
547-
log.SetFlags(log.LstdFlags | log.Lshortfile)
548-
var repoPath string
549-
550-
if len(os.Args) < requiredCmdlineArgs {
551-
repoPath = "."
552-
} else {
553-
repoPath = os.Args[1]
554-
}
555-
556-
aspellCheck, err := aspell.New(path.Join(repoPath, ".aspell.yml"))
557-
if err != nil {
558-
log.Fatalf("error reading aspell exceptions: %s", err)
559-
}
560-
561-
commitPolicy, err := LoadCommitPolicy(path.Join(repoPath, ".check-commit.yml"))
562-
if err != nil {
563-
log.Fatalf("error reading configuration: %s", err)
564-
}
565-
566-
if commitPolicy.IsEmpty() {
567-
log.Printf("WARNING: using empty configuration (i.e. no verification)")
568-
}
569-
570-
gitEnv, err := readGitEnvironment()
571-
if err != nil {
572-
log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err)
573-
}
574-
575-
subjects, messages, content, err := getCommitData(gitEnv)
576-
if err != nil {
577-
log.Fatalf("error getting commit data: %s", err)
578-
}
579-
580-
if err := commitPolicy.CheckSubjectList(subjects); err != nil {
581-
log.Printf("encountered one or more commit message errors\n")
582-
log.Fatalf("%s\n", commitPolicy.HelpText)
583-
}
584-
585-
err = aspellCheck.Check(subjects, messages, content)
586-
if err != nil {
587-
log.Printf("encountered one or more commit message spelling errors\n")
588-
// log.Fatalf("%s\n", err)
589-
log.Fatalf("%s\n", aspellCheck.HelpText)
590-
}
591-
592-
log.Printf("check completed without errors\n")
593-
}

main.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path"
8+
9+
"github.com/haproxytech/check-commit/v5/aspell"
10+
"github.com/haproxytech/check-commit/v5/version"
11+
)
12+
13+
func main() {
14+
err := version.Set()
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
if len(os.Args) == 2 {
19+
for _, arg := range os.Args[1:] {
20+
if arg == "version" {
21+
fmt.Println("check-commit", version.Version)
22+
fmt.Println("built from:", version.Repo)
23+
fmt.Println("commit date:", version.CommitDate)
24+
os.Exit(0)
25+
}
26+
if arg == "tag" {
27+
fmt.Println(version.Tag)
28+
os.Exit(0)
29+
}
30+
}
31+
}
32+
log.SetFlags(log.LstdFlags | log.Lshortfile)
33+
var repoPath string
34+
35+
if len(os.Args) < requiredCmdlineArgs {
36+
repoPath = "."
37+
} else {
38+
repoPath = os.Args[1]
39+
}
40+
41+
aspellCheck, err := aspell.New(path.Join(repoPath, ".aspell.yml"))
42+
if err != nil {
43+
log.Fatalf("error reading aspell exceptions: %s", err)
44+
}
45+
46+
commitPolicy, err := LoadCommitPolicy(path.Join(repoPath, ".check-commit.yml"))
47+
if err != nil {
48+
log.Fatalf("error reading configuration: %s", err)
49+
}
50+
51+
if commitPolicy.IsEmpty() {
52+
log.Printf("WARNING: using empty configuration (i.e. no verification)")
53+
}
54+
55+
gitEnv, err := readGitEnvironment()
56+
if err != nil {
57+
log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err)
58+
}
59+
60+
subjects, messages, content, err := getCommitData(gitEnv)
61+
if err != nil {
62+
log.Fatalf("error getting commit data: %s", err)
63+
}
64+
65+
if err := commitPolicy.CheckSubjectList(subjects); err != nil {
66+
log.Printf("encountered one or more commit message errors\n")
67+
log.Fatalf("%s\n", commitPolicy.HelpText)
68+
}
69+
70+
err = aspellCheck.Check(subjects, messages, content)
71+
if err != nil {
72+
log.Printf("encountered one or more commit message spelling errors\n")
73+
// log.Fatalf("%s\n", err)
74+
log.Fatalf("%s\n", aspellCheck.HelpText)
75+
}
76+
77+
log.Printf("check completed without errors\n")
78+
}

version/runtime.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2019 HAProxy Technologies LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"errors"
19+
"runtime/debug"
20+
"strings"
21+
)
22+
23+
var (
24+
Repo = ""
25+
Version = "dev"
26+
Tag = "dev"
27+
CommitDate = ""
28+
)
29+
30+
var ErrBuildDataNotReadable = errors.New("not able to read build data")
31+
32+
func Set() error {
33+
buildinfo, ok := debug.ReadBuildInfo()
34+
if !ok {
35+
return ErrBuildDataNotReadable
36+
}
37+
Repo = buildinfo.Main.Path
38+
CommitDate = get(buildinfo, "vcs.time")
39+
commit := get(buildinfo, "vcs.revision")
40+
if len(commit) > 8 {
41+
commit = commit[:8]
42+
}
43+
if commit == "" {
44+
commit = "unknown"
45+
}
46+
47+
var dirty string
48+
if get(buildinfo, "vcs.modified") == "true" {
49+
dirty = ".dirty"
50+
}
51+
Version = strings.Replace(buildinfo.Main.Version, "(devel)", "dev", 1) + "." + commit + dirty
52+
Tag = strings.Replace(buildinfo.Main.Version, "(devel)", "dev", 1)
53+
54+
return nil
55+
}
56+
57+
func get(buildInfo *debug.BuildInfo, key string) string {
58+
for _, setting := range buildInfo.Settings {
59+
if setting.Key == key {
60+
return setting.Value
61+
}
62+
}
63+
return ""
64+
}

0 commit comments

Comments
 (0)