Initial commit

This commit is contained in:
MedzikUser 2021-08-11 14:27:54 +02:00
commit 31951b1561
10 changed files with 213 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Cache
/.cache
# DotFiles
/.env
/.env.*
/*.env
# Bin
*.exe
*.exe~
*.out
/dist/

26
common/checkErr.go Normal file
View File

@ -0,0 +1,26 @@
package common
import (
"github.com/sirupsen/logrus"
)
/*
This function checks for an error.
If the error isn't nil it return true and print error otherwise false.
err := errors.New("Test Error")
if common.CheckErr(err, "example error") {
return
}
fmt.Println("No error was found!")
*/
func CheckErr(err error, trace string) bool {
if err != nil {
Log.WithFields(logrus.Fields{
"trace": trace,
}).Error(err)
}
return err != nil
}

21
common/checkErr_test.go Normal file
View File

@ -0,0 +1,21 @@
package common
import (
"errors"
"testing"
)
func TestCheckErrTrue(T *testing.T) {
err := errors.New("Test")
e := CheckErr(err, "test err")
if e != true {
T.Error(e)
}
}
func TestCheckErrFalse(T *testing.T) {
e := CheckErr(nil, "test err")
if e != false {
T.Error(e)
}
}

27
common/log.go Normal file
View File

@ -0,0 +1,27 @@
package common
import (
"os"
"github.com/sirupsen/logrus"
)
/*
Info:
common.Log.Info("Info")
Debug:
common.Log.Debug("Debug")
Error:
common.Log.Error("Error")
Warn:
common.Log.Warn("Warn")
*/
var Log = &logrus.Logger{
Out: os.Stdout,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}

6
common/uptime.go Normal file
View File

@ -0,0 +1,6 @@
package common
// TODO:
func Uptime() {
}

9
convert/round.go Normal file
View File

@ -0,0 +1,9 @@
package convert
func Round(val float64) int {
if val < 0 {
return int(val - 1.0)
}
return int(val)
}

83
convert/seconds.go Normal file
View File

@ -0,0 +1,83 @@
package convert
import (
"strconv"
"time"
)
// FIXME:
func Seconds(s time.Duration) string {
hours := Round(s.Hours())
minutes := Round(s.Minutes())
seconds := Round(s.Seconds())
hours, days := HoursToDays(hours)
days, months := DaysToMonths(days)
months, years := MonthsToYears(months)
var format string
if years > 0 {
format += strconv.Itoa(years) + " years "
months -= years * 12
}
if months > 0 {
format += strconv.Itoa(months) + " months "
days -= months * 30
}
if days > 0 {
format += strconv.Itoa(days) + " days "
hours -= days * 24
}
if hours > 0 {
format += strconv.Itoa(hours) + " hours "
minutes -= hours * 24
}
if minutes > 0 {
format += strconv.Itoa(minutes) + " minutes "
seconds -= minutes * 60
}
if seconds > 0 {
format += strconv.Itoa(seconds) + " seconds"
}
return format
}
func HoursToDays(hours int) (int, int) {
var days int
for hours/24 > 0 {
days++
hours -= 24
}
return hours, days
}
func DaysToMonths(days int) (int, int) {
var months int
for days/30 > 0 {
months++
days -= 30
}
return days, months
}
func MonthsToYears(months int) (int, int) {
var years int
for months/12 > 0 {
years++
months -= 12
}
return months, years
}

13
convert/seconds_test.go Normal file
View File

@ -0,0 +1,13 @@
package convert
import (
"testing"
"time"
)
func TestSeconds(t *testing.T) {
d, err := time.ParseDuration("48h")
t.Error("1", err)
out := Seconds(d)
t.Error("2", out)
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/MedzikUser/go-utils
go 1.16
require github.com/sirupsen/logrus v1.8.1

10
go.sum Normal file
View File

@ -0,0 +1,10 @@
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=