diff --git a/.aspell.yml b/.aspell.yml index c9113aa..1ce77de 100644 --- a/.aspell.yml +++ b/.aspell.yml @@ -26,4 +26,5 @@ allowed: - cmdline - sed - runnumber - - args + - LLC + - devel diff --git a/aspell/aspell.go b/aspell/aspell.go index 88fe48f..47cb205 100644 --- a/aspell/aspell.go +++ b/aspell/aspell.go @@ -42,6 +42,9 @@ var ( "sed": {}, "stdin": {}, "args": {}, + "arg": {}, + "dev": {}, + "vcs": {}, } badWordsGlobal = map[string]struct{}{} ) diff --git a/check.go b/check.go index 463985e..4392c99 100644 --- a/check.go +++ b/check.go @@ -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" @@ -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") -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..43469b1 --- /dev/null +++ b/main.go @@ -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") +} diff --git a/version/runtime.go b/version/runtime.go new file mode 100644 index 0000000..e64e9cc --- /dev/null +++ b/version/runtime.go @@ -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 "" +}