From c66b0eb2ff59e24ed727255d31febfaf0988f782 Mon Sep 17 00:00:00 2001 From: Zeev Diukman Date: Wed, 11 Sep 2024 17:21:06 +0300 Subject: [PATCH] 1 --- .gitconfig | 2 ++ go.mod | 12 +++++++++ go.sum | 20 +++++++++++++++ helper.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 .gitconfig create mode 100644 go.mod create mode 100644 go.sum create mode 100644 helper.go diff --git a/.gitconfig b/.gitconfig new file mode 100644 index 0000000..2146dc4 --- /dev/null +++ b/.gitconfig @@ -0,0 +1,2 @@ +[url "git@github.com:zeevdiukman/z.git"] + insteadOf = https://github.com/ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..040e0ab --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module github.com/zeevdiukman/z/helper + +go 1.23.1 + +require github.com/gookit/goutil v0.6.16 + +require ( + github.com/gookit/color v1.5.4 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fe1db2d --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= +github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= +github.com/gookit/goutil v0.6.16 h1:9fRMCF4X9abdRD5+2HhBS/GwafjBlTUBjRtA5dgkvuw= +github.com/gookit/goutil v0.6.16/go.mod h1:op2q8AoPDFSiY2+qkHxcBWQMYxOLQ1GbLXqe7vrwscI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..77fb704 --- /dev/null +++ b/helper.go @@ -0,0 +1,75 @@ +package helper + +import ( + "fmt" + "log" + "net" + "net/http" + "os" + "os/signal" + "runtime" + "strings" + "syscall" + + "github.com/gookit/goutil/dump" +) + +func IsIPAddr(ip string) bool { + ipaddr := net.ParseIP(NormaliseIPAddr(ip)) + return ipaddr != nil +} + +func NormaliseIPAddr(ip string) string { + if strings.HasSuffix(ip, "/32") && strings.Contains(ip, ".") { // single host (IPv4) + ip = strings.TrimSuffix(ip, "/32") + } else { + ip = strings.TrimSuffix(ip, "/128") // single host (IPv6) + } + + return ip +} + +// fqdn format to domain format +func FtoD(fqdn string) string { + return strings.TrimSuffix(fqdn, ".") +} + +func StartHTTP() { + log.Printf("Starting HTTP at :80") + http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "DEV port 80 OK") + })) +} + +func P2(input ...any) { + dump.Print(input...) +} +func P(input ...any) { + fmt.Println(input...) +} + +func PrintOldStyleMetrics() { + runtime.GOMAXPROCS(4) + fmt.Printf("Operating system is: %s\n", runtime.GOOS) + // Get number of go routines, max os threads allocated to process and host number of cpus + numGoroutines := runtime.NumGoroutine() + fmt.Printf("The program is using %d go routines\n", numGoroutines) + maxThreads := runtime.GOMAXPROCS(0) + fmt.Printf("The program is configured to %d max threads ", maxThreads) + numCPUs := runtime.NumCPU() + fmt.Printf("- the host has %d cpus", numCPUs) + +} + +func Start(app func()) { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + // done := make(chan bool, 1) + + go func() { + app() + }() + + <-sigs + +}