atc0005 / todo

A collection of TODO items not specific to any one project
MIT License
0 stars 0 forks source link

Test using `debug.ReadBuildInfo()` for version retrieval #41

Open atc0005 opened 2 years ago

atc0005 commented 2 years ago

From https://github.com/mgechev/revive/commit/b7d1908051ec9493485b3dcbf42b4565b7744e74:

    // Output build info (version, commit, date and builtBy)
    if versionFlag {
        var buildInfo string
        if date != "unknown" && builtBy != "unknown" {
            buildInfo = fmt.Sprintf("Built\t\t%s by %s\n", date, builtBy)
        }

        if commit != "none" {
            buildInfo = fmt.Sprintf("Commit:\t\t%s\n%s", commit, buildInfo)
        }

        if version == "dev" {
            bi, ok := debug.ReadBuildInfo()
            if ok {
                version = bi.Main.Version
                if strings.HasPrefix(version, "v") {
                    version = bi.Main.Version[1:]
                }
                if len(buildInfo) == 0 {
                    fmt.Printf("version %s\n", version)
                    os.Exit(0)
                }
            }
        }

        fmt.Printf("Version:\t%s\n%s", version, buildInfo)
        os.Exit(0)

The idea being that in addition to setting a build "version" via Makefile builds, it would be really useful to support determining the version when a package is installed via go install.