awl/main.go
Sam 4cf19ebf78
All checks were successful
continuous-integration/drone/push Build is passing
Another "minor refactor" (#61)
I need to make fewer of these :)
Reviewed-on: #61
2022-08-11 07:25:36 +00:00

66 lines
1.2 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"errors"
"fmt"
"os"
"strings"
"git.froth.zone/sam/awl/cli"
"git.froth.zone/sam/awl/query"
"git.froth.zone/sam/awl/util"
)
var version = "DEV"
func main() {
if opts, code, err := run(); err != nil {
// TODO: Make not ew
if errors.Is(err, cli.ErrNotError) || strings.Contains(err.Error(), "help requested") {
os.Exit(0)
} else {
opts.Logger.Error(err)
os.Exit(code)
}
}
}
func run() (opts util.Options, code int, err error) {
opts, err = cli.ParseCLI(version)
if err != nil {
return opts, 1, fmt.Errorf("parse: %w", err)
}
var resp util.Response
// Retry queries if a query fails
for i := 0; i < opts.Request.Retries; i++ {
resp, err = query.CreateQuery(opts)
if err == nil {
break
} else {
opts.Logger.Warn("Retrying request, error:", err)
}
}
// Query failed, make it fail
if err != nil {
return opts, 9, fmt.Errorf("query: %w", err)
}
var str string
if opts.JSON || opts.XML || opts.YAML {
str, err = query.PrintSpecial(resp.DNS, opts)
if err != nil {
return opts, 10, fmt.Errorf("format print: %w", err)
}
} else {
str = query.ToString(resp, opts)
}
fmt.Println(str)
return opts, 0, nil
}