-
Notifications
You must be signed in to change notification settings - Fork 15
/
parse_cmd.go
116 lines (92 loc) · 2.67 KB
/
parse_cmd.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
package main
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
)
var ErrUnknownCommand = errors.New("unknown command")
var ErrInvalidArguments = errors.New("invalid arguments")
func parseCmd(cmdStr string) (string, []string, error) {
cmdStr = strings.TrimSpace(cmdStr)
log.Println("The cmdStr is: ", cmdStr)
name, rest, _ := strings.Cut(cmdStr, " ")
name = strings.ToLower(name)
rest = strings.TrimSpace(rest)
switch name {
case "subscribe", "unsubscribe", "help", "status", "cookie":
if len(rest) > 0 {
return "help", nil, fmt.Errorf("%w: wanted no arguments", ErrInvalidArguments)
}
return name, nil, nil
case "version":
// Ignore any extra arguments.
return name, nil, nil
case "add-review":
if rest == "" {
return "help", nil, fmt.Errorf(`%w: wanted review content`, ErrInvalidArguments)
}
return name, []string{rest}, nil
case "get-reviews":
args := strings.Fields(rest)
switch len(args) {
case 0:
return name, nil, nil
case 1:
n, err := strconv.Atoi(args[0])
if err != nil || n < 0 {
return "help", nil, fmt.Errorf(`%w: wanted a positive integer`, ErrInvalidArguments)
}
return name, args, nil
default:
return "help", nil, fmt.Errorf(`%w: wanted a positive integer`, ErrInvalidArguments)
}
case "schedule":
args := strings.Fields(rest)
if len(args) == 0 {
return "help", nil, fmt.Errorf("%w: wanted list of days", ErrInvalidArguments)
}
var userSchedule []string
for _, day := range args {
fullDayName, err := parseDay(day)
if err != nil {
return "help", nil, fmt.Errorf("%w: %w", ErrInvalidArguments, err)
}
userSchedule = append(userSchedule, fullDayName)
}
return "schedule", userSchedule, nil
case "skip", "unskip":
// TODO(#49): Allow (un)skipping days other than tomorrow
if rest != "tomorrow" {
return "help", nil, fmt.Errorf(`%w: wanted "tomorrow"`, ErrInvalidArguments)
}
return name, []string{"tomorrow"}, nil
case "thank", "thanks":
return "thanks", nil, nil
default:
return "help", nil, fmt.Errorf("%w: %q", ErrUnknownCommand, name)
}
}
var ErrUnknownDay = errors.New("unknown day abbreviation")
// parseDay expands day name abbreviations into their canonical form.
func parseDay(word string) (string, error) {
switch strings.ToLower(word) {
case "mon", "monday":
return "monday", nil
case "tu", "tue", "tuesday":
return "tuesday", nil
case "wed", "wednesday":
return "wednesday", nil
case "th", "thu", "thurs", "thursday":
return "thursday", nil
case "fri", "friday":
return "friday", nil
case "sat", "saturday":
return "saturday", nil
case "sun", "sunday":
return "sunday", nil
default:
return "", fmt.Errorf("%w: %q", ErrUnknownDay, word)
}
}