commit 31951b1561eac3ba22451d18b856e292f4152525 Author: MedzikUser <87065584+MedzikUser@users.noreply.github.com> Date: Wed Aug 11 14:27:54 2021 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efd1d1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Cache +/.cache + +# DotFiles +/.env +/.env.* +/*.env + +# Bin +*.exe +*.exe~ +*.out +/dist/ diff --git a/common/checkErr.go b/common/checkErr.go new file mode 100644 index 0000000..1bf3a88 --- /dev/null +++ b/common/checkErr.go @@ -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 +} diff --git a/common/checkErr_test.go b/common/checkErr_test.go new file mode 100644 index 0000000..21dd1c7 --- /dev/null +++ b/common/checkErr_test.go @@ -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) + } +} diff --git a/common/log.go b/common/log.go new file mode 100644 index 0000000..9c09529 --- /dev/null +++ b/common/log.go @@ -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, +} diff --git a/common/uptime.go b/common/uptime.go new file mode 100644 index 0000000..8387d86 --- /dev/null +++ b/common/uptime.go @@ -0,0 +1,6 @@ +package common + +// TODO: +func Uptime() { + +} diff --git a/convert/round.go b/convert/round.go new file mode 100644 index 0000000..86a216c --- /dev/null +++ b/convert/round.go @@ -0,0 +1,9 @@ +package convert + +func Round(val float64) int { + if val < 0 { + return int(val - 1.0) + } + + return int(val) +} diff --git a/convert/seconds.go b/convert/seconds.go new file mode 100644 index 0000000..4d4ca63 --- /dev/null +++ b/convert/seconds.go @@ -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 +} diff --git a/convert/seconds_test.go b/convert/seconds_test.go new file mode 100644 index 0000000..75533da --- /dev/null +++ b/convert/seconds_test.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..074c1aa --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/MedzikUser/go-utils + +go 1.16 + +require github.com/sirupsen/logrus v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..59bd790 --- /dev/null +++ b/go.sum @@ -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=