-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
48 lines (42 loc) · 814 Bytes
/
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
package main
import (
"fmt"
"os"
"os/exec"
)
func exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
func delFileIfExists(file string) {
if exists(file) {
errIt(os.Remove(file), "")
}
}
func openFileInEditor(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
executable, err := exec.LookPath(editor)
errIt(err, "")
cmd := exec.Command(executable, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func errIt(err error, msg string) {
if err != nil {
fmt.Println(msg, err)
os.Exit(1)
}
}
func createFile(file string, content string) {
delFileIfExists(file)
f, err := os.Create(file)
errIt(err, "")
defer f.Close()
_, err = f.WriteString(content)
errIt(err, "")
}