/
util.go
65 lines (53 loc) · 1.28 KB
/
util.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
65
package cointop
import (
"bytes"
"encoding/gob"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/cdyfng/coind/cointop/common/open"
)
// OpenLink opens the url in a browser
func (ct *Cointop) OpenLink() error {
ct.debuglog("openLink()")
open.URL(ct.RowLink())
return nil
}
// GetBytes returns the interface in bytes form
func GetBytes(key interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(key)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UserPreferredHomeDir returns the preferred home directory for the user
func UserPreferredHomeDir() string {
var home string
if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
} else if runtime.GOOS == "linux" {
home = os.Getenv("XDG_CONFIG_HOME")
}
if home == "" {
home, _ = os.UserHomeDir()
}
return home
}
// NormalizePath normalizes and extends the path string
func NormalizePath(path string) string {
// expand tilde
if strings.HasPrefix(path, "~/") {
path = filepath.Join(UserPreferredHomeDir(), path[2:])
}
path = strings.Replace(path, "/", string(filepath.Separator), -1)
return path
}
// Slugify returns a slugified string
func Slugify(s string) string {
s = strings.TrimSpace(strings.ToLower(s))
return s
}