diff --git a/version/logversion.go b/version/logversion.go new file mode 100644 index 0000000..03844f5 --- /dev/null +++ b/version/logversion.go @@ -0,0 +1,16 @@ +package version + +import ( + "path/filepath" + "runtime" + + "github.com/p9c/log" +) + +var F, E, W, I, D, T log.LevelPrinter + +func init() { + _, file,_, _ := runtime.Caller(0) + verPath := filepath.Dir(file)+"/" + F, E, W, I, D, T = log.GetLogPrinterSet(log.AddLoggerSubsystem(verPath)) +} diff --git a/version/update/log.go b/version/update/log.go index d0cdea5..c924772 100644 --- a/version/update/log.go +++ b/version/update/log.go @@ -2,7 +2,8 @@ package main import ( "github.com/p9c/log" - "github.com/p9c/qu/version" + + "github.com/p9c/log/version" ) var F, E, W, I, D, T = log.GetLogPrinterSet(log.AddLoggerSubsystem(version.PathBase)) diff --git a/version/update/main.go b/version/update/main.go index 7057992..184e849 100644 --- a/version/update/main.go +++ b/version/update/main.go @@ -5,36 +5,43 @@ import ( "io/ioutil" "os" "path/filepath" - "runtime" "strings" "time" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/storer" + + "github.com/p9c/interrupt/version" ) var ( - URL string - GitRef string - GitCommit string - BuildTime string - Tag string + URL string + GitRef string + GitCommit string + BuildTime string + Tag string + Major, Minor, Patch int + Meta string + PathBase string ) func main() { + I.Ln(version.Get()) BuildTime = time.Now().Format(time.RFC3339) var cwd string var e error - if cwd, e = os.Getwd(); e != nil { + if cwd, e = os.Getwd(); E.Chk(e) { return } + cwd = filepath.Dir(cwd) + // I.Ln(cwd) var repo *git.Repository - if repo, e = git.PlainOpen(cwd); e != nil { + if repo, e = git.PlainOpen(cwd); E.Chk(e) { return } var rr []*git.Remote - if rr, e = repo.Remotes(); e != nil { + if rr, e = repo.Remotes(); E.Chk(e) { return } for i := range rr { @@ -55,15 +62,18 @@ func main() { } } + var tr *git.Worktree + if tr, e = repo.Worktree(); E.Chk(e) { + } var rh *plumbing.Reference - if rh, e = repo.Head(); e != nil { + if rh, e = repo.Head(); E.Chk(e) { return } rhs := rh.Strings() GitRef = rhs[0] GitCommit = rhs[1] var rt storer.ReferenceIter - if rt, e = repo.Tags(); e != nil { + if rt, e = repo.Tags(); E.Chk(e) { return } var maxVersion int @@ -71,42 +81,44 @@ func main() { var maxIs bool if e = rt.ForEach( func(pr *plumbing.Reference) (e error) { - prs := strings.Split(pr.String(), "/")[2] + s := strings.Split(pr.String(), "/") + prs := s[2] if strings.HasPrefix(prs, "v") { var va [3]int - _, _ = fmt.Sscanf(prs, "v%d.%d.%d", &va[0], &va[1], &va[2]) + var meta string + _, _ = fmt.Sscanf(prs, "v%d.%d.%d%s", &va[0], &va[1], &va[2], &meta) vn := va[0]*1000000 + va[1]*1000 + va[2] if maxVersion < vn { maxVersion = vn maxString = prs + Major = va[0] + Minor = va[1] + Patch = va[2] + Meta = meta } if pr.Hash() == rh.Hash() { maxIs = true + return } } - return nil + return }, - ); e != nil { + ); E.Chk(e) { return } if !maxIs { maxString += "+" } Tag = maxString - _, file, _, _ := runtime.Caller(0) - // fmt.Fprintln(os.Stderr, "file", file) - urlSplit := strings.Split(URL, "/") - // fmt.Fprintln(os.Stderr, "urlSplit", urlSplit) - baseFolder := urlSplit[len(urlSplit)-1] - // fmt.Fprintln(os.Stderr, "baseFolder", baseFolder) - splitPath := strings.Split(file, baseFolder) - // fmt.Fprintln(os.Stderr, "splitPath", splitPath) - PathBase := filepath.Join(splitPath[0], baseFolder) + string(filepath.Separator) - PathBase = strings.ReplaceAll(PathBase, "\\", "\\\\") - // fmt.Fprintln(os.Stderr, "PathBase", PathBase) + PathBase = tr.Filesystem.Root() + "/" + // I.Ln(PathBase) versionFile := `package version -import "fmt" +`+`//go:generate go run ./update/. + +import ( + "fmt" +) var ( @@ -123,17 +135,29 @@ var ( Tag = "%s" // PathBase is the path base returned from runtime caller PathBase = "%s" + // Major is the major number from the tag + Major = %d + // Minor is the minor number from the tag + Minor = %d + // Patch is the patch version number from the tag + Patch = %d + // Meta is the extra arbitrary string field from Semver spec + Meta = "%s" ) // Get returns a pretty printed version information string func Get() string { return fmt.Sprint( - "Repository Information\n"+ - " git repository: "+URL+"\n", - " branch: "+GitRef+"\n"+ - " commit: "+GitCommit+"\n"+ - " built: "+BuildTime+"\n"+ - " Tag: "+Tag+"\n", + "\nRepository Information\n"+ + "\tGit repository: "+URL+"\n", + "\tBranch: "+GitRef+"\n"+ + "\tCommit: "+GitCommit+"\n"+ + "\tBuilt: "+BuildTime+"\n"+ + "\tTag: "+Tag+"\n", + "\tMajor:", Major, "\n", + "\tMinor:", Minor, "\n", + "\tPatch:", Patch, "\n", + "\tMeta: ", Meta, "\n", ) } ` @@ -145,8 +169,14 @@ func Get() string { BuildTime, Tag, PathBase, + Major, + Minor, + Patch, + Meta, ) - if e = ioutil.WriteFile("version/version.go", []byte(versionFileOut), 0666); E.Chk(e) { + path := filepath.Join(filepath.Join(PathBase, "version"), "version.go") + if e = ioutil.WriteFile(path, []byte(versionFileOut), 0666); E.Chk(e) { } + // I.Ln("updated version.go written") return } diff --git a/version/version.go b/version/version.go index 2f0d777..d39ca63 100644 --- a/version/version.go +++ b/version/version.go @@ -1,6 +1,10 @@ package version -import "fmt" +//go:generate go run ./update/. + +import ( + "fmt" +) var ( @@ -9,24 +13,36 @@ var ( // GitRef is the gitref, as in refs/heads/branchname GitRef = "refs/heads/main" // GitCommit is the commit hash of the current HEAD - GitCommit = "568956448c87f6147be12234760f99779ede0304" + GitCommit = "49c260b36b2fa7567be0392c1a1009d1c74db759" // BuildTime stores the time when the current binary was built - BuildTime = "2021-04-15T02:19:31+02:00" + BuildTime = "2021-04-30T18:21:49+02:00" // Tag lists the Tag on the build, adding a + to the newest Tag if the commit is // not that commit - Tag = "v0.0.1" + Tag = "v0.0.2" // PathBase is the path base returned from runtime caller - PathBase = "/home/loki/src/github.com/p9c/interrupt/" + PathBase = "/home/loki/src/github.com/p9c/pod/pkg/interrupt/" + // Major is the major number from the tag + Major = 0 + // Minor is the minor number from the tag + Minor = 0 + // Patch is the patch version number from the tag + Patch = 2 + // Meta is the extra arbitrary string field from Semver spec + Meta = "" ) // Get returns a pretty printed version information string func Get() string { return fmt.Sprint( - "Repository Information\n"+ - " git repository: "+URL+"\n", - " branch: "+GitRef+"\n"+ - " commit: "+GitCommit+"\n"+ - " built: "+BuildTime+"\n"+ - " Tag: "+Tag+"\n", + "\nRepository Information\n"+ + "\tGit repository: "+URL+"\n", + "\tBranch: "+GitRef+"\n"+ + "\tCommit: "+GitCommit+"\n"+ + "\tBuilt: "+BuildTime+"\n"+ + "\tTag: "+Tag+"\n", + "\tMajor:", Major, "\n", + "\tMinor:", Minor, "\n", + "\tPatch:", Patch, "\n", + "\tMeta: ", Meta, "\n", ) }