First working version. Can add Projects and Tasks
This commit is contained in:
27
util/array.go
Normal file
27
util/array.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package util
|
||||
|
||||
import "reflect"
|
||||
|
||||
// InArray checks is val exists in a Slice
|
||||
func InArray(val interface{}, array interface{}) bool {
|
||||
return AtArrayPosition(val, array) != -1
|
||||
}
|
||||
|
||||
// AtArrayPosition find the int position of val in a Slice
|
||||
func AtArrayPosition(val interface{}, array interface{}) (index int) {
|
||||
index = -1
|
||||
|
||||
switch reflect.TypeOf(array).Kind() {
|
||||
case reflect.Slice:
|
||||
s := reflect.ValueOf(array)
|
||||
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
|
||||
index = i
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
34
util/env.go
Normal file
34
util/env.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
// "github.com/subosito/gotenv"
|
||||
)
|
||||
|
||||
// func init() {
|
||||
// gotenv.Load()
|
||||
// }
|
||||
|
||||
func GetEnvInt(key string, defaultVal int) int {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
if i, err := strconv.Atoi(v); err == nil {
|
||||
return i
|
||||
} else {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func GetEnvStr(key, defaultVal string) string {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
return v
|
||||
}
|
||||
|
||||
return defaultVal
|
||||
}
|
||||
51
util/util.go
Normal file
51
util/util.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/asdine/storm/v3"
|
||||
)
|
||||
|
||||
// ConnectStorm Create database connection
|
||||
func ConnectStorm() *storm.DB {
|
||||
db, err := storm.Open(GetEnvStr("DB_FILE", "geek-life.db"))
|
||||
FatalIfError(err, "Could not connect Embedded Database File")
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
// UnixToTime create time.Time from string timestamp
|
||||
func UnixToTime(timestamp string) time.Time {
|
||||
parts := strings.Split(timestamp, ".")
|
||||
i, err := strconv.ParseInt(parts[0], 10, 64)
|
||||
if LogIfError(err, "Could not parse timestamp : "+timestamp+" (using current time instead)") {
|
||||
return time.Unix(i, 0)
|
||||
}
|
||||
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// LogIfError logs the error and returns true on Error. think as IfError
|
||||
func LogIfError(err error, msgOrPattern string, args ...interface{}) bool {
|
||||
if err != nil {
|
||||
message := fmt.Sprintf(msgOrPattern, args...)
|
||||
log.Printf("%s: %w\n", message, err)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// FatalIfError logs the error and Exit program on Error
|
||||
func FatalIfError(err error, msgOrPattern string, args ...interface{}) {
|
||||
message := fmt.Sprintf(msgOrPattern, args...)
|
||||
|
||||
if LogIfError(err, message) {
|
||||
log.Fatal("FATAL ERROR: Exiting program! - ", message, "\n")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user