diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ff8b3edb3bfce0eb481faf9ba746d50eee454b8..4ff1fd4bdc31e1fb665a5df85761495bfa6dddc7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> - <component name="ComposerSettings"> - <execution /> - </component> <component name="ProjectRootManager"> <output url="file://$PROJECT_DIR$/out" /> </component> diff --git a/.idea/runConfigurations/version_auto_test.xml b/.idea/runConfigurations/version_auto_test.xml new file mode 100644 index 0000000000000000000000000000000000000000..a092c4ba6ea92e632556406319849c26a3f2d0b6 --- /dev/null +++ b/.idea/runConfigurations/version_auto_test.xml @@ -0,0 +1,16 @@ +<component name="ProjectRunConfigurationManager"> + <configuration default="false" name="version auto test" type="GoApplicationRunConfiguration" factoryName="Go Application"> + <module name="version" /> + <working_directory value="$PROJECT_DIR$/../../alvine/local-dev/components/alvine/apps/test" /> + <parameters value="auto --verbose" /> + <EXTENSION ID="com.fapiko.jetbrains.plugins.better_direnv.runconfigs.GolandRunConfigurationExtension"> + <option name="DIRENV_ENABLED" value="false" /> + <option name="DIRENV_TRUSTED" value="false" /> + </EXTENSION> + <kind value="PACKAGE" /> + <package value="gitlab.schukai.com/oss/utilities/version" /> + <directory value="$PROJECT_DIR$" /> + <filePath value="$PROJECT_DIR$/main.go" /> + <method v="2" /> + </configuration> +</component> \ No newline at end of file diff --git a/README.md b/README.md index c15d29c3903fd9d7d6d6f7c03b3c99a12ce60fee..cb3c00635a61d482b5864fa742aca7ac0f5e3adf 100644 --- a/README.md +++ b/README.md @@ -71,18 +71,26 @@ it will trigger a patch or a major version bump respectively. Here is how you can use the 'auto' feature: ```bash -version auto --git +version auto ``` -Without the '--git' flag, the tool will use the last tag in the git history -as the starting point for the version calculation. The result will be printed -to stdout or written to a file if the '--path' flag is provided. - +This command implies the '--git' flag, which means that the tool will tag the +git repository with the new version. The tag name will be the new version number. It is important to note that this feature requires that your commit messages -follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0-beta.2/) +follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) message format. +**Predict** + +The `predict` command allows you to predict the next version based on the +project's git commit history since the last tag. Other than the `auto` command, +this command does not write the result to a file or tag the git repository. + +```bash +version predict +``` + **Makefile** diff --git a/commandline.go b/commandline.go index 2c8ca7f603bd1f2fd68cdf6d52b8d0aecd30d3ae..9160c2fba8f27d2608fee954d7b969ae3d797dbe 100644 --- a/commandline.go +++ b/commandline.go @@ -31,7 +31,9 @@ type commandLineOptions struct { Date struct { } `command:"date" description:"print the current date and time in the format YYYYMMDDHHMMSS"` Auto struct { - } `command:"auto" description:"check the git repository and increase the version if necessary. Implies --git"` + } `command:"auto" description:"check the git repository and increase the version if necessary. This implies --git"` + Predict struct { + } `command:"predict" description:"predict the next version based on the git history. This implies --git"` Print struct { } `command:"print" description:"print the current version, you can combine this with --git to print the last tag"` } @@ -69,6 +71,8 @@ func increaseMajor() (string, error) { var verbosity = false var nullTerminated = false +var command string +var predict = false func executeCommand() { @@ -89,7 +93,7 @@ func executeCommand() { } var newVersion string - command := activeCommand.Name + command = activeCommand.Name if arguments.Verbose { verbosity = true @@ -99,7 +103,25 @@ func executeCommand() { nullTerminated = true } - if arguments.Git || command == "auto" { + if command == "predict" { + predict = true + } + + if command == "auto" || command == "predict" { + arguments.Git = true + + if arguments.Path != "" || arguments.Selector != "" { + msg := "Error: cannot use --path or --selector with auto or predict\n" + _, err := fmt.Fprintf(os.Stderr, msg) + if err != nil { + fmt.Printf(msg) + } + os.Exit(1) + } + + } + + if arguments.Git { path, err := os.Getwd() if err != nil { @@ -124,7 +146,7 @@ func executeCommand() { } } - if command == "auto" { + if command == "auto" || command == "predict" { commitType, err := GetCommitType() if arguments.Verbose { diff --git a/version.go b/version.go index 71e6ea1873022a73125b72911708ab62bede0595..e5f0263ed695b3180ada9e06db5e7a01b2fac9c5 100644 --- a/version.go +++ b/version.go @@ -236,6 +236,16 @@ func writeVersionToGit(version string) error { func writeVersion(version string) error { + if predict == true { + if nullTerminated { + version += string(rune(0)) + fmt.Printf("%s", version) + } else { + fmt.Printf(version) + } + return nil + } + if arguments.Git { return writeVersionToGit(version) } else if currentType == "json" { @@ -254,5 +264,12 @@ func writeVersion(version string) error { return nil } + msg := fmt.Sprintf("Unknown type: %s for path: %s", currentType, arguments.Path) + _, err := fmt.Fprintf(os.Stderr, "%s\n", msg) + if err != nil { + fmt.Printf("%s\n", msg) + } + return nil + }