1414package cmd
1515
1616import (
17+ "errors"
1718 "fmt"
1819 "os"
1920 "strconv"
2021 "sync"
2122
2223 "github.com/VirusTotal/vt-go/vt"
2324 "github.com/spf13/cobra"
25+ "github.com/spf13/viper"
2426)
2527
26- var notificationsPurgeCmdHelp = `Delete all hunting notifications.
28+ var notificationsDeleteCmdHelp = `Delete hunting notifications.
2729
28- This command deletes all the malware hunting notifications associated to the
30+ This command deletes the malware hunting notifications associated to the
2931currently configured API key.`
3032
31- // NewHuntingNotificationsPurgeCmd returns a command for deleting all hunting
33+ // NewHuntingNotificationsDeleteCmd returns a command for deleting all hunting
3234// notifications for the current user.
33- func NewHuntingNotificationsPurgeCmd () * cobra.Command {
35+ func NewHuntingNotificationsDeleteCmd () * cobra.Command {
3436 cmd := & cobra.Command {
35- Use : "purge" ,
36- Short : "Delete all hunting notifications" ,
37- Long : notificationsPurgeCmdHelp ,
38-
37+ Use : "delete [notification id]..." ,
38+ Short : "Delete hunting notifications" ,
39+ Long : notificationsDeleteCmdHelp ,
3940 RunE : func (cmd * cobra.Command , args []string ) error {
41+ deleteAll := viper .GetBool ("all" )
42+ deleteTag := viper .GetString ("with-tag" )
43+ if len (args ) == 0 && ! deleteAll && deleteTag == "" {
44+ return errors .New ("Specify notification id or use --all or --with-tag" )
45+ }
4046 client , err := NewAPIClient ()
4147 if err != nil {
4248 return err
4349 }
44- _ , err = client .Delete (vt .URL ("intelligence/hunting_notifications" ))
45- return err
50+ if deleteAll {
51+ _ , err = client .Delete (vt .URL ("intelligence/hunting_notifications" ))
52+ } else if deleteTag != "" {
53+ _ , err = client .Delete (vt .URL ("intelligence/hunting_notifications?tag=%s" , deleteTag ))
54+ } else {
55+ var wg sync.WaitGroup
56+ for _ , arg := range args {
57+ wg .Add (1 )
58+ go func (notificationID string ) {
59+ url := vt .URL ("intelligence/hunting_notifications/%s" , notificationID )
60+ if _ , err := client .Delete (url ); err != nil {
61+ fmt .Fprintf (os .Stderr , "%v\n " , err )
62+ }
63+ wg .Done ()
64+ }(arg )
65+ }
66+ wg .Wait ()
67+ }
68+ return nil
4669 },
4770 }
4871
72+ cmd .Flags ().BoolP ("all" , "a" , false , "delete all notifications" )
73+ cmd .Flags ().StringP ("with-tag" , "t" , "" , "delete notifications with a given tag" )
74+
4975 return cmd
5076}
5177
@@ -76,7 +102,7 @@ func NewHuntingNotificationsListCmd() *cobra.Command {
76102 addLimitFlag (cmd .Flags ())
77103 addCursorFlag (cmd .Flags ())
78104
79- cmd .AddCommand (NewHuntingNotificationsPurgeCmd ())
105+ cmd .AddCommand (NewHuntingNotificationsDeleteCmd ())
80106
81107 return cmd
82108}
@@ -90,7 +116,7 @@ func NewHuntingNotificationsCmd() *cobra.Command {
90116 }
91117
92118 cmd .AddCommand (NewHuntingNotificationsListCmd ())
93- cmd .AddCommand (NewHuntingNotificationsPurgeCmd ())
119+ cmd .AddCommand (NewHuntingNotificationsDeleteCmd ())
94120
95121 return cmd
96122}
@@ -216,7 +242,6 @@ func NewHuntingRulesetsDeleteCmd() *cobra.Command {
216242 if err != nil {
217243 return err
218244 }
219-
220245 var wg sync.WaitGroup
221246 for _ , arg := range args {
222247 wg .Add (1 )
@@ -228,7 +253,6 @@ func NewHuntingRulesetsDeleteCmd() *cobra.Command {
228253 wg .Done ()
229254 }(arg )
230255 }
231-
232256 wg .Wait ()
233257 return nil
234258 },
0 commit comments