-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_edit_neno.go
58 lines (52 loc) · 1.01 KB
/
cmd_edit_neno.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
package main
import (
"fmt"
"github.com/urfave/cli/v2"
"io/ioutil"
"os"
"os/exec"
"time"
)
func editCmd() *cli.Command {
return &cli.Command{
Name: "edit",
Usage: "edit",
UsageText: "使用vim 进行编辑",
Action: func(c *cli.Context) error {
neno := execTextEditor()
if neno == "panic]" {
fmt.Println("请先安装vim")
return nil
}
if neno == "" {
fmt.Println("没有输入任何内容")
return nil
}
return AddNeno(c, neno)
},
}
}
func execTextEditor() string {
filePath := "" + time.Now().Format(time.RFC3339) + ".txt"
cmd := exec.Command("vim", filePath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return "panic]"
}
tmpData, err := ioutil.ReadFile(filePath)
if err != nil {
return "panic]"
}
defer removeTmpFile(filePath, false)
return string(tmpData)
}
func removeTmpFile(filePath string, removeAll bool) {
realFilePath := filePath
var err error
err = os.Remove(realFilePath)
if err != nil {
panic(err)
}
}