forked from aevea/commitsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (109 loc) · 3.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"os"
"github.com/aevea/commitsar/config"
"github.com/aevea/commitsar/internal/version_runner"
"github.com/logrusorgru/aurora"
"github.com/aevea/commitsar/internal/root_runner"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
)
// version is a global variable passed during build time
var version string
// commit is a global variable passed during build time. Should be used if version is not available.
var commit string
// date is a global variable passed during build time
var date string
func runRoot(cmd *cobra.Command, args []string) error {
if viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
}
runner := root_runner.New()
commitConfig := config.CommitConfig()
commitConfig.Path = "."
if len(args) > 0 {
commitConfig.Path = args[0]
}
return runner.Run(commitConfig, args...)
}
func bindRootFlags(rootCmd *cobra.Command) error {
rootCmd.Flags().BoolP("verbose", "v", false, "verbose output")
err := viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose"))
if err != nil {
return err
}
rootCmd.Flags().BoolP("strict", "s", true, "strict mode")
err = viper.BindPFlag("commits.strict", rootCmd.Flags().Lookup("strict"))
if err != nil {
return err
}
rootCmd.Flags().BoolP("all", "a", false, "iterate through all the commits on the branch")
err = viper.BindPFlag("commits.all", rootCmd.Flags().Lookup("all"))
if err != nil {
return err
}
rootCmd.Flags().StringSlice("required-scopes", nil, "forces scope to match one of the provided values")
err = viper.BindPFlag("commits.required-scopes", rootCmd.Flags().Lookup("required-scopes"))
if err != nil {
return err
}
// Not used. TODO: Documentation
rootCmd.Flags().StringP("path", "d", ".", "dir points to the path of the repository")
err = viper.BindPFlag("path", rootCmd.Flags().Lookup("path"))
if err != nil {
return err
}
return nil
}
func main() {
log.SetHandler(cli.Default)
if err := config.LoadConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}
var rootCmd = &cobra.Command{
Use: "commitsar <from?>...<to>",
Short: "Checks if commits comply",
Long: "Checks if commits comply with conventional commits",
RunE: runRoot,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MinimumNArgs(0),
}
if err := bindRootFlags(rootCmd); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Version returns undefined if not on a tag. This needs to reset it.
if version == "undefined" {
version = ""
}
if version == "" && commit != "" {
version = commit
}
if version == "" && commit == "" {
version = "development"
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Commitsar",
Long: `All software has versions. This is Commitsars.`,
RunE: func(cmd *cobra.Command, args []string) error {
err := version_runner.Run(
version_runner.VersionInfo{
Version: version,
Date: date,
},
)
return err
},
}
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(aurora.Red(err))
os.Exit(1)
}
}