From 99cdaa0b69e884128e8f673f96bd4d3e32743d80 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 29 Dec 2022 14:43:09 +0100 Subject: [PATCH] main: refactor BuildInfo code Simplify and move it into a new file version.go. --- init_dir.go | 2 - main.go | 82 ----------------------------------------- version.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 84 deletions(-) create mode 100644 version.go diff --git a/init_dir.go b/init_dir.go index e895f00..5ade692 100644 --- a/init_dir.go +++ b/init_dir.go @@ -55,8 +55,6 @@ func isDir(dir string) error { // In reverse mode, we create .gocryptfs.reverse.conf and the directory does // not need to be empty. func initDir(args *argContainer) { - initGIT() - var err error if args.reverse { _, err = os.Stat(args.config) diff --git a/main.go b/main.go index dad2c73..7facf78 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,10 @@ package main import ( - "fmt" "log" "os" "path/filepath" "runtime" - "runtime/debug" "strconv" "strings" @@ -21,67 +19,9 @@ import ( "github.com/rfjakob/gocryptfs/v2/internal/fido2" "github.com/rfjakob/gocryptfs/v2/internal/readpassword" "github.com/rfjakob/gocryptfs/v2/internal/speed" - "github.com/rfjakob/gocryptfs/v2/internal/stupidgcm" "github.com/rfjakob/gocryptfs/v2/internal/tlog" ) -// GitVersion is the gocryptfs version according to git, set by build.bash -var GitVersion = "[GitVersion not set - please compile using ./build.bash]" - -// GitVersionFuse is the go-fuse library version, set by build.bash -var GitVersionFuse = "[GitVersionFuse not set - please compile using ./build.bash]" - -// BuildDate is a date string like "2017-09-06", set by build.bash -var BuildDate = "0000-00-00" - -// raceDetector is set to true by race.go if we are compiled with "go build -race" -var raceDetector bool - -func initGIT() { - bi, ok := debug.ReadBuildInfo() - if !ok { - return - } - if strings.HasPrefix(GitVersion, `[`) { - GitVersion = bi.Main.Version - var gitDate string - var gitDirty bool - for _, item := range bi.Settings { - switch item.Key { - case "vcs.revision": - GitVersion = item.Value - case "vcs.time": - gitDate = item.Value - gitDate = strings.Map(func(r rune) rune { - switch { - case r >= '0' && r <= '9': - return r - default: - return -1 - } - }, gitDate) - case "vcs.modified": - gitDirty, _ = strconv.ParseBool(item.Value) - } - } - if len(gitDate) > 0 { - GitVersion = gitDate + "-" + GitVersion - } - if gitDirty { - GitVersion = GitVersion + " (dirty)" - } - } - const fuseModule = `github.com/hanwen/go-fuse/v2` - - if strings.HasPrefix(GitVersionFuse, `[`) { - for _, item := range bi.Deps { - if item.Path == fuseModule { - GitVersionFuse = item.Version - } - } - } -} - // loadConfig loads the config file `args.config` and decrypts the masterkey, // or gets via the `-masterkey` or `-zerokey` command line options, if specified. func loadConfig(args *argContainer) (masterkey []byte, cf *configfile.ConfFile, err error) { @@ -183,28 +123,6 @@ func changePassword(args *argContainer) { tlog.Info.Printf(tlog.ColorGreen + "Password changed." + tlog.ColorReset) } -// printVersion prints a version string like this: -// gocryptfs v1.7-32-gcf99cfd; go-fuse v1.0.0-174-g22a9cb9; 2019-05-12 go1.12 linux/amd64 -func printVersion() { - initGIT() - - var tagsSlice []string - if stupidgcm.BuiltWithoutOpenssl { - tagsSlice = append(tagsSlice, "without_openssl") - } - tags := "" - if tagsSlice != nil { - tags = " " + strings.Join(tagsSlice, " ") - } - built := fmt.Sprintf("%s %s", BuildDate, runtime.Version()) - if raceDetector { - built += " -race" - } - fmt.Printf("%s %s%s; go-fuse %s; %s %s/%s\n", - tlog.ProgramName, GitVersion, tags, GitVersionFuse, built, - runtime.GOOS, runtime.GOARCH) -} - func main() { mxp := runtime.GOMAXPROCS(0) if mxp < 4 && os.Getenv("GOMAXPROCS") == "" { diff --git a/version.go b/version.go new file mode 100644 index 0000000..97bf83a --- /dev/null +++ b/version.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "runtime" + "runtime/debug" + "strconv" + "strings" + + "github.com/rfjakob/gocryptfs/v2/internal/stupidgcm" + "github.com/rfjakob/gocryptfs/v2/internal/tlog" +) + +const ( + gitVersionNotSet = "[GitVersion not set - please compile using ./build.bash]" + gitVersionFuseNotSet = "[GitVersionFuse not set - please compile using ./build.bash]" + buildDateNotSet = "0000-00-00" +) + +var ( + // GitVersion is the gocryptfs version according to git, set by build.bash + GitVersion = gitVersionNotSet + // GitVersionFuse is the go-fuse library version, set by build.bash + GitVersionFuse = gitVersionFuseNotSet + // BuildDate is a date string like "2017-09-06", set by build.bash + BuildDate = buildDateNotSet +) + +func init() { + versionFromBuildInfo() +} + +// raceDetector is set to true by race.go if we are compiled with "go build -race" +var raceDetector bool + +// printVersion prints a version string like this: +// gocryptfs v1.7-32-gcf99cfd; go-fuse v1.0.0-174-g22a9cb9; 2019-05-12 go1.12 linux/amd64 +func printVersion() { + var tagsSlice []string + if stupidgcm.BuiltWithoutOpenssl { + tagsSlice = append(tagsSlice, "without_openssl") + } + tags := "" + if tagsSlice != nil { + tags = " " + strings.Join(tagsSlice, " ") + } + built := fmt.Sprintf("%s %s", BuildDate, runtime.Version()) + if raceDetector { + built += " -race" + } + fmt.Printf("%s %s%s; go-fuse %s; %s %s/%s\n", + tlog.ProgramName, GitVersion, tags, GitVersionFuse, built, + runtime.GOOS, runtime.GOARCH) +} + +// versionFromBuildInfo tries to get some information out of the information baked in +// by the Go compiler. Does nothing when build.bash was used to build. +func versionFromBuildInfo() { + info, ok := debug.ReadBuildInfo() + if !ok { + tlog.Debug.Println("versionFromBuildInfo: ReadBuildInfo() failed") + return + } + // Parse BuildSettings + var vcsRevision, vcsTime string + var vcsModified bool + for _, s := range info.Settings { + switch s.Key { + case "vcs.revision": + vcsRevision = s.Value + case "vcs.time": + vcsTime = s.Value + case "vcs.modified": + vcsModified, _ = strconv.ParseBool(s.Value) + } + } + // Fill our version strings + if GitVersion == gitVersionNotSet { + GitVersion = info.Main.Version + if GitVersion == "(devel)" && vcsRevision != "" { + GitVersion = fmt.Sprintf("vcs.revision=%s", vcsRevision) + } + if vcsModified { + GitVersion += "-dirty" + } + } + if GitVersionFuse == gitVersionFuseNotSet { + for _, m := range info.Deps { + if m.Path == "github.com/hanwen/go-fuse/v2" { + GitVersionFuse = m.Version + if m.Replace != nil { + GitVersionFuse = m.Replace.Version + } + break + } + } + } + if BuildDate == buildDateNotSet { + if vcsTime != "" { + BuildDate = fmt.Sprintf("vcs.time=%s", vcsTime) + } + } +}