/
config.go
225 lines (208 loc) Β· 9.95 KB
/
config.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
package config
import (
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Config is a complete set of app configuration
type Config struct {
Token string
ProviderPlugin string
ProviderOpts map[string]string
CommitAnalyzerPlugin string
CommitAnalyzerOpts map[string]string
CIConditionPlugin string
CIConditionOpts map[string]string
ChangelogGeneratorPlugin string
ChangelogGeneratorOpts map[string]string
Changelog string
FilesUpdaterPlugins []string
FilesUpdaterOpts map[string]string
HooksPlugins []string
HooksOpts map[string]string
UpdateFiles []string
Match string
VersionFile bool
Prerelease bool
Ghr bool
NoCI bool
Dry bool
AllowInitialDevelopmentVersions bool
AllowNoChanges bool
ForceBumpPatchVersion bool
MaintainedVersion string
PrependChangelog bool
DownloadPlugins bool
ShowProgress bool
AllowMaintainedVersionOnDefaultBranch bool
PluginResolver string
}
func mustGetString(cmd *cobra.Command, name string) string {
res, err := cmd.Flags().GetString(name)
if err != nil {
panic(err)
}
return res
}
func mustGetStringArray(cmd *cobra.Command, name string) []string {
res, err := cmd.Flags().GetStringArray(name)
if err != nil {
panic(err)
}
return res
}
func mustGetBool(cmd *cobra.Command, name string) bool {
res, err := cmd.Flags().GetBool(name)
if err != nil {
panic(err)
}
return res
}
func mergeOpts(v map[string]string, c []string) map[string]string {
opts := make(map[string]string)
for k, v := range v {
opts[k] = v
}
for _, opt := range c {
sOpt := strings.SplitN(opt, "=", 2)
if len(sOpt) < 2 {
continue
}
opts[strings.ToLower(sOpt[0])] = sOpt[1]
}
return opts
}
func NewConfig(cmd *cobra.Command) (*Config, error) {
provOpts := mergeOpts(
viper.GetStringMapString("plugins.provider.options"),
mustGetStringArray(cmd, "provider-opt"))
caOpts := mergeOpts(
viper.GetStringMapString("plugins.commit-analyzer.options"),
mustGetStringArray(cmd, "commit-analyzer-opt"))
ciOpts := mergeOpts(
viper.GetStringMapString("plugins.ci-condition.options"),
mustGetStringArray(cmd, "ci-condition-opt"))
cgOpts := mergeOpts(
viper.GetStringMapString("plugins.changelog-generator.options"),
mustGetStringArray(cmd, "changelog-generator-opt"))
fuOpts := mergeOpts(
viper.GetStringMapString("plugins.files-updater.options"),
mustGetStringArray(cmd, "files-updater-opt"))
hoOpts := mergeOpts(
viper.GetStringMapString("plugins.hooks.options"),
mustGetStringArray(cmd, "hooks-opt"))
conf := &Config{
Token: mustGetString(cmd, "token"),
ProviderPlugin: viper.GetString("plugins.provider.name"),
ProviderOpts: provOpts,
CommitAnalyzerPlugin: viper.GetString("plugins.commit-analyzer.name"),
CommitAnalyzerOpts: caOpts,
CIConditionPlugin: viper.GetString("plugins.ci-condition.name"),
CIConditionOpts: ciOpts,
ChangelogGeneratorPlugin: viper.GetString("plugins.changelog-generator.name"),
ChangelogGeneratorOpts: cgOpts,
Changelog: mustGetString(cmd, "changelog"),
FilesUpdaterPlugins: viper.GetStringSlice("plugins.files-updater.names"),
FilesUpdaterOpts: fuOpts,
HooksPlugins: viper.GetStringSlice("plugins.hooks.names"),
HooksOpts: hoOpts,
UpdateFiles: mustGetStringArray(cmd, "update"),
Match: mustGetString(cmd, "match"),
VersionFile: mustGetBool(cmd, "version-file"),
Prerelease: mustGetBool(cmd, "prerelease"),
Ghr: mustGetBool(cmd, "ghr"),
NoCI: mustGetBool(cmd, "no-ci"),
Dry: mustGetBool(cmd, "dry"),
AllowInitialDevelopmentVersions: mustGetBool(cmd, "allow-initial-development-versions"),
AllowNoChanges: mustGetBool(cmd, "allow-no-changes"),
ForceBumpPatchVersion: mustGetBool(cmd, "force-bump-patch-version"),
MaintainedVersion: viper.GetString("maintainedVersion"),
PrependChangelog: mustGetBool(cmd, "prepend-changelog"),
DownloadPlugins: mustGetBool(cmd, "download-plugins"),
ShowProgress: mustGetBool(cmd, "show-progress"),
AllowMaintainedVersionOnDefaultBranch: mustGetBool(cmd, "allow-maintained-version-on-default-branch"),
PluginResolver: viper.GetString("pluginResolver"),
}
return conf, nil
}
func must(err error) {
if err != nil {
panic(err)
}
}
func detectCI() string {
if os.Getenv("GITHUB_ACTIONS") == "true" {
return "github"
}
if os.Getenv("GITLAB_CI") == "true" {
return "gitlab"
}
return "default"
}
func defaultProvider() string {
if os.Getenv("GITLAB_CI") == "true" {
return "gitlab"
}
return "github"
}
func SetFlags(cmd *cobra.Command) {
cmd.Flags().StringP("token", "t", "", "provider token")
cmd.Flags().String("provider", defaultProvider(), "provider plugin name")
cmd.Flags().StringArray("provider-opt", []string{}, "options that are passed to the provider plugin")
cmd.Flags().String("commit-analyzer", "default", "commit-analyzer plugin name")
cmd.Flags().StringArray("commit-analyzer-opt", []string{}, "options that are passed to the commit-analyzer plugin")
cmd.Flags().String("ci-condition", detectCI(), "ci-condition plugin name")
cmd.Flags().StringArray("ci-condition-opt", []string{}, "options that are passed to the ci-condition plugin")
cmd.Flags().String("changelog-generator", "default", "changelog-generator plugin name")
cmd.Flags().StringArray("changelog-generator-opt", []string{}, "options that are passed to the changelog-generator plugin")
cmd.Flags().String("changelog", "", "creates a changelog file")
cmd.Flags().StringSlice("files-updater", []string{"npm"}, "files-updater plugin names")
cmd.Flags().StringArray("files-updater-opt", []string{}, "options that are passed to the files-updater plugins")
cmd.Flags().StringSlice("hooks", []string{}, "hooks plugin names")
cmd.Flags().StringArray("hooks-opt", []string{}, "options that are passed to the hooks plugins")
cmd.Flags().StringArrayP("update", "u", []string{}, "updates the version of a certain files")
cmd.Flags().String("match", "", "only consider tags matching the given glob(7) pattern, excluding the \"refs/tags/\" prefix.")
cmd.Flags().String("maintained-version", "", "set the maintained version as base for new releases")
cmd.Flags().BoolP("version-file", "f", false, "create a .version file with the new version")
cmd.Flags().Bool("prerelease", false, "flags the release as a prerelease")
cmd.Flags().Bool("ghr", false, "create a .ghr file with the parameters for ghr")
cmd.Flags().Bool("no-ci", false, "run semantic-release locally")
cmd.Flags().Bool("dry", false, "do not create release")
cmd.Flags().Bool("allow-initial-development-versions", false, "semantic-release will start your initial development release at 0.1.0 and will handle breaking changes as minor version updates. This option will be ignored if a release with major version greater than or equal 1 exists.")
cmd.Flags().Bool("allow-no-changes", false, "exit with code 0 if no changes are found, useful if semantic-release is automatically run")
cmd.Flags().Bool("force-bump-patch-version", false, "increments the patch version if no changes are found")
cmd.Flags().Bool("prepend-changelog", false, "if the changelog file already exist the new changelog is prepended")
cmd.Flags().Bool("download-plugins", false, "downloads all required plugins if needed")
cmd.Flags().Bool("show-progress", false, "shows the plugin download progress")
cmd.Flags().String("config", "", "config file (default is .semrelrc)")
cmd.Flags().Bool("allow-maintained-version-on-default-branch", false, "allow configuring the maintained version on the default branch")
cmd.Flags().String("plugin-resolver", "registry", "which resolver should be used to resolve plugins (registry or github)")
cmd.Flags().SortFlags = true
must(viper.BindPFlag("maintainedVersion", cmd.Flags().Lookup("maintained-version")))
must(viper.BindEnv("maintainedVersion", "MAINTAINED_VERSION"))
must(viper.BindPFlag("plugins.provider.name", cmd.Flags().Lookup("provider")))
must(viper.BindPFlag("plugins.commit-analyzer.name", cmd.Flags().Lookup("commit-analyzer")))
must(viper.BindPFlag("plugins.ci-condition.name", cmd.Flags().Lookup("ci-condition")))
must(viper.BindPFlag("plugins.changelog-generator.name", cmd.Flags().Lookup("changelog-generator")))
must(viper.BindPFlag("plugins.files-updater.names", cmd.Flags().Lookup("files-updater")))
must(viper.BindPFlag("plugins.hooks.names", cmd.Flags().Lookup("hooks")))
must(viper.BindPFlag("pluginResolver", cmd.Flags().Lookup("plugin-resolver")))
must(viper.BindEnv("pluginResolver", "SEMREL_PLUGIN_RESOLVER"))
}
func InitConfig(cmd *cobra.Command) error {
cfgFile := mustGetString(cmd, "config")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath(".")
viper.SetConfigName(".semrelrc")
}
viper.SetConfigType("json")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
return nil
}