Skip to content
Snippets Groups Projects
Verified Commit 164906dc authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: new command print

parent 3ce6f60b
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GoImports">
<option name="addUnambiguousImportsOnTheFly" value="false" />
<option name="optimizeImportsOnTheFly" value="false" />
</component>
</project>
\ No newline at end of file
<component name="libraryTable">
<library name="fs">
<CLASSES>
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/html/template/testdata/fs.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="testdata">
<CLASSES>
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/symlink.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/test-prefix.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/utf8-7zip.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/zip64-2.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/winxp.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-22738.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/dupdir.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/utf8-infozip.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-win7.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/test-trailing-junk.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-osx.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-winrar.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/utf8-winrar.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-winzip.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/test-baddirsz.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-7zip.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/subdir.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/test.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/dd.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/utf8-winzip.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/crc32-not-streamed.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/readme.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/unix.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/utf8-osx.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-infozip.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/go-with-datadesc-sig.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/zip64.zip!/" />
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/src/archive/zip/testdata/time-go.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="zoneinfo">
<CLASSES>
<root url="jar://$PROJECT_DIR$/.devenv/profile/share/go/lib/time/zoneinfo.zip!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownSettings">
<enabledExtensions>
<entry key="MermaidLanguageExtension" value="false" />
<entry key="PlantUMLLanguageExtension" value="true" />
</enabledExtensions>
<option name="fontFamily" value="JetBrains Mono" />
<option name="fontSize" value="15" />
</component>
</project>
\ No newline at end of file
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
<component name="ComposerSettings"> <component name="ComposerSettings">
<execution /> <execution />
</component> </component>
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="accountSettings"> <component name="accountSettings">
<option name="activeProfile" value="profile:default" /> <option name="activeProfile" value="profile:default" />
<option name="activeRegion" value="eu-west-1" /> <option name="activeRegion" value="eu-west-1" />
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/version.iml" filepath="$PROJECT_DIR$/.idea/version.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="version auto --git" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="version" />
<working_directory value="$PROJECT_DIR$" />
<parameters value="auto --git" />
<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
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
...@@ -30,6 +30,8 @@ type commandLineOptions struct { ...@@ -30,6 +30,8 @@ type commandLineOptions struct {
} `command:"date" description:"print the current date and time in the format YYYYMMDDHHMMSS"` } `command:"date" description:"print the current date and time in the format YYYYMMDDHHMMSS"`
Auto struct { 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. Implies --git"`
Print struct {
} `command:"print" description:"print the current version, you can combine this with --git to print the last tag"`
} }
func increasePatch() (string, error) { func increasePatch() (string, error) {
...@@ -104,6 +106,17 @@ func executeCommand() { ...@@ -104,6 +106,17 @@ func executeCommand() {
} }
switch command { switch command {
case "print":
if arguments.Git {
version, err := getLatestSemanticTag(".")
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
fmt.Printf("%s", version)
os.Exit(0)
}
case "date": case "date":
currentTime := time.Now() currentTime := time.Now()
build = currentTime.Format("20060102150405") build = currentTime.Format("20060102150405")
......
...@@ -4,6 +4,10 @@ import ( ...@@ -4,6 +4,10 @@ import (
"fmt" "fmt"
"strings" "strings"
"regexp"
"sort"
"strconv"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
...@@ -38,6 +42,67 @@ func GetCommitType(path string) (CommitType, error) { ...@@ -38,6 +42,67 @@ func GetCommitType(path string) (CommitType, error) {
return commitType, nil return commitType, nil
} }
func getLatestSemanticTag(path string) (string, error) {
r, err := git.PlainOpen(path)
if err != nil {
return "", fmt.Errorf("failed to open repository: %v", err)
}
tags, err := r.Tags()
if err != nil {
return "", fmt.Errorf("failed to get tags: %v", err)
}
var tagList []string
err = tags.ForEach(func(tag *plumbing.Reference) error {
tagName := tag.Name().Short()
if isSemanticVersion(tagName) {
tagList = append(tagList, tagName)
}
return nil
})
if err != nil {
return "", fmt.Errorf("failed to iterate over tags: %v", err)
}
if len(tagList) == 0 {
return "", fmt.Errorf("no semantic tags found")
}
sort.Slice(tagList, func(i, j int) bool {
return compareSemanticVersions(tagList[i], tagList[j])
})
return tagList[len(tagList)-1], nil
}
func isSemanticVersion(tagName string) bool {
versionRegex := regexp.MustCompile(`^v?\d+\.\d+\.\d+.*$`)
return versionRegex.MatchString(tagName)
}
func compareSemanticVersions(a, b string) bool {
// Remove leading "v" if present
a = strings.TrimPrefix(a, "v")
b = strings.TrimPrefix(b, "v")
aParts := strings.Split(a, ".")
bParts := strings.Split(b, ".")
for i := 0; i < len(aParts) && i < len(bParts); i++ {
aNum, _ := strconv.Atoi(aParts[i])
bNum, _ := strconv.Atoi(bParts[i])
if aNum != bNum {
return aNum < bNum
}
}
return len(aParts) < len(bParts)
}
func getLatestTagCommit(r *git.Repository) (*object.Commit, error) { func getLatestTagCommit(r *git.Repository) (*object.Commit, error) {
tags, err := r.Tags() tags, err := r.Tags()
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment