2021-01-13 13:18:51 +00:00
|
|
|
// Package version contains AdGuard Home version information.
|
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2021-03-31 10:55:21 +01:00
|
|
|
"runtime/debug"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-04-07 18:16:06 +01:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
2021-03-31 10:55:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Channel constants.
|
|
|
|
const (
|
|
|
|
ChannelDevelopment = "development"
|
|
|
|
ChannelEdge = "edge"
|
|
|
|
ChannelBeta = "beta"
|
|
|
|
ChannelRelease = "release"
|
2021-01-13 13:18:51 +00: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 (
|
2021-03-31 10:55:21 +01:00
|
|
|
channel string = ChannelDevelopment
|
|
|
|
goarm string
|
|
|
|
gomips string
|
|
|
|
version string
|
|
|
|
buildtime string
|
2021-01-25 11:09:29 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// Channel returns the current AdGuard Home release channel.
|
|
|
|
func Channel() (v string) {
|
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:55:21 +01:00
|
|
|
// vFmtFull defines the format of full version output.
|
|
|
|
const vFmtFull = "AdGuard Home, version %s"
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// Full returns the full current version of AdGuard Home.
|
|
|
|
func Full() (v string) {
|
2021-03-31 10:55:21 +01:00
|
|
|
return fmt.Sprintf(vFmtFull, version)
|
2021-01-13 13:18:51 +00: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 10:55:21 +01:00
|
|
|
|
|
|
|
// Common formatting constants.
|
|
|
|
const (
|
|
|
|
sp = " "
|
|
|
|
nl = "\n"
|
|
|
|
tb = "\t"
|
|
|
|
nltb = nl + tb
|
|
|
|
)
|
|
|
|
|
|
|
|
// Constants defining the format of module information string.
|
|
|
|
const (
|
|
|
|
modInfoAtSep = "@"
|
|
|
|
modInfoDevSep = sp
|
|
|
|
modInfoSumLeft = " (sum: "
|
|
|
|
modInfoSumRight = ")"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fmtModule returns formatted information about module. The result looks like:
|
|
|
|
//
|
|
|
|
// github.com/Username/module@v1.2.3 (sum: someHASHSUM=)
|
|
|
|
//
|
|
|
|
func fmtModule(m *debug.Module) (formatted string) {
|
|
|
|
if m == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if repl := m.Replace; repl != nil {
|
|
|
|
return fmtModule(repl)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &strings.Builder{}
|
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, m.Path)
|
2021-03-31 10:55:21 +01:00
|
|
|
if ver := m.Version; ver != "" {
|
|
|
|
sep := modInfoAtSep
|
|
|
|
if ver == "(devel)" {
|
|
|
|
sep = modInfoDevSep
|
|
|
|
}
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, sep, ver)
|
2021-03-31 10:55:21 +01:00
|
|
|
}
|
|
|
|
if sum := m.Sum; sum != "" {
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, modInfoSumLeft, sum, modInfoSumRight)
|
2021-03-31 10:55:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants defining the headers of build information message.
|
|
|
|
const (
|
|
|
|
vFmtAGHHdr = "AdGuard Home"
|
|
|
|
vFmtVerHdr = "Version: "
|
|
|
|
vFmtChanHdr = "Channel: "
|
|
|
|
vFmtGoHdr = "Go version: "
|
|
|
|
vFmtTimeHdr = "Build time: "
|
|
|
|
vFmtRaceHdr = "Race: "
|
|
|
|
vFmtGOOSHdr = "GOOS: " + runtime.GOOS
|
|
|
|
vFmtGOARCHHdr = "GOARCH: " + runtime.GOARCH
|
|
|
|
vFmtGOARMHdr = "GOARM: "
|
|
|
|
vFmtGOMIPSHdr = "GOMIPS: "
|
|
|
|
vFmtDepsHdr = "Dependencies:"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Verbose returns formatted build information. Output example:
|
|
|
|
//
|
|
|
|
// AdGuard Home
|
|
|
|
// Version: v0.105.3
|
|
|
|
// Channel: development
|
|
|
|
// Go version: go1.15.3
|
|
|
|
// Build time: 2021-03-30T16:26:08Z+0300
|
|
|
|
// GOOS: darwin
|
|
|
|
// GOARCH: amd64
|
|
|
|
// Race: false
|
|
|
|
// Main module:
|
|
|
|
// ...
|
|
|
|
// Dependencies:
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Make it write into passed io.Writer.
|
|
|
|
func Verbose() (v string) {
|
|
|
|
b := &strings.Builder{}
|
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(
|
2021-03-31 10:55:21 +01:00
|
|
|
b,
|
|
|
|
vFmtAGHHdr,
|
|
|
|
nl,
|
|
|
|
vFmtVerHdr,
|
|
|
|
version,
|
|
|
|
nl,
|
|
|
|
vFmtChanHdr,
|
|
|
|
channel,
|
|
|
|
nl,
|
|
|
|
vFmtGoHdr,
|
|
|
|
runtime.Version(),
|
|
|
|
)
|
|
|
|
if buildtime != "" {
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nl, vFmtTimeHdr, buildtime)
|
2021-03-31 10:55:21 +01:00
|
|
|
}
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nl, vFmtGOOSHdr, nl, vFmtGOARCHHdr)
|
2021-03-31 10:55:21 +01:00
|
|
|
if goarm != "" {
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nl, vFmtGOARMHdr, "v", goarm)
|
2021-03-31 10:55:21 +01:00
|
|
|
} else if gomips != "" {
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nl, vFmtGOMIPSHdr, gomips)
|
2021-03-31 10:55:21 +01:00
|
|
|
}
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nl, vFmtRaceHdr, strconv.FormatBool(isRace))
|
2021-03-31 10:55:21 +01:00
|
|
|
|
|
|
|
info, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(info.Deps) == 0 {
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nl, vFmtDepsHdr)
|
2021-03-31 10:55:21 +01:00
|
|
|
for _, dep := range info.Deps {
|
|
|
|
if depStr := fmtModule(dep); depStr != "" {
|
2021-04-07 18:16:06 +01:00
|
|
|
aghstrings.WriteToBuilder(b, nltb, depStr)
|
2021-03-31 10:55:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|