awl/main.go
Sam Therapy 92812c337f
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Another day's work
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2022-07-21 19:06:46 -05:00

75 lines
1.5 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"os"
"strings"
"time"
"git.froth.zone/sam/awl/cli"
"git.froth.zone/sam/awl/query"
"gopkg.in/yaml.v2"
)
var version = "DEV"
func main() {
opts, err := cli.ParseCLI(version)
if err != nil {
// TODO: Make not ew
if errors.Is(err, cli.ErrNotError) || strings.Contains(err.Error(), "help requested") {
os.Exit(0)
}
opts.Logger.Fatal(err)
os.Exit(1)
}
resp, err := query.CreateQuery(opts)
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
}
switch {
case opts.JSON:
json, err := json.MarshalIndent(resp.DNS, "", " ")
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
}
fmt.Println(string(json))
case opts.XML:
xml, err := xml.MarshalIndent(resp.DNS, "", " ")
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
}
fmt.Println(string(xml))
case opts.YAML:
yaml, err := yaml.Marshal(resp.DNS)
if err != nil {
opts.Logger.Fatal(err)
os.Exit(1)
}
fmt.Println(string(yaml))
default:
if !opts.Short {
// Print everything
fmt.Println(resp.DNS)
fmt.Println(";; Query time:", resp.RTT)
fmt.Println(";; SERVER:", opts.Request.Server)
fmt.Println(";; WHEN:", time.Now().Format(time.RFC1123Z))
fmt.Println(";; MSG SIZE rcvd:", resp.DNS.Len())
} else {
// Print just the responses, nothing else
for _, res := range resp.DNS.Answer {
temp := strings.Split(res.String(), "\t")
fmt.Println(temp[len(temp)-1])
}
}
}
}