Skip to content

Commit c72c9cb

Browse files
authored
feat: add tag.output to manage the tag output (#228)
* feat: add tag.strip-prefix bool * tag.strip-prefix is required for mono repo structures if I need to run ```shell svu n --tag.prefix foo/v --tag.pattern 'foo/v*' --log.directory foo > foo/.version svu n --tag.prefix bla/v --tag.pattern 'bla/v*' --log.directory bla > bla/.version export BLA_NEXT=$(cat bla/.version) export FOO_NEXT=$(cat foo/.version) docker tag foo foo:${FOO_NEXT} docker tag bla bla:${BLA_NEXT} ``` I do not want FOO and BLA _NEXT to have the prefix * added --config to be able to have multiple config files to make monorepo usage easier * chore: address comments from PR * chore: remove StripPrefix flag as it isnt needed * chore: address lint issues
1 parent dc2e412 commit c72c9cb

4 files changed

Lines changed: 67 additions & 29 deletions

File tree

example.svu.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
#
33
# https://github.com/caarlos0/svu
44
verbose: false
5-
tag.pattern: ""
6-
tag.prefix: "v"
7-
tag.mode: all
8-
log.directory:
9-
- "."
5+
tag:
6+
pattern: ""
7+
prefix: "v"
8+
mode: all
9+
log:
10+
directory:
11+
- "."
1012
metadata: ""
1113
always: false
1214
v0: false

internal/svu/svu.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ var (
3333
)
3434

3535
type Options struct {
36-
Ctx context.Context
37-
Action Action
38-
Pattern string
39-
Prefix string
40-
PreRelease string
41-
Metadata string
42-
Directories []string
43-
TagMode string
44-
Always bool
45-
KeepV0 bool
46-
JSON bool
36+
Ctx context.Context
37+
Action Action
38+
Pattern string
39+
Prefix string
40+
PrefixOutput string
41+
PreRelease string
42+
Metadata string
43+
TagMode string
44+
ConfigRoot string
45+
Directories []string
46+
Always bool
47+
KeepV0 bool
48+
JSON bool
4749
}
4850

4951
type VersionInfo struct {

main.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"log"
99
"os"
10+
"path"
1011
"strings"
1112

1213
goversion "github.com/caarlos0/go-version"
@@ -26,6 +27,7 @@ var examples []byte
2627

2728
func main() {
2829
var verbose bool
30+
var configFile string
2931
var opts svu.Options
3032

3133
runFunc := func(cmd *cobra.Command) error {
@@ -57,6 +59,10 @@ func main() {
5759
)
5860
}
5961

62+
if opts.PrefixOutput == "^tag.prefix^" {
63+
opts.PrefixOutput = opts.Prefix
64+
}
65+
6066
if verbose {
6167
log.SetFlags(0)
6268
} else {
@@ -124,12 +130,13 @@ func main() {
124130
Short: "Creates a svu configuration file",
125131
Aliases: []string{"i"},
126132
RunE: func(_ *cobra.Command, _ []string) error {
127-
return os.WriteFile(".svu.yaml", exampleConfig, 0o644)
133+
return os.WriteFile(configFile, exampleConfig, 0o644)
128134
},
129135
}
130136

131137
rootCmd.SetVersionTemplate("{{.Version}}")
132138
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "enable logs")
139+
rootCmd.PersistentFlags().StringVar(&configFile, "config", ".svu.yml", "set config file")
133140
rootCmd.AddCommand(initCmd)
134141
nextCmd.Flags().BoolVar(&opts.Always, "always", false, "if no commits trigger a version change, increment the patch")
135142
nextCmd.Flags().BoolVar(&opts.KeepV0, "v0", false, "prevent major version increments if current version is still v0")
@@ -146,6 +153,7 @@ func main() {
146153
cmd.Flags().BoolVar(&opts.JSON, "json", false, "output version as json")
147154
cmd.Flags().StringVar(&opts.Pattern, "tag.pattern", "", "ignore tags that do not match the given pattern")
148155
cmd.Flags().StringVar(&opts.Prefix, "tag.prefix", "v", "sets a tag custom prefix")
156+
cmd.Flags().StringVar(&opts.PrefixOutput, "tag.output", "^tag.prefix^", "set the tag output to use when printing the version (default: 'tag.prefix')")
149157
cmd.Flags().StringVar(&opts.TagMode, "tag.mode", git.TagModeAll, "determine if it should look for tags in all branches, or just the current one")
150158
cmd.Flags().StringVar(&opts.PreRelease, "prerelease", "", "sets the version prerelease")
151159
cmd.Flags().StringVar(&opts.Metadata, "metadata", "", "sets the version metadata")
@@ -158,18 +166,20 @@ func main() {
158166
} {
159167
cmd.Flags().StringSliceVar(&opts.Directories, "log.directory", nil, "only use commits that changed files in the given directories")
160168
}
161-
162-
home, _ := os.UserHomeDir()
163-
config, _ := os.UserConfigDir()
164-
viper.AutomaticEnv()
165-
viper.SetEnvPrefix("svu")
166-
viper.AddConfigPath(".")
167-
viper.AddConfigPath(git.Root(context.Background()))
168-
viper.AddConfigPath(config)
169-
viper.AddConfigPath(home)
170-
viper.SetConfigName(".svu")
171-
viper.SetConfigType("yaml")
172169
cobra.OnInitialize(func() {
170+
home, _ := os.UserHomeDir()
171+
config, _ := os.UserConfigDir()
172+
cfgPath := path.Dir(configFile)
173+
configFile = path.Base(configFile)
174+
opts.ConfigRoot = cfgPath
175+
viper.AutomaticEnv()
176+
viper.SetEnvPrefix("svu")
177+
viper.AddConfigPath(cfgPath)
178+
viper.AddConfigPath(git.Root(context.Background()))
179+
viper.AddConfigPath(config)
180+
viper.AddConfigPath(home)
181+
viper.SetConfigType("yaml")
182+
viper.SetConfigName(configFile)
173183
if viper.ReadInConfig() == nil {
174184
presetRequiredFlags(rootCmd)
175185
}
@@ -189,7 +199,15 @@ func presetRequiredFlags(cmd *cobra.Command) {
189199
_ = viper.BindPFlags(cmd.Flags())
190200
cmd.Flags().VisitAll(func(f *pflag.Flag) {
191201
if viper.IsSet(f.Name) {
192-
_ = cmd.Flags().Set(f.Name, viper.GetString(f.Name))
202+
switch f.Name {
203+
case "log.directory":
204+
dirs := viper.GetStringSlice(f.Name)
205+
for _, dir := range dirs {
206+
_ = cmd.Flags().Set(f.Name, dir)
207+
}
208+
default:
209+
_ = cmd.Flags().Set(f.Name, viper.GetString(f.Name))
210+
}
193211
}
194212
})
195213
for _, scmd := range cmd.Commands() {

mono-repo-example.svu.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# svu configuration.
2+
#
3+
# https://github.com/caarlos0/svu
4+
verbose: false
5+
tag:
6+
pattern: "project-name/v*"
7+
prefix: "projet-name/v"
8+
#if you want the output to be without the prefix, set output to "" it defaults to tag.prefix
9+
#output: ""
10+
mode: all
11+
log:
12+
directory:
13+
- "project-name"
14+
metadata: ""
15+
always: false
16+
v0: false

0 commit comments

Comments
 (0)