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

fix: missing print version from version

parent 80a25c19
No related branches found
No related tags found
No related merge requests found
...@@ -9,3 +9,4 @@ devenv.local.nix ...@@ -9,3 +9,4 @@ devenv.local.nix
# pre-commit # pre-commit
.pre-commit-config.yaml .pre-commit-config.yaml
.attach_pid*
...@@ -10,9 +10,8 @@ import ( ...@@ -10,9 +10,8 @@ import (
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
) )
var ( var arguments *commandLineOptions
arguments *commandLineOptions var gitRepo *git.Repository
)
type commandLineOptions struct { type commandLineOptions struct {
Path string `short:"p" long:"path" description:"path to the file"` Path string `short:"p" long:"path" description:"path to the file"`
...@@ -67,8 +66,6 @@ func increaseMajor() (string, error) { ...@@ -67,8 +66,6 @@ func increaseMajor() (string, error) {
return next.String(), nil return next.String(), nil
} }
var gitRepo *git.Repository
func executeCommand() { func executeCommand() {
arguments = new(commandLineOptions) arguments = new(commandLineOptions)
...@@ -93,15 +90,21 @@ func executeCommand() { ...@@ -93,15 +90,21 @@ func executeCommand() {
if arguments.Git || command == "auto" { if arguments.Git || command == "auto" {
gitRepo, err = git.PlainOpen(".") gitRepo, err = git.PlainOpen(".")
if err != nil { if err != nil {
fmt.Println(err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
os.Exit(-1) os.Exit(-1)
} }
} }
if command == "auto" { if command == "auto" {
updateType, err := GetCommitType(gitRepo) updateType, err := GetCommitType()
if err != nil { if err != nil {
fmt.Println(err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
os.Exit(-1) os.Exit(-1)
} }
...@@ -122,16 +125,25 @@ func executeCommand() { ...@@ -122,16 +125,25 @@ func executeCommand() {
switch command { switch command {
case "print": case "print":
if arguments.Git {
version, err := getLatestSemanticTag(gitRepo) version, err := getVersion()
if err != nil {
_, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("Error: %s\n", err)
os.Exit(-1)
} }
os.Exit(-1)
}
if arguments.Null {
// terminate with null byte
fmt.Printf("%s", version.String()) fmt.Printf("%s", version.String())
os.Exit(0) } else {
fmt.Printf("%s\n", version.String())
} }
os.Exit(0)
case "date": case "date":
currentTime := time.Now() currentTime := time.Now()
build = currentTime.Format("20060102150405") build = currentTime.Format("20060102150405")
...@@ -141,6 +153,7 @@ func executeCommand() { ...@@ -141,6 +153,7 @@ func executeCommand() {
} }
fmt.Printf("%s", build) fmt.Printf("%s", build)
os.Exit(0)
case "init": case "init":
newVersion = "0.1.0" newVersion = "0.1.0"
...@@ -154,13 +167,19 @@ func executeCommand() { ...@@ -154,13 +167,19 @@ func executeCommand() {
} }
if err != nil { if err != nil {
fmt.Println(err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
os.Exit(-1) os.Exit(-1)
} }
err = writeVersion(newVersion) err = writeVersion(newVersion)
if err != nil { if err != nil {
fmt.Println(err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
os.Exit(-1) os.Exit(-1)
} }
......
...@@ -3,11 +3,10 @@ package main ...@@ -3,11 +3,10 @@ package main
import "fmt" import "fmt"
var ( var (
timeoutError = fmt.Errorf("timeout") timeoutError = fmt.Errorf("timeout")
noActiveCommandError = fmt.Errorf("no command active command") notImplementedError = fmt.Errorf("not implemented")
notImplementedError = fmt.Errorf("not implemented") missingSelector = fmt.Errorf("missing selector")
missingSelector = fmt.Errorf("missing selector") noSemverError = fmt.Errorf("no semver")
noSemverError = fmt.Errorf("no semver") notFoundError = fmt.Errorf("not found")
notFoundError = fmt.Errorf("not found") multipleFoundError = fmt.Errorf("multiple found")
multipleFoundError = fmt.Errorf("multiple found")
) )
...@@ -56,19 +56,19 @@ const ( ...@@ -56,19 +56,19 @@ const (
FixCommit FixCommit
) )
func GetCommitType(r *git.Repository) (CommitType, error) { func GetCommitType() (CommitType, error) {
latestTag, err := getLatestSemanticTag(r) latestTag, err := getLatestSemanticTag()
if err != nil { if err != nil {
return OtherCommit, err return OtherCommit, err
} }
tagCommit, err := getTagCommit(r, latestTag.Tag) tagCommit, err := getTagCommit(latestTag.Tag)
if err != nil { if err != nil {
return OtherCommit, err return OtherCommit, err
} }
commitType, err := getCommitTypeSinceTag(r, tagCommit) commitType, err := getCommitTypeSinceTag(tagCommit)
if err != nil { if err != nil {
return OtherCommit, err return OtherCommit, err
} }
...@@ -76,8 +76,8 @@ func GetCommitType(r *git.Repository) (CommitType, error) { ...@@ -76,8 +76,8 @@ func GetCommitType(r *git.Repository) (CommitType, error) {
return commitType, nil return commitType, nil
} }
func getLatestSemanticTag(r *git.Repository) (SemanticVersion, error) { func getLatestSemanticTag() (SemanticVersion, error) {
tagList, err := getSemanticTags(r) tagList, err := getSemanticTags()
if err != nil { if err != nil {
return SemanticVersion{}, err return SemanticVersion{}, err
} }
...@@ -89,7 +89,10 @@ func getLatestSemanticTag(r *git.Repository) (SemanticVersion, error) { ...@@ -89,7 +89,10 @@ func getLatestSemanticTag(r *git.Repository) (SemanticVersion, error) {
return tagList[len(tagList)-1], nil return tagList[len(tagList)-1], nil
} }
func getTagCommit(r *git.Repository, tag string) (*object.Commit, error) { func getTagCommit(tag string) (*object.Commit, error) {
var r = gitRepo
tags, err := r.Tags() tags, err := r.Tags()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get tags: %v", err) return nil, fmt.Errorf("failed to get tags: %v", err)
...@@ -120,7 +123,10 @@ func getTagCommit(r *git.Repository, tag string) (*object.Commit, error) { ...@@ -120,7 +123,10 @@ func getTagCommit(r *git.Repository, tag string) (*object.Commit, error) {
return tagCommit, nil return tagCommit, nil
} }
func getSemanticTags(r *git.Repository) ([]SemanticVersion, error) { func getSemanticTags() ([]SemanticVersion, error) {
r := gitRepo
tags, err := r.Tags() tags, err := r.Tags()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get tags: %v", err) return nil, fmt.Errorf("failed to get tags: %v", err)
...@@ -146,7 +152,10 @@ func getSemanticTags(r *git.Repository) ([]SemanticVersion, error) { ...@@ -146,7 +152,10 @@ func getSemanticTags(r *git.Repository) ([]SemanticVersion, error) {
return tagList, nil return tagList, nil
} }
func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitType, error) { func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) {
r := gitRepo
cIter, err := r.Log(&git.LogOptions{}) cIter, err := r.Log(&git.LogOptions{})
if err != nil { if err != nil {
return OtherCommit, fmt.Errorf("failed to get commit log: %v", err) return OtherCommit, fmt.Errorf("failed to get commit log: %v", err)
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -89,46 +88,11 @@ func getFromYaml() (string, error) { ...@@ -89,46 +88,11 @@ func getFromYaml() (string, error) {
} }
func getVersionFromGit() (string, error) {
path, err := os.Getwd()
if err != nil {
return "", err
}
repo, err := git.PlainOpen(path)
if err != nil {
return "", err
}
tags, err := repo.Tags()
if err != nil {
return "", err
}
currentVersion, _ := semver.NewVersion("0.1.0")
err = tags.ForEach(func(t *plumbing.Reference) error {
version, err := semver.NewVersion(t.Name().Short())
if err != nil {
return nil
}
if version.GreaterThan(currentVersion) {
currentVersion = version
}
return nil
})
return currentVersion.String(), nil
}
func readVersion() (string, error) { func readVersion() (string, error) {
if arguments.Git { if arguments.Git {
return getVersionFromGit() version, err := getLatestSemanticTag()
return version.String(), err
} else if arguments.Path != "" { } else if arguments.Path != "" {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment