/
domains.go
79 lines (63 loc) · 1.61 KB
/
domains.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"errors"
"fmt"
"github.com/function61/gokit/osutil"
"github.com/function61/james/pkg/domainwhois/domainwhoiswhoisxmlapi"
"github.com/scylladb/termtables"
"github.com/spf13/cobra"
)
func domainsEntry() *cobra.Command {
cmd := &cobra.Command{
Use: "domains",
Short: "Domain related commands",
}
cmd.AddCommand(&cobra.Command{
Use: "ls",
Short: "List tracked domains",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(listDomains())
},
})
cmd.AddCommand(&cobra.Command{
Use: "add [domain]",
Short: "Start tracking whois data of domain",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(addDomain(args[0]))
},
})
return cmd
}
func listDomains() error {
jamesfile, err := readJamesfile()
if err != nil {
return err
}
tbl := termtables.CreateTable()
tbl.AddHeaders("Domain", "Registrant", "Registrar", "Expires")
for _, domain := range jamesfile.File.Domains {
tbl.AddRow(
domain.Domain,
domain.RegistrantName,
domain.Registrar,
domain.Expires.Format("2006-01-02"))
}
fmt.Println(tbl.Render())
return nil
}
func addDomain(name string) error {
jamesfile, err := readJamesfile()
osutil.ExitIfError(err)
if jamesfile.File.Credentials.WhoisXmlApi == nil {
return errors.New("credentials not set")
}
svc := domainwhoiswhoisxmlapi.New(string(*jamesfile.File.Credentials.WhoisXmlApi))
whoisData, err := svc.Whois(name)
if err != nil {
return err
}
jamesfile.File.Domains = append(jamesfile.File.Domains, *whoisData)
return writeJamesfile(&jamesfile.File)
}