From bbca9d709647f566850114f77f16c813f925ddec Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Fri, 2 Sep 2022 13:59:46 +0300 Subject: [PATCH] [sources] Add support for displaying version and other information (based on `z-run` code). --- sources/cmd/archiver/archiver.go | 22 ++++++-- sources/cmd/server/server.go | 22 ++++++-- sources/cmd/version/version.go | 58 ++++++++++++++++++++ sources/cmd/wrapper.go | 6 ++- sources/embedded/build.go | 29 ++++++++++ sources/embedded/variables.go | 91 ++++++++++++++++++++++++++++++++ 6 files changed, 219 insertions(+), 9 deletions(-) create mode 100644 sources/cmd/version/version.go create mode 100644 sources/embedded/build.go create mode 100644 sources/embedded/variables.go diff --git a/sources/cmd/archiver/archiver.go b/sources/cmd/archiver/archiver.go index bbfacb2..62516f5 100644 --- a/sources/cmd/archiver/archiver.go +++ b/sources/cmd/archiver/archiver.go @@ -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) - return + if _, _error := os.Stdout.WriteString (usageText); _error == nil { + return + } else { + AbortError (_error, "[5418de54] unexpected error!") + } + case "--man" : - os.Stderr.WriteString (manualText) - return + if _, _error := os.Stdout.WriteString (manualText); _error == nil { + return + } else { + AbortError (_error, "[5418de54] unexpected error!") + } } } diff --git a/sources/cmd/server/server.go b/sources/cmd/server/server.go index 8f329cc..9495e1a 100644 --- a/sources/cmd/server/server.go +++ b/sources/cmd/server/server.go @@ -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) - return + if _, _error := os.Stdout.WriteString (usageText); _error == nil { + return + } else { + AbortError (_error, "[68e52113] unexpected error!") + } + case "--man" : - os.Stderr.WriteString (manualText) - return + if _, _error := os.Stdout.WriteString (manualText); _error == nil { + return + } else { + AbortError (_error, "[78817f08] unexpected error!") + } } } diff --git a/sources/cmd/version/version.go b/sources/cmd/version/version.go new file mode 100644 index 0000000..7708a5f --- /dev/null +++ b/sources/cmd/version/version.go @@ -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 == "" { + 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.Stdout); _error != nil { + AbortError (_error, "[74bfa815] unexpected error!") + } +} + diff --git a/sources/cmd/wrapper.go b/sources/cmd/wrapper.go index 28e8bb7..11c1f90 100644 --- a/sources/cmd/wrapper.go +++ b/sources/cmd/wrapper.go @@ -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") } } diff --git a/sources/embedded/build.go b/sources/embedded/build.go new file mode 100644 index 0000000..43336db --- /dev/null +++ b/sources/embedded/build.go @@ -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 + diff --git a/sources/embedded/variables.go b/sources/embedded/variables.go new file mode 100644 index 0000000..42688f6 --- /dev/null +++ b/sources/embedded/variables.go @@ -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] + } +} +