-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_delete.go
59 lines (51 loc) · 1.08 KB
/
cmd_delete.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
package main
import (
"context"
"flag"
"fmt"
"net"
"sort"
"strings"
"github.com/google/subcommands"
)
type cmdDelete struct {
address string
}
func (*cmdDelete) Name() string {
return "delete"
}
func (*cmdDelete) Synopsis() string { return "Delete an entry" }
func (*cmdDelete) Usage() string {
return `delete addresstodelete:
Deletes an entry from the list.
`
}
func (p *cmdDelete) SetFlags(f *flag.FlagSet) {
}
func newCmdDelete() *cmdDelete {
cmd := &cmdDelete{}
return cmd
}
func (p *cmdDelete) Execute(_ context.Context, f *flag.FlagSet,
_ ...interface{}) subcommands.ExitStatus {
database := Load()
if !strings.Contains(p.address, "/32") {
p.address = p.address + "/32"
}
_, _, e := net.ParseCIDR(p.address)
if e != nil {
fmt.Printf("%s", e.Error())
return 1
}
idx := sort.Search(len(database), func(i int) bool {
return p.address == database[i].IP
})
if idx > -1 && idx < len(database) {
database = append(database[:idx], database[idx+1:]...)
Save(database)
fmt.Printf("Deleted and updated")
} else {
fmt.Print("Address not found")
}
return 0
}