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
|
package commands
import (
"database/sql"
"fmt"
"regexp"
"strings"
"punchcard/internal/context"
"punchcard/internal/queries"
"github.com/spf13/cobra"
)
func NewAddClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "client <name> [<email>]",
Short: "Add a new client",
Long: "Add a new client to the database. Name can include email in format 'Name <email@domain.com>'",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
name, email := parseNameAndEmail(args)
billableRateFloat, _ := cmd.Flags().GetFloat64("hourly-rate")
billableRate := int64(billableRateFloat * 100)
q := context.GetDB(cmd.Context())
if q == nil {
return fmt.Errorf("database not available in context")
}
var emailParam sql.NullString
if email != "" {
emailParam = sql.NullString{String: email, Valid: true}
}
var billableRateParam sql.NullInt64
if billableRate > 0 {
billableRateParam = sql.NullInt64{Int64: billableRate, Valid: true}
}
client, err := q.CreateClient(cmd.Context(), queries.CreateClientParams{
Name: name,
Email: emailParam,
BillableRate: billableRateParam,
})
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}
output := fmt.Sprintf("Created client: %s", client.Name)
if client.Email.Valid {
output += fmt.Sprintf(" <%s>", client.Email.String)
}
output += fmt.Sprintf(" (ID: %d)\n", client.ID)
cmd.Print(output)
return nil
},
}
cmd.Flags().Float64P("hourly-rate", "r", 0, "Default hourly billable rate for this client")
return cmd
}
func parseNameAndEmail(args []string) (string, string) {
nameArg := args[0]
var emailArg string
if len(args) > 1 {
emailArg = args[1]
}
if emailArg != "" {
if matches := emailAndNameRegex.FindStringSubmatch(emailArg); matches != nil {
emailArg = strings.TrimSpace(matches[2])
}
}
if matches := emailAndNameRegex.FindStringSubmatch(nameArg); matches != nil {
nameArg = strings.TrimSpace(matches[1])
if emailArg == "" {
emailArg = strings.TrimSpace(matches[2])
}
}
return nameArg, emailArg
}
var emailAndNameRegex = regexp.MustCompile(`^(.+?)<([^>]+@[^>]+)>$`)
|