This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
113 lines (102 loc) · 2.44 KB
/
utils.go
File metadata and controls
113 lines (102 loc) · 2.44 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
)
var Version = "0.2.0"
// Default editor to be used if for some reason $EDITOR env variable is not set
const defaultEditor = "vim"
// List current GoNote version.
func ListVersion() string {
return fmt.Sprintf("GoNote Ver.%s", Version)
}
// Generates unique filename for temp file.
func GenerateRandomHandle() string {
return fmt.Sprintf("go_note_%s.txt", time.Now().Format("20060102150405"))
}
// Converts timestamp to Date readable to humans
func HumanDate(d string) string {
t := time.Unix(GetSimpleNoteTimestamp(d), 0)
return t.Format("2006-01-02 15:04:05")
}
// GetSimpleNoteTimestamp returns proper int timestamp parsed from SimpleNote date field.
func GetSimpleNoteTimestamp(d string) int64 {
d = strings.Split(d, ".")[0] // Get only seconds
i, _ := strconv.Atoi(d)
return int64(i)
}
// Check if value is in array.
func CheckIn(needle string, haystack []string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}
// ToString converts some types used as flags values to string.
// We don't have to care about floats here (for now at least).
func ConvertToString(val interface{}) string {
switch val.(type) {
case string:
return val.(string)
case int:
return strconv.Itoa(val.(int))
case bool:
return strconv.FormatBool(val.(bool))
default:
// Should never happen, will fail (most probably)
return val.(string)
}
}
func ParseTags(tags []string) (tagString string) {
tc := make([]string, len(tags))
for i, t := range tags {
tc[i] = tagPrefix + t
}
return strings.Join(tc, ", ")
}
// writeToFile opens external editor with predetermined temporary file
// after editor is closed reads data from the temp file and deletes it.
func WriteToFile(prevContent string) (content string, err error) {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = defaultEditor
}
fpath := path.Join(os.TempDir(), GenerateRandomHandle())
f, err := os.Create(fpath)
if err != nil {
return
}
defer f.Close()
// This is used in edit action
if prevContent != "" {
_, err = f.WriteString(prevContent)
if err != nil {
return
}
}
cmd := exec.Command(editor, fpath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
if err != nil {
return
}
c, err := ioutil.ReadFile(fpath)
if err != nil {
return
}
return string(c), nil
}