From c7b0c8c7e086a48b6bc4abfc5fb3d2923d0d8fb3 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 09:52:26 +0100 Subject: [PATCH 01/11] added some docs to ldflags package --- internal/ldflags/definitions.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/ldflags/definitions.go b/internal/ldflags/definitions.go index 0401ee7..6a74624 100644 --- a/internal/ldflags/definitions.go +++ b/internal/ldflags/definitions.go @@ -1,6 +1,7 @@ package ldflags +// -X key=value ldflags definitions, injected at build time. var ( - // Version The version of the semverbot binary, injected at build time. + // Version The release version of the sbot binary. Version string = "dev" ) From 3cde4c175f63facc5e69ee7a22d657fa6aa228de Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 09:52:38 +0100 Subject: [PATCH 02/11] rename --- internal/ldflags/{definitions.go => x.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename internal/ldflags/{definitions.go => x.go} (100%) diff --git a/internal/ldflags/definitions.go b/internal/ldflags/x.go similarity index 100% rename from internal/ldflags/definitions.go rename to internal/ldflags/x.go From b4c0352385675727a56ce1ec03dbfbf32006a3e1 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 10:54:47 +0100 Subject: [PATCH 03/11] changed DefaultGitCommitDelimiters from just "[]" to "[]/" just in case someone deletes a branch straight away after merging, this way the merge pull request can still be used to recognize the branch which is usually mentioned in the commit message --- internal/constants.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/constants.go b/internal/constants.go index 6024cc9..ac59ec6 100644 --- a/internal/constants.go +++ b/internal/constants.go @@ -10,7 +10,7 @@ const ( DefaultGitBranchDelimiters = "/" // DefaultGitCommitDelimiters the default delimiters used by the git-commit mode. - DefaultGitCommitDelimiters = "[]" + DefaultGitCommitDelimiters = "[]/" // DefaultGitTagsPrefix the default prefix prepended to git tags. DefaultGitTagsPrefix = "v" From d40bf269f72b1583aa810855ee73032a1f09db19 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 11:01:04 +0100 Subject: [PATCH 04/11] updated readme for previous commit --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aaa3f6e..8717bab 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ major = ["release"] delimiters = "/" [modes.git-commit] -delimiters = "[]" +delimiters = "[]/" ``` ## Configuration properties @@ -224,7 +224,9 @@ A string of delimiters which are used to split a git commit message. e.g. delimiters `"[]"` will split `[feature] some-feature` into `["feature", " some-feature"]`, and the `feature` and ` some-feature` strings will be matched against semver map values. -Defaults to `"[]"` due to its popular use in git commit messages. +Defaults to `"[]/"` due to their popular use in git commit messages. The "/" character is often used in pull request +commit messages on GitHub, GitLab and Bitbucket. If somehow the branch name recognition +fails, the merge commit message is used as backup. ## Examples From 65b0c2f1b914fbd8b6fabad99de3341dada343d8 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 12:54:52 +0100 Subject: [PATCH 05/11] added default additional config file paths - fixed redundant endline in default config template --- pkg/cli/defaults.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/cli/defaults.go b/pkg/cli/defaults.go index bc13b08..2c8db0e 100644 --- a/pkg/cli/defaults.go +++ b/pkg/cli/defaults.go @@ -7,6 +7,9 @@ import ( ) var ( + // DefaultAdditionalConfigFilePaths additional default relative filepaths to the config file. + DefaultAdditionalConfigFilePaths = []string{".sbot.toml", ".sbot/config.toml"} + // DefaultConfigFilePath the default relative filepath to the config file. DefaultConfigFilePath = internal.DefaultConfigFilePath @@ -50,7 +53,6 @@ delimiters = "%s" [modes.git-commit] delimiters = "%s" - ` return fmt.Sprintf( From 063c15cb63249d52353f7b6a5a66d901490871db Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 12:55:40 +0100 Subject: [PATCH 06/11] updated .semverbot.toml --- .semverbot.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.semverbot.toml b/.semverbot.toml index 73428ab..7973699 100644 --- a/.semverbot.toml +++ b/.semverbot.toml @@ -20,4 +20,4 @@ major = ["release"] delimiters = "/" [modes.git-commit] -delimiters = "[]" +delimiters = "[]/" From a4f6ff453078027d8118ff68754c530ddf613850 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 12:58:14 +0100 Subject: [PATCH 07/11] added .semverbot/config.toml to default additional config filepaths --- pkg/cli/defaults.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cli/defaults.go b/pkg/cli/defaults.go index 2c8db0e..ca0fd61 100644 --- a/pkg/cli/defaults.go +++ b/pkg/cli/defaults.go @@ -8,7 +8,7 @@ import ( var ( // DefaultAdditionalConfigFilePaths additional default relative filepaths to the config file. - DefaultAdditionalConfigFilePaths = []string{".sbot.toml", ".sbot/config.toml"} + DefaultAdditionalConfigFilePaths = []string{".sbot.toml", ".semverbot/config.toml", ".sbot/config.toml"} // DefaultConfigFilePath the default relative filepath to the config file. DefaultConfigFilePath = internal.DefaultConfigFilePath From 52b5a0d2732d0a138c2d39a66ea38c41c6480659 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 13:04:02 +0100 Subject: [PATCH 08/11] added ext/viperx package which contains code built on top of viper - implemented function to load a config from a path --- pkg/ext/viperx/viper.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 pkg/ext/viperx/viper.go diff --git a/pkg/ext/viperx/viper.go b/pkg/ext/viperx/viper.go new file mode 100644 index 0000000..5401d06 --- /dev/null +++ b/pkg/ext/viperx/viper.go @@ -0,0 +1,21 @@ +package viperx + +import ( + "path/filepath" + "strings" + + "github.com/rs/zerolog/log" + "github.com/spf13/viper" +) + +// LoadConfig loads a configuration file. +// Returns an error if it fails. +func LoadConfig(path string) (err error) { + viper.AddConfigPath(filepath.Dir(path)) + viper.SetConfigName(strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))) + viper.SetConfigType(strings.Split(filepath.Ext(path), ".")[1]) + + log.Debug().Str("path", path).Msg("loading config file...") + + return viper.ReadInConfig() +} From f6740cdfa989d37906b13b67eb1d2362f61c3939 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 13:12:08 +0100 Subject: [PATCH 09/11] implemented attempts to read additional config files in case config flag and/or default config cannot be read - fine tuned cmd.SilenceUsage and cmd.SilenceErrors disable statements - renamed LoadDefaultConfig to LoadDefaultConfigValues - renamed LoadConfig to LoadConfigFile --- pkg/cli/commands/root.go | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/pkg/cli/commands/root.go b/pkg/cli/commands/root.go index 0941ebb..563b09e 100644 --- a/pkg/cli/commands/root.go +++ b/pkg/cli/commands/root.go @@ -3,7 +3,6 @@ package commands import ( "errors" "os" - "path/filepath" "strings" "github.com/rs/zerolog" @@ -13,6 +12,7 @@ import ( "github.com/restechnica/semverbot/pkg/cli" v1 "github.com/restechnica/semverbot/pkg/cli/commands/v1" + "github.com/restechnica/semverbot/pkg/ext/viperx" "github.com/restechnica/semverbot/pkg/git" "github.com/restechnica/semverbot/pkg/semver" ) @@ -49,19 +49,18 @@ func NewRootCommand() *cobra.Command { // RootCommandPersistentPreRunE runs before the command and any subcommand runs. // Returns an error if it failed. func RootCommandPersistentPreRunE(cmd *cobra.Command, args []string) (err error) { - // silence usage and errors because errors at this point are unrelated to CLI usage errors - cmd.SilenceErrors = true + // silence usage output on error because errors at this point are unrelated to CLI usage cmd.SilenceUsage = true ConfigureLogging() log.Debug().Str("command", "root").Msg("starting pre-run...") - log.Debug().Msg("loading default config...") + log.Debug().Msg("loading default config values...") - LoadDefaultConfig() + LoadDefaultConfigValues() - if err = LoadConfig(); err != nil { + if err = LoadConfigFile(cmd); err != nil { return err } @@ -75,6 +74,9 @@ func RootCommandPersistentPreRunE(cmd *cobra.Command, args []string) (err error) return err } + // silence errors which at this point are unrelated to CLI (cobra/viper) errors + cmd.SilenceErrors = false + return err } @@ -94,27 +96,40 @@ func SetLogLevel() { } } -// LoadConfig loads the SemverBot configuration file. -// Returns an error if it fails. -func LoadConfig() (err error) { - viper.AddConfigPath(filepath.Dir(cli.ConfigFlag)) - viper.SetConfigName(strings.TrimSuffix(filepath.Base(cli.ConfigFlag), filepath.Ext(cli.ConfigFlag))) - viper.SetConfigType(strings.Split(filepath.Ext(cli.ConfigFlag), ".")[1]) - - log.Debug().Str("path", cli.ConfigFlag).Msg("loading config...") +// LoadConfigFile loads the SemverBot configuration file. +// If the config flag was used, it will only try to load that path. +// If the config flag was not used, multiple default config file paths will be tried. +// Returns no error if config files are not found, returns an error if it fails otherwise. +func LoadConfigFile(cmd *cobra.Command) (err error) { + configFlag := cmd.Flag("config") - if err = viper.ReadInConfig(); err != nil { + if err = viperx.LoadConfig(cli.ConfigFlag); err != nil { if errors.As(err, &viper.ConfigFileNotFoundError{}) { - log.Warn().Msg("config file not found") + log.Warn().Msgf("config file %s not found", cli.ConfigFlag) + } + + if configFlag.Changed { return nil } } + for _, path := range cli.DefaultAdditionalConfigFilePaths { + if err = viperx.LoadConfig(path); err == nil { + return err + } + + if !errors.As(err, &viper.ConfigFileNotFoundError{}) { + return err + } + + log.Warn().Msgf("config file %s not found", path) + } + return err } -// LoadDefaultConfig loads the default SemverBot config. -func LoadDefaultConfig() { +// LoadDefaultConfigValues loads the default SemverBot config. +func LoadDefaultConfigValues() { viper.SetDefault(cli.GitTagsPrefixConfigKey, cli.DefaultGitTagsPrefix) viper.SetDefault(cli.ModeConfigKey, cli.DefaultMode) viper.SetDefault(cli.ModesGitBranchDelimitersConfigKey, cli.DefaultGitBranchDelimiters) From 5a7b875b0509ab65a023bc351a22fa58dffd2e18 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 13:55:27 +0100 Subject: [PATCH 10/11] approached the implementation of trying multiple config files a bit different, more readable and tested - fixed some bugs where errors had to be assigned nil instead of keeping there error value --- pkg/cli/commands/root.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pkg/cli/commands/root.go b/pkg/cli/commands/root.go index 563b09e..7b47342 100644 --- a/pkg/cli/commands/root.go +++ b/pkg/cli/commands/root.go @@ -97,23 +97,26 @@ func SetLogLevel() { } // LoadConfigFile loads the SemverBot configuration file. -// If the config flag was used, it will only try to load that path. +// If the config flag was used, it will try to load only that path. // If the config flag was not used, multiple default config file paths will be tried. // Returns no error if config files are not found, returns an error if it fails otherwise. func LoadConfigFile(cmd *cobra.Command) (err error) { configFlag := cmd.Flag("config") - if err = viperx.LoadConfig(cli.ConfigFlag); err != nil { - if errors.As(err, &viper.ConfigFileNotFoundError{}) { - log.Warn().Msgf("config file %s not found", cli.ConfigFlag) + if configFlag.Changed { + if err = viperx.LoadConfig(cli.ConfigFlag); err != nil { + if errors.As(err, &viper.ConfigFileNotFoundError{}) { + log.Warn().Msgf("config file %s not found", cli.ConfigFlag) + return nil + } } - if configFlag.Changed { - return nil - } + return err } - for _, path := range cli.DefaultAdditionalConfigFilePaths { + paths := append([]string{cli.DefaultConfigFilePath}, cli.DefaultAdditionalConfigFilePaths...) + + for _, path := range paths { if err = viperx.LoadConfig(path); err == nil { return err } @@ -122,6 +125,7 @@ func LoadConfigFile(cmd *cobra.Command) (err error) { return err } + err = nil log.Warn().Msgf("config file %s not found", path) } From f8f223d6daf18aec3dbd871bba6111a0d8917140 Mon Sep 17 00:00:00 2001 From: Olivier Van Aken Date: Wed, 17 Jan 2024 14:04:19 +0100 Subject: [PATCH 11/11] updated readme with changes --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8717bab..f0203bf 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,14 @@ Increments the `patch` level. ## How to configure -`sbot` supports a configuration file. It looks for a `.semverbot.toml` file in the current working directory by default. +`sbot` supports a configuration file. It looks in the current working directory by default. + +Supported default paths: +- `.semverbot.toml` +- `.sbot.toml` +- `.semverbot/config.toml` +- `.sbot/config.toml` + `.json` and `.yaml` formats are not officially supported, but might work. Using `.toml` is highly recommended. ### Defaults @@ -286,7 +293,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - + - name: set up path run: | mkdir bin @@ -309,7 +316,7 @@ jobs: echo "current version: ${current_version}" echo "next version: ${release_version}" - ... build / publish ... + # ... build / publish ... - name: release version run: |