-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmain.go
236 lines (216 loc) · 5.73 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
_ "embed"
"fmt"
"io"
"log"
"os"
"strings"
goversion "github.com/caarlos0/go-version"
"github.com/caarlos0/svu/v3/internal/git"
"github.com/caarlos0/svu/v3/internal/svu"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
//go:embed description.txt
var description []byte
//go:embed examples.sh
var examples []byte
func main() {
var verbose bool
var opts svu.Options
runFunc := func(cmd *cobra.Command) error {
version, err := svu.Version(opts)
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), version)
return err
}
rootCmd := &cobra.Command{
Use: "svu",
Short: "Semantic Version Utility",
Long: string(description),
Version: buildVersion(version, commit, date, builtBy).String(),
Example: paddingLeft(string(examples)),
SilenceUsage: true,
PersistentPreRunE: func(*cobra.Command, []string) error {
switch opts.TagMode {
case git.TagModeAll, git.TagModeCurrent:
default:
return fmt.Errorf(
"invalid tag-mode: %q: valid options are %q and %q",
opts.TagMode,
git.TagModeCurrent,
git.TagModeAll,
)
}
if verbose {
log.SetFlags(0)
} else {
log.SetOutput(io.Discard)
}
return nil
},
}
prereleaseCmd := &cobra.Command{
Use: "prerelease",
Aliases: []string{"pr"},
Short: "Increases the build portion of the prerelease",
RunE: func(cmd *cobra.Command, args []string) error {
opts.Action = svu.PreRelease
return runFunc(cmd)
},
}
nextCmd := &cobra.Command{
Use: "next",
Aliases: []string{"n"},
Short: "Next version based on git history",
RunE: func(cmd *cobra.Command, args []string) error {
opts.Action = svu.Next
return runFunc(cmd)
},
}
majorCmd := &cobra.Command{
Use: "major",
Short: "New major release",
RunE: func(cmd *cobra.Command, args []string) error {
opts.Action = svu.Major
return runFunc(cmd)
},
}
minorCmd := &cobra.Command{
Use: "minor",
Short: "New minor release",
Aliases: []string{"m"},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Action = svu.Minor
return runFunc(cmd)
},
}
patchCmd := &cobra.Command{
Use: "patch",
Short: "New patch release",
Aliases: []string{"p"},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Action = svu.Patch
return runFunc(cmd)
},
}
currentCmd := &cobra.Command{
Use: "current",
Short: "Current version",
Aliases: []string{"c"},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Action = svu.Current
return runFunc(cmd)
},
}
initCmd := &cobra.Command{
Use: "init",
Short: "Creates a svu configuration file",
Aliases: []string{"i"},
RunE: func(cmd *cobra.Command, args []string) error {
return os.WriteFile(".svu.yaml", exampleConfig, 0o644)
},
}
rootCmd.SetVersionTemplate("{{.Version}}")
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "enable logs")
rootCmd.AddCommand(initCmd)
nextCmd.Flags().BoolVar(&opts.Always, "always", false, "if no commits trigger a version change, increment the patch")
nextCmd.Flags().BoolVar(&opts.KeepV0, "v0", false, "prevent major version increments if current version is still v0")
for _, cmd := range []*cobra.Command{
nextCmd,
majorCmd,
minorCmd,
patchCmd,
currentCmd,
prereleaseCmd,
} {
// init does not share these flags.
cmd.Flags().StringVar(&opts.Pattern, "tag.pattern", "", "ignore tags that do not match the given pattern")
cmd.Flags().StringVar(&opts.Prefix, "tag.prefix", "v", "sets a tag custom prefix")
cmd.Flags().StringVar(&opts.TagMode, "tag.mode", git.TagModeAll, "determine if it should look for tags in all branches, or just the current one")
cmd.Flags().StringVar(&opts.PreRelease, "prerelease", "", "sets the version prerelease")
cmd.Flags().StringVar(&opts.Metadata, "metadata", "", "sets the version metadata")
rootCmd.AddCommand(cmd)
}
for _, cmd := range []*cobra.Command{
nextCmd,
prereleaseCmd,
} {
cmd.Flags().StringSliceVar(&opts.Directories, "log.directory", nil, "only use commits that changed files in the given directories")
}
home, _ := os.UserHomeDir()
config, _ := os.UserConfigDir()
viper.AutomaticEnv()
viper.SetEnvPrefix("svu")
viper.AddConfigPath(".")
viper.AddConfigPath(git.Root())
viper.AddConfigPath(config)
viper.AddConfigPath(home)
viper.SetConfigName(".svu")
viper.SetConfigType("yaml")
cobra.OnInitialize(func() {
if viper.ReadInConfig() == nil {
presetRequiredFlags(rootCmd)
}
})
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func presetRequiredFlags(cmd *cobra.Command) {
viper.BindPFlags(cmd.Flags())
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if viper.IsSet(f.Name) {
cmd.Flags().Set(f.Name, viper.GetString(f.Name))
}
})
for _, scmd := range cmd.Commands() {
presetRequiredFlags(scmd)
}
}
// nolint: gochecknoglobals
var (
version = ""
commit = ""
date = ""
builtBy = ""
treeState = ""
)
//go:embed art.txt
var asciiArt string
//go:embed example.svu.yaml
var exampleConfig []byte
func buildVersion(version, commit, date, builtBy string) goversion.Info {
return goversion.GetVersionInfo(
goversion.WithAppDetails("svu", "Semantic Version Utility", "https://github.com/caarlos0/svu"),
goversion.WithASCIIName(asciiArt),
func(i *goversion.Info) {
if commit != "" {
i.GitCommit = commit
}
if treeState != "" {
i.GitTreeState = treeState
}
if date != "" {
i.BuildDate = date
}
if version != "" {
i.GitVersion = version
}
if builtBy != "" {
i.BuiltBy = builtBy
}
},
)
}
func paddingLeft(in string) string {
var out []string
for _, line := range strings.Split(in, "\n") {
out = append(out, " "+line)
}
return strings.Join(out, "\n")
}