This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
64 lines (58 loc) · 1.76 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package wally
import (
"strconv"
"strings"
rdb "github.com/dancannon/gorethink"
"github.com/fatih/color"
)
var (
// Success coloured text
Success = color.New(color.FgGreen)
// Info coloured text
Info = color.New(color.FgBlue)
// Warning coloured text
Warning = color.New(color.FgYellow)
// Std coloured text
Std = color.New(color.FgMagenta)
)
// DatabaseRebuild resets the database to an empty state, it also sets the
// secondary index for the index table.
func DatabaseRebuild(session *rdb.Session) {
rdb.DB(Conf.Database.Name).TableCreate(Conf.Tables.DocumentTable).Exec(session)
rdb.DB(Conf.Database.Name).TableCreate(Conf.Tables.IndexTable).Exec(session)
rdb.DB(Conf.Database.Name).Table(Conf.Tables.IndexTable).IndexCreate("word").Exec(session)
rdb.DB(Conf.Database.Name).Table(Conf.Tables.DocumentTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
rdb.DB(Conf.Database.Name).Table(Conf.Tables.IndexTable).Delete(rdb.DeleteOpts{
Durability: "soft",
ReturnChanges: false,
}).Exec(session)
}
// ToString converts an interface{} to a string, a string, byte slice or integer
// is an accepted value and converted as such, anything else returns an empty string.
func ToString(v interface{}) string {
switch v.(type) {
case string:
return v.(string)
case []byte:
return string(v.([]byte))
case int:
return strconv.Itoa(v.(int))
default:
return ""
}
}
// TruncateText returns a truncated version of a string with a suffix appended.
func TruncateText(text, suffix string, upperBound int) string {
if len(text) > upperBound {
truncated := text[:upperBound]
nextSpace := strings.LastIndex(truncated, " ")
if nextSpace > 0 {
truncated = text[:nextSpace]
}
return truncated + suffix
}
return text
}