Skip to content

Commit d2e64ae

Browse files
soohoowoohoochristophwitzko
authored andcommitted
feat: add option to specify config path
1 parent 7a0d1f2 commit d2e64ae

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

cmd/semantic-release/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ func main() {
4949
Version: SRVERSION,
5050
}
5151

52-
err := config.InitConfig(cmd)
53-
if err != nil {
54-
fmt.Printf("\nConfig error: %s\n", err.Error())
55-
os.Exit(1)
56-
return
57-
}
58-
err = cmd.Execute()
52+
config.SetFlags(cmd)
53+
cobra.OnInitialize(func() {
54+
err := config.InitConfig(cmd)
55+
if err != nil {
56+
fmt.Printf("\nConfig error: %s\n", err.Error())
57+
os.Exit(1)
58+
return
59+
}
60+
})
61+
err := cmd.Execute()
5962
if err != nil {
6063
fmt.Printf("\n%s\n", err.Error())
6164
os.Exit(1)

pkg/config/config.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func defaultProvider() string {
154154
return "github"
155155
}
156156

157-
func InitConfig(cmd *cobra.Command) error {
157+
func SetFlags(cmd *cobra.Command) {
158158
cmd.Flags().StringP("token", "t", "", "provider token")
159159
cmd.Flags().String("provider", defaultProvider(), "provider plugin name")
160160
cmd.Flags().StringArray("provider-opt", []string{}, "options that are passed to the provider plugin")
@@ -183,12 +183,9 @@ func InitConfig(cmd *cobra.Command) error {
183183
cmd.Flags().Bool("prepend-changelog", false, "if the changelog file already exist the new changelog is prepended")
184184
cmd.Flags().Bool("download-plugins", false, "downloads all required plugins if needed")
185185
cmd.Flags().Bool("show-progress", false, "shows the plugin download progress")
186+
cmd.Flags().String("config", "", "config file (default is .semrelrc)")
186187
cmd.Flags().SortFlags = true
187188

188-
viper.AddConfigPath(".")
189-
viper.SetConfigName(".semrelrc")
190-
viper.SetConfigType("json")
191-
192189
must(viper.BindPFlag("maintainedVersion", cmd.Flags().Lookup("maintained-version")))
193190
must(viper.BindEnv("maintainedVersion", "MAINTAINED_VERSION"))
194191

@@ -198,6 +195,17 @@ func InitConfig(cmd *cobra.Command) error {
198195
must(viper.BindPFlag("plugins.changelog-generator.name", cmd.Flags().Lookup("changelog-generator")))
199196
must(viper.BindPFlag("plugins.files-updater.names", cmd.Flags().Lookup("files-updater")))
200197
must(viper.BindPFlag("plugins.hooks.names", cmd.Flags().Lookup("hooks")))
198+
}
199+
200+
func InitConfig(cmd *cobra.Command) error {
201+
cfgFile := mustGetString(cmd, "config")
202+
if cfgFile != "" {
203+
viper.SetConfigFile(cfgFile)
204+
} else {
205+
viper.AddConfigPath(".")
206+
viper.SetConfigName(".semrelrc")
207+
}
208+
viper.SetConfigType("json")
201209

202210
if err := viper.ReadInConfig(); err != nil {
203211
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {

0 commit comments

Comments
 (0)