2019-05-09 01:07:38 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-03-31 13:03:49 +03:00
|
|
|
"fmt"
|
2020-09-29 19:55:07 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2019-05-09 01:07:38 +03:00
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseDate(date string) (time.Time, error) {
|
|
|
|
return time.Parse("2006-01-02 15:04:05", date)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDate(date time.Time) string {
|
|
|
|
return date.Format("2006-01-02 15:04:05")
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:39:56 +03:00
|
|
|
func FormatDateHuman(date time.Time) string {
|
|
|
|
return date.Format("Mon, 02 Jan 2006 15:04")
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:07:38 +03:00
|
|
|
func ParseUserAgent(ua string) (string, string, error) {
|
2020-09-29 19:58:10 +03:00
|
|
|
re := regexp.MustCompile(`(?iU)^wakatime\/[\d+.]+\s\((\w+)-.*\)\s.+\s([^\/\s]+)-wakatime\/.+$`)
|
2019-05-09 01:07:38 +03:00
|
|
|
groups := re.FindAllStringSubmatch(ua, -1)
|
|
|
|
if len(groups) == 0 || len(groups[0]) != 3 {
|
2019-11-08 01:11:19 +03:00
|
|
|
return "", "", errors.New("failed to parse user agent string")
|
2019-05-09 01:07:38 +03:00
|
|
|
}
|
|
|
|
return groups[0][1], groups[0][2], nil
|
|
|
|
}
|
2019-05-11 18:49:56 +03:00
|
|
|
|
2020-09-29 19:55:07 +03:00
|
|
|
func MakeConnectionString(config *config.Config) string {
|
2020-10-04 11:37:38 +03:00
|
|
|
switch config.Db.Dialect {
|
2020-03-31 13:03:49 +03:00
|
|
|
case "mysql":
|
2020-09-29 19:58:10 +03:00
|
|
|
return mysqlConnectionString(config)
|
2020-03-31 13:03:49 +03:00
|
|
|
case "postgres":
|
|
|
|
return postgresConnectionString(config)
|
2020-03-31 15:43:15 +03:00
|
|
|
case "sqlite3":
|
|
|
|
return sqliteConnectionString(config)
|
2020-03-31 13:03:49 +03:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-09-29 19:58:10 +03:00
|
|
|
func mysqlConnectionString(config *config.Config) string {
|
2020-09-12 00:24:51 +03:00
|
|
|
//location, _ := time.LoadLocation("Local")
|
2020-09-12 17:32:43 +03:00
|
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
2020-10-04 11:37:38 +03:00
|
|
|
config.Db.User,
|
|
|
|
config.Db.Password,
|
|
|
|
config.Db.Host,
|
|
|
|
config.Db.Port,
|
|
|
|
config.Db.Name,
|
2020-09-12 00:24:51 +03:00
|
|
|
"Local",
|
2020-03-31 13:03:49 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-29 19:55:07 +03:00
|
|
|
func postgresConnectionString(config *config.Config) string {
|
2020-03-31 13:03:49 +03:00
|
|
|
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable",
|
2020-10-04 11:37:38 +03:00
|
|
|
config.Db.Host,
|
|
|
|
config.Db.Port,
|
|
|
|
config.Db.User,
|
|
|
|
config.Db.Name,
|
|
|
|
config.Db.Password,
|
2020-03-31 13:03:49 +03:00
|
|
|
)
|
2019-05-11 18:49:56 +03:00
|
|
|
}
|
2020-03-31 15:43:15 +03:00
|
|
|
|
2020-09-29 19:55:07 +03:00
|
|
|
func sqliteConnectionString(config *config.Config) string {
|
2020-10-04 11:37:38 +03:00
|
|
|
return config.Db.Name
|
2020-03-31 15:43:15 +03:00
|
|
|
}
|