-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.go
80 lines (66 loc) · 1.52 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
package main
import (
"fmt"
"os"
log "github.com/inconshreveable/log15"
"github.com/lemonade-command/lemonade/client"
"github.com/lemonade-command/lemonade/lemon"
"github.com/lemonade-command/lemonade/server"
)
var logLevelMap = map[int]log.Lvl{
0: log.LvlDebug,
1: log.LvlInfo,
2: log.LvlWarn,
3: log.LvlError,
4: log.LvlCrit,
}
func main() {
cli := &lemon.CLI{
In: os.Stdin,
Out: os.Stdout,
Err: os.Stderr,
}
os.Exit(Do(cli, os.Args))
}
func Do(c *lemon.CLI, args []string) int {
logger := log.New()
logger.SetHandler(log.LvlFilterHandler(log.LvlError, log.StdoutHandler))
if err := c.FlagParse(args, false); err != nil {
writeError(c, err)
return lemon.FlagParseError
}
logLevel := logLevelMap[c.LogLevel]
logger.SetHandler(log.LvlFilterHandler(logLevel, log.StdoutHandler))
if c.Help {
fmt.Fprint(c.Err, lemon.Usage)
return lemon.Help
}
lc := client.New(c, logger)
var err error
switch c.Type {
case lemon.OPEN:
logger.Debug("Opening URL")
err = lc.Open(c.DataSource, c.TransLocalfile, c.TransLoopback)
case lemon.COPY:
logger.Debug("Copying text")
err = lc.Copy(c.DataSource)
case lemon.PASTE:
logger.Debug("Pasting text")
var text string
text, err = lc.Paste()
c.Out.Write([]byte(text))
case lemon.SERVER:
logger.Debug("Starting Server")
err = server.Serve(c, logger)
default:
panic("Unreachable code")
}
if err != nil {
writeError(c, err)
return lemon.RPCError
}
return lemon.Success
}
func writeError(c *lemon.CLI, err error) {
fmt.Fprintln(c.Err, err.Error())
}