-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_snooze.go
More file actions
139 lines (125 loc) · 3.33 KB
/
cmd_snooze.go
File metadata and controls
139 lines (125 loc) · 3.33 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
)
func init() {
snoozeCmd := &cobra.Command{
Use: "snooze <name>",
Short: "Snooze a contact until a future date",
Long: "Suppress a contact from check/triage until a given date. Use --until with an absolute date (2026-04-01) or relative duration (2m, 6w).",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
until, _ := cmd.Flags().GetString("until")
if until == "" {
return fmt.Errorf("--until is required")
}
t, err := parseUntil(until)
if err != nil {
return err
}
cfg, err := loadConfig()
if err != nil {
return err
}
matches, err := findAllContactsMulti(cfg, args[0])
if err != nil {
return err
}
name := contactName(*matches[0].obj)
dryRun := isDryRun(cmd)
if !dryRun {
ctx := context.Background()
for _, m := range matches {
setSnoozeUntil(m.obj.Card, t)
if _, err := m.client.PutAddressObject(ctx, m.obj.Path, m.obj.Card); err != nil {
return fmt.Errorf("updating contact: %w", err)
}
}
}
if isJSONMode(cmd) {
out := map[string]interface{}{
"action": "snooze",
"name": name,
"until": t.Format("2006-01-02"),
"accounts": len(matches),
}
if dryRun {
out["dry_run"] = true
}
return printJSON(cmd, out)
}
if dryRun {
fmt.Printf("Would snooze %s until %s (dry run)\n", name, t.Format("2006-01-02"))
} else if len(matches) > 1 {
fmt.Printf("Snoozed %s until %s (%d accounts)\n", name, t.Format("2006-01-02"), len(matches))
} else {
fmt.Printf("Snoozed %s until %s\n", name, t.Format("2006-01-02"))
}
return nil
},
}
snoozeCmd.Flags().String("until", "", "Date to snooze until (e.g. 2026-04-01 or 2m)")
rootCmd.AddCommand(snoozeCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "unsnooze <name>",
Short: "Remove snooze from a contact",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadConfig()
if err != nil {
return err
}
matches, err := findAllContactsMulti(cfg, args[0])
if err != nil {
return err
}
name := contactName(*matches[0].obj)
dryRun := isDryRun(cmd)
var wouldUpdate int
for _, m := range matches {
if _, ok := getSnoozeUntil(m.obj.Card); ok {
wouldUpdate++
}
}
if !dryRun && wouldUpdate > 0 {
ctx := context.Background()
for _, m := range matches {
if _, ok := getSnoozeUntil(m.obj.Card); !ok {
continue
}
removeSnoozeUntil(m.obj.Card)
if _, err := m.client.PutAddressObject(ctx, m.obj.Path, m.obj.Card); err != nil {
return fmt.Errorf("updating contact: %w", err)
}
}
}
if isJSONMode(cmd) {
out := map[string]interface{}{
"action": "unsnooze",
"name": name,
"accounts": wouldUpdate,
}
if dryRun {
out["dry_run"] = true
}
return printJSON(cmd, out)
}
if dryRun {
if wouldUpdate == 0 {
fmt.Printf("%s is not snoozed\n", name)
} else {
fmt.Printf("Would unsnooze %s (dry run)\n", name)
}
} else if wouldUpdate == 0 {
fmt.Printf("%s is not snoozed\n", name)
} else if wouldUpdate > 1 {
fmt.Printf("Unsnoozed %s (%d accounts)\n", name, wouldUpdate)
} else {
fmt.Printf("Unsnoozed %s\n", name)
}
return nil
},
})
}