|
| 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 | +} |
0 commit comments