2021-01-13 16:18:51 +03:00
|
|
|
// Package version contains AdGuard Home version information.
|
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2021-03-31 12:55:21 +03:00
|
|
|
"runtime/debug"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-06-02 17:28:16 +03:00
|
|
|
"time"
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2021-03-31 12:55:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Channel constants.
|
|
|
|
const (
|
2024-03-13 16:25:51 +03:00
|
|
|
ChannelBeta = "beta"
|
|
|
|
ChannelCandidate = "candidate"
|
2021-03-31 12:55:21 +03:00
|
|
|
ChannelDevelopment = "development"
|
|
|
|
ChannelEdge = "edge"
|
|
|
|
ChannelRelease = "release"
|
2021-01-13 16:18:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// These are set by the linker. Unfortunately we cannot set constants during
|
|
|
|
// linking, and Go doesn't have a concept of immutable variables, so to be
|
|
|
|
// thorough we have to only export them through getters.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Find out if we can get GOARM and GOMIPS values the same way
|
|
|
|
// we can GOARCH and GOOS.
|
|
|
|
var (
|
2022-06-02 17:28:16 +03:00
|
|
|
channel string = ChannelDevelopment
|
|
|
|
goarm string
|
|
|
|
gomips string
|
|
|
|
version string
|
|
|
|
committime string
|
2021-01-25 14:09:29 +03:00
|
|
|
)
|
|
|
|
|
2021-01-13 16:18:51 +03:00
|
|
|
// Channel returns the current AdGuard Home release channel.
|
|
|
|
func Channel() (v string) {
|
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:55:21 +03:00
|
|
|
// vFmtFull defines the format of full version output.
|
|
|
|
const vFmtFull = "AdGuard Home, version %s"
|
|
|
|
|
2021-01-13 16:18:51 +03:00
|
|
|
// Full returns the full current version of AdGuard Home.
|
|
|
|
func Full() (v string) {
|
2021-03-31 12:55:21 +03:00
|
|
|
return fmt.Sprintf(vFmtFull, version)
|
2021-01-13 16:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GOARM returns the GOARM value used to build the current AdGuard Home release.
|
|
|
|
func GOARM() (v string) {
|
|
|
|
return goarm
|
|
|
|
}
|
|
|
|
|
|
|
|
// GOMIPS returns the GOMIPS value used to build the current AdGuard Home
|
|
|
|
// release.
|
|
|
|
func GOMIPS() (v string) {
|
|
|
|
return gomips
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the AdGuard Home build version.
|
|
|
|
func Version() (v string) {
|
|
|
|
return version
|
|
|
|
}
|
2021-03-31 12:55:21 +03:00
|
|
|
|
|
|
|
// fmtModule returns formatted information about module. The result looks like:
|
|
|
|
//
|
2022-09-07 18:03:18 +03:00
|
|
|
// github.com/Username/module@v1.2.3 (sum: someHASHSUM=)
|
2021-03-31 12:55:21 +03:00
|
|
|
func fmtModule(m *debug.Module) (formatted string) {
|
|
|
|
if m == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if repl := m.Replace; repl != nil {
|
|
|
|
return fmtModule(repl)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &strings.Builder{}
|
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
stringutil.WriteToBuilder(b, m.Path)
|
2021-03-31 12:55:21 +03:00
|
|
|
if ver := m.Version; ver != "" {
|
2022-11-02 16:18:02 +03:00
|
|
|
sep := "@"
|
2021-03-31 12:55:21 +03:00
|
|
|
if ver == "(devel)" {
|
2022-11-02 16:18:02 +03:00
|
|
|
sep = " "
|
2021-03-31 12:55:21 +03:00
|
|
|
}
|
2022-11-02 16:18:02 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
stringutil.WriteToBuilder(b, sep, ver)
|
2021-03-31 12:55:21 +03:00
|
|
|
}
|
2022-11-02 16:18:02 +03:00
|
|
|
|
2021-03-31 12:55:21 +03:00
|
|
|
if sum := m.Sum; sum != "" {
|
2022-11-02 16:18:02 +03:00
|
|
|
stringutil.WriteToBuilder(b, "(sum: ", sum, ")")
|
2021-03-31 12:55:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants defining the headers of build information message.
|
|
|
|
const (
|
2024-01-30 18:43:51 +03:00
|
|
|
vFmtAGHHdr = "AdGuard Home"
|
|
|
|
vFmtVerHdr = "Version: "
|
|
|
|
vFmtSchemaVerHdr = "Schema version: "
|
|
|
|
vFmtChanHdr = "Channel: "
|
|
|
|
vFmtGoHdr = "Go version: "
|
|
|
|
vFmtTimeHdr = "Commit time: "
|
|
|
|
vFmtRaceHdr = "Race: "
|
|
|
|
vFmtGOOSHdr = "GOOS: " + runtime.GOOS
|
|
|
|
vFmtGOARCHHdr = "GOARCH: " + runtime.GOARCH
|
|
|
|
vFmtGOARMHdr = "GOARM: "
|
|
|
|
vFmtGOMIPSHdr = "GOMIPS: "
|
|
|
|
vFmtDepsHdr = "Dependencies:"
|
2021-03-31 12:55:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Verbose returns formatted build information. Output example:
|
|
|
|
//
|
2022-09-07 18:03:18 +03:00
|
|
|
// AdGuard Home
|
|
|
|
// Version: v0.105.3
|
2024-01-30 18:43:51 +03:00
|
|
|
// Schema version: 27
|
2022-09-07 18:03:18 +03:00
|
|
|
// Channel: development
|
|
|
|
// Go version: go1.15.3
|
|
|
|
// Build time: 2021-03-30T16:26:08Z+0300
|
|
|
|
// GOOS: darwin
|
|
|
|
// GOARCH: amd64
|
|
|
|
// Race: false
|
|
|
|
// Main module:
|
|
|
|
// ...
|
|
|
|
// Dependencies:
|
|
|
|
// ...
|
2021-03-31 12:55:21 +03:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Make it write into passed io.Writer.
|
2024-01-30 18:43:51 +03:00
|
|
|
func Verbose(schemaVersion uint) (v string) {
|
2021-03-31 12:55:21 +03:00
|
|
|
b := &strings.Builder{}
|
|
|
|
|
2022-01-18 15:05:34 +03:00
|
|
|
const nl = "\n"
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtAGHHdr, nl)
|
|
|
|
stringutil.WriteToBuilder(b, vFmtVerHdr, version, nl)
|
|
|
|
|
|
|
|
schemaVerStr := strconv.FormatUint(uint64(schemaVersion), 10)
|
|
|
|
stringutil.WriteToBuilder(b, vFmtSchemaVerHdr, schemaVerStr, nl)
|
|
|
|
|
|
|
|
stringutil.WriteToBuilder(b, vFmtChanHdr, channel, nl)
|
|
|
|
stringutil.WriteToBuilder(b, vFmtGoHdr, runtime.Version(), nl)
|
2022-06-02 17:28:16 +03:00
|
|
|
|
2023-07-03 14:10:40 +03:00
|
|
|
writeCommitTime(b)
|
2022-06-02 17:28:16 +03:00
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtGOOSHdr, nl)
|
|
|
|
stringutil.WriteToBuilder(b, vFmtGOARCHHdr, nl)
|
|
|
|
|
2021-03-31 12:55:21 +03:00
|
|
|
if goarm != "" {
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtGOARMHdr, "v", goarm, nl)
|
2021-03-31 12:55:21 +03:00
|
|
|
} else if gomips != "" {
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtGOMIPSHdr, gomips, nl)
|
2021-03-31 12:55:21 +03:00
|
|
|
}
|
2022-06-02 17:28:16 +03:00
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtRaceHdr, strconv.FormatBool(isRace), nl)
|
2021-03-31 12:55:21 +03:00
|
|
|
|
|
|
|
info, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(info.Deps) == 0 {
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtDepsHdr, nl)
|
2021-03-31 12:55:21 +03:00
|
|
|
for _, dep := range info.Deps {
|
|
|
|
if depStr := fmtModule(dep); depStr != "" {
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, "\t", depStr, nl)
|
2021-03-31 12:55:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
2023-07-03 14:10:40 +03:00
|
|
|
|
|
|
|
func writeCommitTime(b *strings.Builder) {
|
|
|
|
if committime == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
commitTimeUnix, err := strconv.ParseInt(committime, 10, 64)
|
|
|
|
if err != nil {
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtTimeHdr, fmt.Sprintf("parse error: %s", err), "\n")
|
2023-07-03 14:10:40 +03:00
|
|
|
} else {
|
2024-01-30 18:43:51 +03:00
|
|
|
stringutil.WriteToBuilder(b, vFmtTimeHdr, time.Unix(commitTimeUnix, 0).String(), "\n")
|
2023-07-03 14:10:40 +03:00
|
|
|
}
|
|
|
|
}
|