-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
51 lines (44 loc) · 908 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
49
50
51
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
)
func OpenFileInEditor(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
executable, err := exec.LookPath(editor)
if err != nil {
return err
}
cmd := exec.Command(executable, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func TempFile(buf []byte) ([]byte, error) {
tmpfile, err := ioutil.TempFile("/var/tmp", "goctl-*")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(buf); err != nil {
tmpfile.Close()
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
if err := OpenFileInEditor(tmpfile.Name()); err != nil {
return []byte{}, err
}
content, err := ioutil.ReadFile(tmpfile.Name())
if err != nil {
return []byte{}, err
}
return content, nil
}