First working version. Can add Projects and Tasks

This commit is contained in:
Anis Ahmad
2020-05-25 23:18:48 +06:00
parent 267995124b
commit 6f9580d98a
12 changed files with 610 additions and 0 deletions

27
util/array.go Normal file
View 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
View 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
View 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")
}
}