-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_add.go
61 lines (54 loc) · 1.14 KB
/
cmd_add.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
60
61
package main
import (
"context"
"flag"
"fmt"
"net"
"os"
"strings"
"github.com/google/subcommands"
)
type cmdAdd struct {
label string
address string
}
//NewCmdAdd shortcut to create new command
func newCmdAdd() *cmdAdd {
cmd := &cmdAdd{}
return cmd
}
func (*cmdAdd) Name() string {
return "add"
}
func (*cmdAdd) Synopsis() string { return "Add a new entry" }
func (*cmdAdd) Usage() string {
return `add -address someaddress -label somelabel:
Add a new entry
`
}
func (p *cmdAdd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.label, "label", "", "label to add to the address")
f.StringVar(&p.address, "address", "", "address to add")
}
func (p *cmdAdd) Execute(_ context.Context, f *flag.FlagSet,
_ ...interface{}) subcommands.ExitStatus {
database := Load()
address := p.address
if !strings.Contains(address, "/") {
address = address + "/32"
}
_, _, e := net.ParseCIDR(address)
if e != nil {
fmt.Printf("%s", e.Error())
os.Exit(1)
}
d := DatabaseEntry{
IP: address,
}
d.Labels = make([]string, 0)
d.Labels = append(d.Labels, p.label)
database = append(database, d)
Save(database)
fmt.Printf("Added and updated")
return 0
}