-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_ignore.go
More file actions
82 lines (74 loc) · 1.71 KB
/
cmd_ignore.go
File metadata and controls
82 lines (74 loc) · 1.71 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
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(&cobra.Command{
Use: "ignore <name>",
Short: "Ignore a contact so it never appears in triage or check",
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 updated, skipped int
for _, m := range matches {
if isIgnored(m.obj.Card) {
skipped++
continue
}
updated++
}
if !dryRun && updated > 0 {
// Reset counts and actually perform the update
updated = 0
ctx := context.Background()
for _, m := range matches {
if isIgnored(m.obj.Card) {
continue
}
setIgnored(m.obj.Card)
if _, err := m.client.PutAddressObject(ctx, m.obj.Path, m.obj.Card); err != nil {
return fmt.Errorf("updating contact: %w", err)
}
updated++
}
}
if isJSONMode(cmd) {
out := map[string]interface{}{
"action": "ignore",
"name": name,
"accounts": updated,
"skipped": skipped,
}
if dryRun {
out["dry_run"] = true
}
return printJSON(cmd, out)
}
if dryRun {
if updated == 0 {
fmt.Printf("%s is already ignored\n", name)
} else {
fmt.Printf("Would ignore %s (dry run)\n", name)
}
} else if updated == 0 {
fmt.Printf("%s is already ignored\n", name)
} else if len(matches) > 1 {
fmt.Printf("Ignored %s (%d accounts)\n", name, updated)
} else {
fmt.Printf("Ignored %s\n", name)
}
return nil
},
})
}