This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
87 lines (75 loc) · 1.71 KB
/
util.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
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"strings"
"github.com/kballard/go-shellquote"
"github.com/whyrusleeping/hellabot"
)
type FlagString struct {
HasValue bool
Value string
}
func (fs FlagString) String() string {
return fs.Value
}
func (fs *FlagString) Set(s string) error {
fs.Value = s
fs.HasValue = true
return nil
}
func Usage(fset *flag.FlagSet) string {
var usage bytes.Buffer
fset.SetOutput(&usage)
fset.Usage()
return usage.String()
}
func ParseFlags(msg *hbot.Message, fset *flag.FlagSet) error {
args, err := shellquote.Split(msg.Content)
if err != nil {
return err
}
if len(args) == 0 {
args = []string{""}
}
fset.SetOutput(ioutil.Discard)
if err = fset.Parse(args[1:]); err == nil {
return nil
}
if err == flag.ErrHelp {
return errors.New(Usage(fset))
}
return err
}
func ErrorReply(bot *hbot.Bot, msg *hbot.Message, err error) {
MultiLineReply(bot, msg, err.Error())
}
func MultiLineReply(bot *hbot.Bot, msg *hbot.Message, s string) {
s = strings.Replace(s, "\t", " ", -1)
r := strings.NewReader(s)
ss := bufio.NewScanner(r)
for ss.Scan() {
bot.Reply(msg, ss.Text())
}
}
func HasPrefix(prefix string) func(*hbot.Bot, *hbot.Message) bool {
return func(bot *hbot.Bot, msg *hbot.Message) bool {
return strings.HasPrefix(msg.Content, prefix)
}
}
func HasCommand(command string) func(*hbot.Bot, *hbot.Message) bool {
return func(bot *hbot.Bot, msg *hbot.Message) bool {
words, err := shellquote.Split(msg.Content)
if err != nil || len(words) == 0 {
return false
}
return strings.TrimSpace(words[0]) == command
}
}
func ReplyTo(bot *hbot.Bot, msg *hbot.Message, s string) {
bot.Reply(msg, fmt.Sprintf("%s: %s", msg.From, s))
}