From 10d6048f4becd95f5ea0b1f9a153d88abc35e660 Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Sun, 10 Jul 2022 00:14:45 -0700 Subject: [PATCH] Spruce up banner, add build info --- .gitignore | 2 ++ Makefile | 13 +++++++++++++ config/config.go | 50 ++++++++++++++++++++++++++++++++---------------- config/misc.go | 26 +++++++++++++++++++++++++ go.mod | 1 + go.sum | 1 + main.go | 19 +++--------------- util.go | 10 ---------- 8 files changed, 79 insertions(+), 43 deletions(-) create mode 100644 Makefile create mode 100644 config/misc.go diff --git a/.gitignore b/.gitignore index a966295..efafbbf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ logs/ data/ *.toml *.log +*.art +tcp.ac diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c051cb0 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +all: deps format check build +build: + go build -x -v -a -ldflags "-s -w -X config.sha1ver=$(gitrev) -X config.buildTime=$(now)" +clean: + rm -f tcp.ac +deps: + go mod tidy -v +run: + go run ./ +check: + go vet ./... +format: + gofmt -s -l -w *.go config/*.go diff --git a/config/config.go b/config/config.go index 966d7f8..7afc9bd 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "runtime/debug" "strings" "sync" @@ -20,10 +21,28 @@ const ( Title = "tcp.ac" ) -var ( - // Version roughly represents the applications current version. - Version = "0.0.0" -) +var binInfo map[string]string + +func init() { + binInfo = make(map[string]string) + + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + + for _, v := range info.Settings { + binInfo[v.Key] = v.Value + } + + var err error + home, err = os.UserHomeDir() + if err != nil { + println(err.Error()) + os.Exit(1) + } + initDefaults() +} var ( BaseURL, HTTPPort, HTTPBind, DBDir, LogDir, @@ -35,16 +54,16 @@ var ( ) var usage = fmt.Sprintf(` - %s v%s + %s brought to you by: --> tcp.direct <-- --c Specify config file ---nocolor disable color and banner ---banner show banner + version and exit ---genconfig write default config to 'config.toml' then exit -`, Title, Version) +--config Specify custom config file +--nocolor Disable color and banner +--genconfig Write default config to stdout and exit +--version Show version info and exit +`, Title) func printUsage() { println(usage) @@ -76,6 +95,9 @@ func argParse() { forceTrace = true case "--nocolor": noColorForce = true + case "--version": + PrintBanner() + os.Exit(0) case "-c", "--config": if len(os.Args) <= i-1 { panic("syntax error! expected file after -c") @@ -128,13 +150,7 @@ func writeConfig() { } func init() { - var err error - home, err = os.UserHomeDir() - if err != nil { - println(err.Error()) - os.Exit(1) - } - initDefaults() + } var once = &sync.Once{} diff --git a/config/misc.go b/config/misc.go new file mode 100644 index 0000000..8c4ca0e --- /dev/null +++ b/config/misc.go @@ -0,0 +1,26 @@ +package config + +import ( + "strings" + + "git.tcp.direct/kayos/common/squish" + "github.com/muesli/termenv" +) + +const Banner = "H4sIAAAAAAACA+OSjrY0szYxyH00rUE62hjK7AAjoIBBLggrKEBUmWGoIkYJkERWZWwOkgKxSNVJhBMMcrngNqB6B0kYyS2kKHg0rQXNNmxewdDRQZKFFHgAaBtBqzBCHaSiBaG4hQjHwoWJdAjQSwoq6ZklRallKgoKKimJJakqXABftjxAeAIAAA==" + +func PrintBanner() { + if noColorForce { + println("tcp.ac\n") + return + } + p := termenv.ColorProfile() + bnr, _ := squish.UnpackStr(Banner) + gr := termenv.String(binInfo["vcs.revision"][:7]).Foreground(termenv.ANSIBrightGreen).String() + born := termenv.String(binInfo["vcs.time"]).Foreground(p.Color("#1e9575")).String() + out := strings.Replace(bnr, "$gitrev$", gr, 1) + out = strings.Replace(out, "$date$", born, 1) + cout := termenv.String(out) + print(cout.Foreground(p.Color("#948DB8")).String()) + +} diff --git a/go.mod b/go.mod index 5269e9d..49ff544 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.tcp.direct/tcp.direct/tcp.ac go 1.18 require ( + git.tcp.direct/kayos/common v0.5.2 git.tcp.direct/kayos/putxt v0.0.0-20220707194005-5bc828145cc4 git.tcp.direct/tcp.direct/database v0.0.0-20220610180603-058d36edd7f0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d diff --git a/go.sum b/go.sum index 3d8c6a9..cbfc0f5 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,7 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7 git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34 h1:tvoLGGLsQ0IYKKQPweMF5qRm3qO4gcTpuzi9jAr3Wkk= git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34/go.mod h1:NX/Gqm/iy6Tg2CfcmmB/kW/qzKKrGR6o2koOKA5KWnc= git.tcp.direct/kayos/common v0.5.2 h1:3+3POz5akWQDoWOQ+L8WHf+pCd5cbLgH/fZqQkRblIc= +git.tcp.direct/kayos/common v0.5.2/go.mod h1:2PenBSSXY/kw0iO7ngPgowlU3OA9vak1obTJlxkO5nk= git.tcp.direct/kayos/putxt v0.0.0-20220707194005-5bc828145cc4 h1:HhXghmJMzXSE/3clQRECP21OIcVv0za9dyzRlryaXno= git.tcp.direct/kayos/putxt v0.0.0-20220707194005-5bc828145cc4/go.mod h1:WInY1F5uGGRQ6Bzq36OFrB240FvP9EVCDn0vqv4mEBM= git.tcp.direct/tcp.direct/database v0.0.0-20220610180603-058d36edd7f0 h1:p0DGzX6vm1xvj3OtmroTJ4eAX51FAcnYwpWmhkx6UA0= diff --git a/main.go b/main.go index 8c46e0e..9d6639a 100644 --- a/main.go +++ b/main.go @@ -1,20 +1,15 @@ package main import ( - "fmt" "os" "os/signal" "syscall" - "github.com/gin-gonic/gin" - "github.com/muesli/termenv" "github.com/rs/zerolog/log" "git.tcp.direct/tcp.direct/tcp.ac/config" ) -var Banner string = "CiAgLGQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogIDg4ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKTU04OE1NTSAsYWRQUFliYSwgOGIsZFBQWWJhLCAgICAgICxhZFBQWVliYSwgICxhZFBQWWJhLCAgCiAgODggICBhOCIgICAgICIiIDg4UCcgICAgIjhhICAgICAiIiAgICAgYFk4IGE4IiAgICAgIiIgIAogIDg4ICAgOGIgICAgICAgICA4OCAgICAgICBkOCAgICAgLGFkUFBQUFA4OCA4YiAgICAgICAgICAKICA4OCwgICI4YSwgICAsYWEgODhiLCAgICxhOCIgODg4IDg4LCAgICAsODggIjhhLCAgICxhYSAgCiAgIlk4ODggYCJZYmJkOCInIDg4YFliYmRQIicgIDg4OCBgIjhiYmRQIlk4ICBgIlliYmQ4IicgIAogICAgICAgICAgICAgICAgICA4OCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgODggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCg==" - func makeDirectories() { log.Trace().Msgf("establishing log directory presence at %s...", config.LogDir) err := os.MkdirAll(config.LogDir, 0o740) @@ -35,21 +30,13 @@ func makeDirectories() { } } -func printBanner() { - out := termenv.String(b64d(Banner)) - p := termenv.ColorProfile() - out = out.Foreground(p.Color("#948DB8")) - fmt.Println(out) -} - -func waitFor(router *gin.Engine) { +func wait() { c := make(chan os.Signal, 5) signal.Notify(c, os.Interrupt, syscall.SIGTERM) for { select { case <-c: log.Warn().Msg("Interrupt detected, shutting down gracefully...") - router. return } } @@ -57,7 +44,7 @@ func waitFor(router *gin.Engine) { func main() { config.Init() - printBanner() + config.PrintBanner() makeDirectories() log.Debug().Msg("debug enabled") log.Trace().Msg("trace enabled") @@ -72,5 +59,5 @@ func main() { } }() go serveTermbin() - waitFor(httpRouter()) + wait() } diff --git a/util.go b/util.go index f6a031a..8252cd7 100644 --- a/util.go +++ b/util.go @@ -1,8 +1,6 @@ package main import ( - "encoding/base64" - "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" ) @@ -14,11 +12,3 @@ func errThrow(c *gin.Context, respcode int, thrown error, msg string) { Err(thrown).Msg(msg) c.String(respcode, msg) } - -func b64d(str string) string { - data, err := base64.StdEncoding.DecodeString(str) - if err != nil { - return err.Error() - } - return string(data) -}