-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandnew.go
88 lines (67 loc) · 1.52 KB
/
commandnew.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
package main
import (
"bufio"
"fmt"
"github.com/geekdada/flomo-cli/client"
"github.com/pkg/errors"
"io"
"log"
"os"
"strings"
)
type NewCommand struct {
Verbose bool `short:"V" long:"verbose" description:"Show verbose debug information"`
Api string `long:"api" description:"flomo API address" env:"FLOMO_API"`
Tag string `short:"t" long:"tag" description:"Additional tag"`
}
func (x *NewCommand) Usage() string {
return "[new command options] <memo content>"
}
func (x *NewCommand) Execute(args []string) error {
code, err := x.Run(args)
os.Exit(code)
return err
}
func (x *NewCommand) Run(args []string) (int, error) {
var content string
if isInputFromPipe() {
content = getStdinContent(os.Stdin)
} else {
content = strings.Join(args, " ")
}
if content == "" {
return 1, errors.New("you must specify the content of the memo")
}
memo := client.Memo{
Content: content,
Tag: x.Tag,
Api: x.Api,
}
responseMessage, err := memo.Submit(x.Verbose)
if err != nil {
log.Println(err)
return 1, err
}
log.Println(*responseMessage)
return 0, nil
}
func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode() & os.ModeCharDevice == 0
}
func getStdinContent(r io.Reader) string {
var runes []rune
var output string
reader := bufio.NewReader(r)
for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
runes = append(runes, input)
}
for j := 0; j < len(runes); j++ {
output += fmt.Sprintf("%c", runes[j])
}
return output
}