forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request: 2416 improve version output
Merge in DNS/adguard-home from 2416-version to master Updates AdguardTeam#2416. Squashed commit of the following: commit ad529ee Merge: 9ba2f68 bfc7e16 Author: Eugene Burkov <[email protected]> Date: Wed Mar 31 12:42:37 2021 +0300 Merge branch 'master' into 2416-version commit 9ba2f68 Author: Eugene Burkov <[email protected]> Date: Wed Mar 31 12:40:53 2021 +0300 all: fix docs commit 521a61d Author: Eugene Burkov <[email protected]> Date: Tue Mar 30 17:48:56 2021 +0300 all: imp code commit 844c4e0 Author: Eugene Burkov <[email protected]> Date: Tue Mar 30 17:48:56 2021 +0300 all: imp docs, log changes commit c8206b0 Author: Eugene Burkov <[email protected]> Date: Tue Mar 30 17:19:51 2021 +0300 version: imp verbose output commit b2b41d4 Author: Eugene Burkov <[email protected]> Date: Tue Mar 30 16:37:23 2021 +0300 version: imp code, docs commit 553bd9c Author: Eugene Burkov <[email protected]> Date: Tue Mar 30 14:07:05 2021 +0300 version: imp verbosed output
- Loading branch information
1 parent
4a79077
commit 4f18c7c
Showing
7 changed files
with
191 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build !race | ||
|
||
package version | ||
|
||
const isRace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build race | ||
|
||
package version | ||
|
||
const isRace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,17 @@ package version | |
import ( | ||
"fmt" | ||
"runtime" | ||
"runtime/debug" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Channel constants. | ||
const ( | ||
ChannelDevelopment = "development" | ||
ChannelEdge = "edge" | ||
ChannelBeta = "beta" | ||
ChannelRelease = "release" | ||
) | ||
|
||
// These are set by the linker. Unfortunately we cannot set constants during | ||
|
@@ -13,35 +24,24 @@ import ( | |
// TODO(a.garipov): Find out if we can get GOARM and GOMIPS values the same way | ||
// we can GOARCH and GOOS. | ||
var ( | ||
channel string = ChannelDevelopment | ||
goarm string | ||
gomips string | ||
version string | ||
) | ||
|
||
// Channel constants. | ||
const ( | ||
ChannelDevelopment = "development" | ||
ChannelEdge = "edge" | ||
ChannelBeta = "beta" | ||
ChannelRelease = "release" | ||
channel string = ChannelDevelopment | ||
goarm string | ||
gomips string | ||
version string | ||
buildtime string | ||
) | ||
|
||
// Channel returns the current AdGuard Home release channel. | ||
func Channel() (v string) { | ||
return channel | ||
} | ||
|
||
// vFmtFull defines the format of full version output. | ||
const vFmtFull = "AdGuard Home, version %s" | ||
|
||
// Full returns the full current version of AdGuard Home. | ||
func Full() (v string) { | ||
msg := "AdGuard Home, version %s, channel %s, arch %s %s" | ||
if goarm != "" { | ||
msg = msg + " v" + goarm | ||
} else if gomips != "" { | ||
msg = msg + " " + gomips | ||
} | ||
|
||
return fmt.Sprintf(msg, version, channel, runtime.GOOS, runtime.GOARCH) | ||
return fmt.Sprintf(vFmtFull, version) | ||
} | ||
|
||
// GOARM returns the GOARM value used to build the current AdGuard Home release. | ||
|
@@ -59,3 +59,137 @@ func GOMIPS() (v string) { | |
func Version() (v string) { | ||
return version | ||
} | ||
|
||
// Common formatting constants. | ||
const ( | ||
sp = " " | ||
nl = "\n" | ||
tb = "\t" | ||
nltb = nl + tb | ||
) | ||
|
||
// writeStrings is a convenient wrapper for strings.(*Builder).WriteString that | ||
// deals with multiple strings and ignores errors that are guaranteed to be nil. | ||
func writeStrings(b *strings.Builder, strs ...string) { | ||
for _, s := range strs { | ||
_, _ = b.WriteString(s) | ||
} | ||
} | ||
|
||
// 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/[email protected] (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{} | ||
|
||
writeStrings(b, m.Path) | ||
if ver := m.Version; ver != "" { | ||
sep := modInfoAtSep | ||
if ver == "(devel)" { | ||
sep = modInfoDevSep | ||
} | ||
writeStrings(b, sep, ver) | ||
} | ||
if sum := m.Sum; sum != "" { | ||
writeStrings(b, modInfoSumLeft, sum, modInfoSumRight) | ||
} | ||
|
||
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: " | ||
vFmtMainHdr = "Main module:" | ||
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{} | ||
|
||
writeStrings( | ||
b, | ||
vFmtAGHHdr, | ||
nl, | ||
vFmtVerHdr, | ||
version, | ||
nl, | ||
vFmtChanHdr, | ||
channel, | ||
nl, | ||
vFmtGoHdr, | ||
runtime.Version(), | ||
) | ||
if buildtime != "" { | ||
writeStrings(b, nl, vFmtTimeHdr, buildtime) | ||
} | ||
writeStrings(b, nl, vFmtGOOSHdr, nl, vFmtGOARCHHdr) | ||
if goarm != "" { | ||
writeStrings(b, nl, vFmtGOARMHdr, "v", goarm) | ||
} else if gomips != "" { | ||
writeStrings(b, nl, vFmtGOMIPSHdr, gomips) | ||
} | ||
writeStrings(b, nl, vFmtRaceHdr, strconv.FormatBool(isRace)) | ||
|
||
info, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return b.String() | ||
} | ||
|
||
writeStrings(b, nl, vFmtMainHdr, nltb, fmtModule(&info.Main)) | ||
|
||
if len(info.Deps) == 0 { | ||
return b.String() | ||
} | ||
|
||
writeStrings(b, nl, vFmtDepsHdr) | ||
for _, dep := range info.Deps { | ||
if depStr := fmtModule(dep); depStr != "" { | ||
writeStrings(b, nltb, depStr) | ||
} | ||
} | ||
|
||
return b.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters