-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (91 loc) · 2.31 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
package main
import (
snippetCli "github.com/baopham/snip/cli"
"github.com/fatih/color"
"github.com/urfave/cli"
"os"
)
func Action(fn func(c *cli.Context) error) func(c *cli.Context) error {
actor := func(c *cli.Context) error {
err := fn(c)
if err != nil {
color.Red(err.Error())
}
return err
}
return actor
}
func main() {
app := cli.NewApp()
app.Version = "3.0.0"
app.Usage = "Save snippets: commands, texts, emoji, etc."
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: `snip add -k="port" -c="lsof -i :{p}" -desc="List processes listening on a particular port"`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "keyword, k",
Usage: "keyword for the snippet",
},
cli.StringFlag{
Name: "content, c",
Usage: "the snippet content",
},
cli.StringFlag{
Name: "description, desc",
Usage: "the snippet description",
},
},
Action: Action(snippetCli.Add),
},
{
Name: "search",
Aliases: []string{"s"},
Usage: "search for snippets: snip search port",
Action: Action(snippetCli.Search),
BashComplete: snippetCli.Autocomplete,
},
{
Name: "generate",
Aliases: []string{"g"},
Usage: "generate the snippet by keyword: snip g port p={9000}",
Action: Action(snippetCli.Generate),
BashComplete: snippetCli.Autocomplete,
},
{
Name: "execute",
Aliases: []string{"x"},
Usage: "execute the snippet by keyword: snip x port p={9000}",
Action: Action(snippetCli.Execute),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "output, o",
Usage: "execute the snippet and save the output to clipboard",
},
cli.BoolFlag{
Name: "force, f",
Usage: "skip the prompt and force to execute",
},
},
BashComplete: snippetCli.Autocomplete,
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all saved snippets: snip list",
Action: Action(snippetCli.List),
BashComplete: snippetCli.Autocomplete,
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "remove a saved snippet: snip remove port",
Action: Action(snippetCli.Remove),
BashComplete: snippetCli.Autocomplete,
},
}
app.Run(os.Args)
}