[sources] Add support for displaying version and other information (based on z-run code).

This commit is contained in:
Ciprian Dorin Craciun 2022-09-02 13:59:46 +03:00
parent 1dd2586c39
commit bbca9d7096
6 changed files with 219 additions and 9 deletions

View file

@ -29,6 +29,8 @@ import "go.etcd.io/bbolt"
import . "github.com/volution/kawipiko/lib/common"
import . "github.com/volution/kawipiko/lib/archiver"
import "github.com/volution/kawipiko/cmd/version"
import _ "embed"
@ -1254,12 +1256,24 @@ func Main () () {
if len (os.Args) == 2 {
switch os.Args[1] {
case "version", "--version", "-v" :
version.Main ("kawipiko-archiver")
return
case "--help", "-h" :
os.Stderr.WriteString (usageText)
if _, _error := os.Stdout.WriteString (usageText); _error == nil {
return
} else {
AbortError (_error, "[5418de54] unexpected error!")
}
case "--man" :
os.Stderr.WriteString (manualText)
if _, _error := os.Stdout.WriteString (manualText); _error == nil {
return
} else {
AbortError (_error, "[5418de54] unexpected error!")
}
}
}

View file

@ -36,6 +36,8 @@ import "github.com/lucas-clemente/quic-go/http3"
import . "github.com/volution/kawipiko/lib/common"
import . "github.com/volution/kawipiko/lib/server"
import "github.com/volution/kawipiko/cmd/version"
import _ "embed"
@ -644,12 +646,24 @@ func Main () () {
if len (os.Args) == 2 {
switch os.Args[1] {
case "version", "--version", "-v" :
version.Main ("kawipiko-server")
return
case "--help", "-h" :
os.Stderr.WriteString (usageText)
if _, _error := os.Stdout.WriteString (usageText); _error == nil {
return
} else {
AbortError (_error, "[68e52113] unexpected error!")
}
case "--man" :
os.Stderr.WriteString (manualText)
if _, _error := os.Stdout.WriteString (manualText); _error == nil {
return
} else {
AbortError (_error, "[78817f08] unexpected error!")
}
}
}

View file

@ -0,0 +1,58 @@
package version
import "bytes"
import "fmt"
import "os"
import . "github.com/volution/kawipiko/lib/common"
import . "github.com/volution/kawipiko/embedded"
func Version (_executableName string, _executable string, _stream *os.File) (error) {
if _executable == "<os.Executable>" {
if _executable_0, _error := os.Executable (); _error == nil {
_executable = _executable_0
} else {
return _error
}
}
_buffer := bytes.NewBuffer (nil)
fmt.Fprintf (_buffer, "* tool : %s\n", _executableName)
fmt.Fprintf (_buffer, "* version : %s\n", BUILD_VERSION)
if _executable != "" {
fmt.Fprintf (_buffer, "* executable : %s\n", _executable)
}
fmt.Fprintf (_buffer, "* build target : %s, %s-%s, %s, %s\n", BUILD_TARGET, BUILD_TARGET_OS, BUILD_TARGET_ARCH, BUILD_COMPILER_VERSION, BUILD_COMPILER_TYPE)
fmt.Fprintf (_buffer, "* build number : %s, %s\n", BUILD_NUMBER, BUILD_TIMESTAMP)
fmt.Fprintf (_buffer, "* code & issues : %s\n", PROJECT_URL)
fmt.Fprintf (_buffer, "* sources git : %s\n", BUILD_GIT_HASH)
fmt.Fprintf (_buffer, "* sources hash : %s\n", BUILD_SOURCES_HASH)
fmt.Fprintf (_buffer, "* uname node : %s\n", UNAME_NODE)
fmt.Fprintf (_buffer, "* uname system : %s, %s, %s\n", UNAME_SYSTEM, UNAME_RELEASE, UNAME_MACHINE)
fmt.Fprintf (_buffer, "* uname hash : %s\n", UNAME_FINGERPRINT)
if _, _error := _buffer.WriteTo (_stream); _error != nil {
return _error
}
return nil
}
func Main (_executableName string) () {
if _error := Version (_executableName, "<os.Executable>", os.Stdout); _error != nil {
AbortError (_error, "[74bfa815] unexpected error!")
}
}

View file

@ -8,6 +8,7 @@ import "os"
import "github.com/volution/kawipiko/cmd/server"
import "github.com/volution/kawipiko/cmd/archiver"
import "github.com/volution/kawipiko/cmd/version"
@ -21,6 +22,9 @@ func main () () {
switch _command {
case "version", "--version", "-v" :
version.Main ("kawipiko-wrapper")
case "server" :
server.Main ()
@ -32,7 +36,7 @@ func main () () {
}
} else {
fmt.Fprintf (os.Stderr, "[!!] [3628f38a] expected command: `server` or `archiver`; aborting!\n")
fmt.Fprintf (os.Stderr, "[!!] [3628f38a] expected command: `server`, `archiver`, or `version`; aborting!\n")
}
}

29
sources/embedded/build.go Normal file
View file

@ -0,0 +1,29 @@
package embedded
import _ "embed"
//go:embed build/version.txt
var buildVersion string
//go:embed build/number.txt
var buildNumber string
//go:embed build/timestamp.txt
var buildTimestamp string
//go:embed build/sources.hash
var buildSourcesHash string
//go:embed build/sources.md5
var BuildSourcesMd5 string
//go:embed build/sources.cpio.gz
var BuildSourcesCpioGz []byte

View file

@ -0,0 +1,91 @@
package embedded
import "crypto/sha256"
import "encoding/hex"
import "io"
import "runtime"
import "strings"
import "golang.org/x/sys/unix"
var PROJECT_URL string = "https://github.com/volution/kawipiko"
var BUILD_TARGET string = "{unknown-target}"
var BUILD_TARGET_ARCH string = runtime.GOARCH
var BUILD_TARGET_OS string = runtime.GOOS
var BUILD_COMPILER_TYPE string = runtime.Compiler
var BUILD_COMPILER_VERSION string = runtime.Version ()
var BUILD_DEVELOPMENT bool = (BUILD_TARGET == "development")
var BUILD_VERSION string = strings.Trim (buildVersion, "\n")
var BUILD_NUMBER string = strings.Trim (buildNumber, "\n")
var BUILD_TIMESTAMP string = strings.Trim (buildTimestamp, "\n")
var BUILD_GIT_HASH string = "{unknown-git-hash}"
var BUILD_SOURCES_HASH string = strings.Trim (buildSourcesHash, "\n")
var UNAME_NODE string = "{unknown-node}"
var UNAME_SYSTEM string = "{unknown-system}"
var UNAME_RELEASE string = "{unknown-release}"
var UNAME_VERSION string = "{unknown-version}"
var UNAME_MACHINE string = "{unknown-machine}"
var UNAME_FINGERPRINT string = "{unknown-fingerprint}"
func init () () {
var _uname unix.Utsname
if _error := unix.Uname (&_uname); _error != nil {
panic (_error)
}
_convert := func (_bytes []byte, _default string) (string) {
_buffer := make ([]byte, 0, len (_bytes))
for _, _byte := range _bytes {
if _byte == 0 {
break
}
_buffer = append (_buffer, byte (_byte))
}
if len (_buffer) > 0 {
return string (_buffer)
} else {
return _default
}
}
UNAME_NODE = _convert (_uname.Nodename[:], "{unknown-node}")
UNAME_SYSTEM = _convert (_uname.Sysname[:], "{unknown-system}")
UNAME_RELEASE = _convert (_uname.Release[:], "{unknown-release}")
UNAME_VERSION = _convert (_uname.Version[:], "{unknown-version}")
UNAME_MACHINE = _convert (_uname.Machine[:], "{unknown-machine}")
if _index := strings.Index (UNAME_NODE, "."); _index != -1 {
UNAME_NODE = UNAME_NODE[0 : _index]
}
if UNAME_NODE == "" {
UNAME_NODE = "{unknown-node}"
}
{
_hasher := sha256.New ()
for _, _token := range []string {
"98ff673c677ffaeb481ce53a8deef977",
UNAME_NODE, UNAME_SYSTEM, UNAME_RELEASE, UNAME_VERSION, UNAME_MACHINE,
} {
io.WriteString (_hasher, _token)
_hasher.Write ([]byte { 0 })
}
_hash := hex.EncodeToString (_hasher.Sum (nil))
UNAME_FINGERPRINT = _hash [0:32]
}
}