Skip to content

Commit

Permalink
MINOR: add version info to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
oktalz committed Aug 12, 2024
1 parent c2b1c89 commit 8684402
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .aspell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ allowed:
- cmdline
- sed
- runnumber
- args
- LLC
- devel
3 changes: 3 additions & 0 deletions aspell/aspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var (
"sed": {},
"stdin": {},
"args": {},
"arg": {},
"dev": {},
"vcs": {},
}
badWordsGlobal = map[string]struct{}{}
)
Expand Down
52 changes: 0 additions & 52 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import (
"io"
"log"
"os"
"path"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"

"github.com/haproxytech/check-commit/v5/aspell"

"github.com/google/go-github/v56/github"

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

const requiredCmdlineArgs = 2

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
var repoPath string

if len(os.Args) < requiredCmdlineArgs {
repoPath = "."
} else {
repoPath = os.Args[1]
}

aspellCheck, err := aspell.New(path.Join(repoPath, ".aspell.yml"))
if err != nil {
log.Fatalf("error reading aspell exceptions: %s", err)
}

commitPolicy, err := LoadCommitPolicy(path.Join(repoPath, ".check-commit.yml"))
if err != nil {
log.Fatalf("error reading configuration: %s", err)
}

if commitPolicy.IsEmpty() {
log.Printf("WARNING: using empty configuration (i.e. no verification)")
}

gitEnv, err := readGitEnvironment()
if err != nil {
log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err)
}

subjects, messages, content, err := getCommitData(gitEnv)
if err != nil {
log.Fatalf("error getting commit data: %s", err)
}

if err := commitPolicy.CheckSubjectList(subjects); err != nil {
log.Printf("encountered one or more commit message errors\n")
log.Fatalf("%s\n", commitPolicy.HelpText)
}

err = aspellCheck.Check(subjects, messages, content)
if err != nil {
log.Printf("encountered one or more commit message spelling errors\n")
// log.Fatalf("%s\n", err)
log.Fatalf("%s\n", aspellCheck.HelpText)
}

log.Printf("check completed without errors\n")
}
78 changes: 78 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"fmt"
"log"
"os"
"path"

"github.com/haproxytech/check-commit/v5/aspell"
"github.com/haproxytech/check-commit/v5/version"
)

func main() {
err := version.Set()
if err != nil {
log.Fatal(err)
}
if len(os.Args) == 2 {
for _, arg := range os.Args[1:] {
if arg == "version" {
fmt.Println("check-commit", version.Version)
fmt.Println("built from:", version.Repo)
fmt.Println("commit date:", version.CommitDate)
os.Exit(0)
}
if arg == "tag" {
fmt.Println(version.Tag)
os.Exit(0)
}
}
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
var repoPath string

if len(os.Args) < requiredCmdlineArgs {
repoPath = "."
} else {
repoPath = os.Args[1]
}

aspellCheck, err := aspell.New(path.Join(repoPath, ".aspell.yml"))
if err != nil {
log.Fatalf("error reading aspell exceptions: %s", err)
}

commitPolicy, err := LoadCommitPolicy(path.Join(repoPath, ".check-commit.yml"))
if err != nil {
log.Fatalf("error reading configuration: %s", err)
}

if commitPolicy.IsEmpty() {
log.Printf("WARNING: using empty configuration (i.e. no verification)")
}

gitEnv, err := readGitEnvironment()
if err != nil {
log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err)
}

subjects, messages, content, err := getCommitData(gitEnv)
if err != nil {
log.Fatalf("error getting commit data: %s", err)
}

if err := commitPolicy.CheckSubjectList(subjects); err != nil {
log.Printf("encountered one or more commit message errors\n")
log.Fatalf("%s\n", commitPolicy.HelpText)
}

err = aspellCheck.Check(subjects, messages, content)
if err != nil {
log.Printf("encountered one or more commit message spelling errors\n")
// log.Fatalf("%s\n", err)
log.Fatalf("%s\n", aspellCheck.HelpText)
}

log.Printf("check completed without errors\n")
}
64 changes: 64 additions & 0 deletions version/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
"errors"
"runtime/debug"
"strings"
)

var (
Repo = ""
Version = "dev"
Tag = "dev"
CommitDate = ""
)

var ErrBuildDataNotReadable = errors.New("not able to read build data")

func Set() error {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return ErrBuildDataNotReadable
}
Repo = buildInfo.Main.Path
CommitDate = get(buildInfo, "vcs.time")
commit := get(buildInfo, "vcs.revision")
if len(commit) > 8 {
commit = commit[:8]
}
if commit == "" {
commit = "unknown"
}

var dirty string
if get(buildInfo, "vcs.modified") == "true" {
dirty = ".dirty"
}
Version = strings.Replace(buildInfo.Main.Version, "(devel)", "dev", 1) + "." + commit + dirty
Tag = strings.Replace(buildInfo.Main.Version, "(devel)", "dev", 1)

return nil
}

func get(buildInfo *debug.BuildInfo, key string) string {
for _, setting := range buildInfo.Settings {
if setting.Key == key {
return setting.Value
}
}
return ""
}

0 comments on commit 8684402

Please sign in to comment.