-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·43 lines (40 loc) · 1.48 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"./maclook"
"github.com/tidwall/gjson"
)
func main() {
macAddress := flag.String("macaddress", "", "macaddress") // get macaddress from command line parameter
detail := flag.Bool("detail", false, "set to true to print detailed report") // check whether user wants to print a detailed report
apiKey := flag.String("apikey", "", "apikey obtained from macaddress.io") // get apikey from commandline
flag.Parse()
if *apiKey == "" { // if apikey is not given as command line parameter, try to get it from env
*apiKey = os.Getenv("apikey")
if *apiKey == "" { // if apikey is not present in env and commandline parameter, raise an error
fmt.Println("Please provide apikey with -apikey=APIKEY_HERE")
fmt.Println("Run with -h for help")
os.Exit(1)
}
}
if *macAddress == "" {
fmt.Println("Please provide macaddress with -macaddress=MACADDRESS_HERE")
fmt.Println("Run with -h for help")
os.Exit(1)
}
maclook.SetAPIMacAdressIO(*apiKey) // setup macaddress api and apikey as macaddress.io
jsonText := maclook.MacLookUp(*macAddress)
if *detail {
fmt.Println(string(jsonText))
} else {
m, ok := gjson.Parse(string(jsonText)).Value().(map[string]interface{}) // 3rd party library to parse deeply nested json
if !ok {
fmt.Println("ERROR: couldnt parse json")
os.Exit(1)
}
fmt.Println("MAC Address :", *macAddress)
fmt.Println("Company Name :", m["vendorDetails"].(map[string]interface{})["companyName"])
}
}