-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
72 lines (58 loc) · 1.47 KB
/
lookup.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
package main
import (
"fmt"
"os"
"strings"
sdk "github.com/elmasy-com/columbus-sdk"
"github.com/elmasy-com/elnet/domain"
)
func lookupHelp() {
fmt.Printf("USAGE\n")
fmt.Printf(" %s lookup <domain>\n", os.Args[0])
fmt.Printf("\n")
fmt.Printf("RETURN\n")
fmt.Printf(" Returns a newline separated list of the full hostnames.\n")
fmt.Printf("\n")
fmt.Printf("EXAMPLE\n")
fmt.Printf(" %s lookup example.com -> Lookup for subdomains of example.com\n", os.Args[0])
}
func lookup(d string) {
if !domain.IsValid(d) {
fmt.Fprintf(os.Stderr, "Failed to lookup for %s: invalid domain\n", d)
os.Exit(1)
}
if os.Getenv("COLUMBUS_URI") != "" {
sdk.SetURI(os.Getenv("COLUMBUS_URI"))
}
subs, err := sdk.Lookup(d, false)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to lookup for %s: %s\n", d, err)
os.Exit(1)
}
for i := range subs {
if subs[i] == "" {
fmt.Printf("%s\n", d)
continue
}
fmt.Printf("%s.%s\n", subs[i], d)
}
}
func Lookup() {
// Fewer arguments than needed
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Domain for lookup is missing!\n")
fmt.Fprintf(os.Stderr, "Use \"%s lookup help\" to get help.\n", os.Args[0])
os.Exit(1)
}
// More arguments than needed
if len(os.Args) > 3 {
fmt.Fprintf(os.Stderr, "Too much arguments: %s\n", strings.Join(os.Args[2:], " "))
fmt.Fprintf(os.Stderr, "Use \"%s lookup help\" to get help.\n", os.Args[0])
os.Exit(1)
}
if os.Args[2] == "help" {
lookupHelp()
os.Exit(0)
}
lookup(os.Args[2])
}