-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
117 lines (98 loc) · 2.17 KB
/
main.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
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
114
115
116
117
package main
import (
"github.com/gobs/args"
"github.com/peterh/liner"
"github.com/urfave/cli/v2"
"io"
"log"
"os"
"sort"
)
var (
historyFile = "neno_command_history.txt"
)
func main() {
app := &cli.App{
Name: "neno",
Usage: "在命令行中记录neno笔记",
UsageText: "neno [global options] command [command options] [arguments...]",
Description: "neno 的一个命令行工具",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "token",
Aliases: []string{"T"},
Usage: "Your github token",
EnvVars: []string{"githubToken"},
},
&cli.StringFlag{
Name: "repo",
Aliases: []string{"R"},
Usage: "Store your neno notes in a specific repo",
EnvVars: []string{"githubRepo"},
},
&cli.StringFlag{
Name: "username",
Aliases: []string{"U"},
Usage: "Your github username",
EnvVars: []string{"githubUsername"},
},
},
Commands: []*cli.Command{
addCmd(),
//ShowTags(),
editCmd(),
exitCmd(),
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowAppHelp(c)
line := newLiner()
defer closeLiner(line)
for {
if commandLine, err := line.Prompt("NENO > "); err == nil {
line.AppendHistory(commandLine)
cmdArgs := args.GetArgs(commandLine)
if len(cmdArgs) == 0 {
continue
}
s := []string{os.Args[0]}
s = append(s, cmdArgs...)
closeLiner(line)
c.App.Run(s)
line = newLiner()
} else if err == liner.ErrPromptAborted || err == io.EOF {
break
} else {
log.Print("Error reading line: ", err)
continue
}
}
}
return nil
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func newLiner() *liner.State {
line := liner.NewLiner()
line.SetCtrlCAborts(true)
if f, err := os.Open(historyFile); err == nil {
line.ReadHistory(f)
f.Close()
}
return line
}
func closeLiner(line *liner.State) {
if f, err := os.Create(historyFile); err != nil {
log.Print("Error writing history file: ", err)
} else {
line.WriteHistory(f)
f.Close()
}
line.Close()
}