doggo/cmd/cli.go

124 lines
3.2 KiB
Go
Raw Normal View History

2020-12-09 18:11:09 +01:00
package main
import (
"os"
2020-12-13 13:19:10 +01:00
"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/posflag"
2020-12-13 16:33:44 +01:00
"github.com/sirupsen/logrus"
2020-12-13 13:19:10 +01:00
flag "github.com/spf13/pflag"
2020-12-09 18:11:09 +01:00
)
var (
// Version and date of the build. This is injected at build-time.
2020-12-13 08:15:45 +01:00
buildVersion = "unknown"
buildDate = "unknown"
2020-12-09 18:11:09 +01:00
)
func main() {
2020-12-10 10:39:05 +01:00
var (
2020-12-13 08:15:45 +01:00
logger = initLogger()
2020-12-17 12:27:44 +01:00
k = koanf.New(".")
2020-12-10 10:39:05 +01:00
)
2020-12-13 13:19:10 +01:00
2020-12-10 10:39:05 +01:00
// Initialize hub.
hub := NewHub(logger, buildVersion)
2020-12-10 17:14:04 +01:00
2020-12-17 12:27:44 +01:00
// Configure Flags.
2020-12-13 13:19:10 +01:00
f := flag.NewFlagSet("config", flag.ContinueOnError)
2020-12-17 12:27:44 +01:00
hub.flag = f
// Custom Help Text.
2020-12-13 16:33:44 +01:00
f.Usage = renderCustomHelp
2020-12-17 12:27:44 +01:00
// Query Options.
2020-12-13 13:19:10 +01:00
f.StringSliceP("query", "q", []string{}, "Domain name to query")
f.StringSliceP("type", "t", []string{}, "Type of DNS record to be queried (A, AAAA, MX etc)")
f.StringSliceP("class", "c", []string{}, "Network class of the DNS record to be queried (IN, CH, HS etc)")
2020-12-13 17:00:54 +01:00
f.StringSliceP("nameserver", "n", []string{}, "Address of the nameserver to send packets to")
2020-12-13 13:19:10 +01:00
// Resolver Options
f.Int("timeout", 5, "Sets the timeout for a query to T seconds. The default timeout is 5 seconds.")
2020-12-17 12:27:44 +01:00
f.Bool("search", true, "Use the search list provided in resolv.conf. It sets the `ndots` parameter as well unless overriden by `ndots` flag.")
2020-12-18 07:40:40 +01:00
f.Int("ndots", 0, "Specify the ndots paramter. Default value is taken from resolv.conf and fallbacks to 1 if ndots statement is missing in resolv.conf")
2020-12-16 15:10:01 +01:00
f.BoolP("ipv4", "4", false, "Use IPv4 only")
f.BoolP("ipv6", "6", false, "Use IPv6 only")
2020-12-13 13:19:10 +01:00
// Output Options
f.BoolP("json", "J", false, "Set the output format as JSON")
f.Bool("time", false, "Display how long it took for the response to arrive")
2020-12-13 16:33:44 +01:00
f.Bool("color", true, "Show colored output")
2020-12-13 13:19:10 +01:00
f.Bool("debug", false, "Enable debug mode")
2020-12-17 17:38:23 +01:00
// Parse and Load Flags.
2020-12-17 12:27:44 +01:00
err := f.Parse(os.Args[1:])
if err != nil {
hub.Logger.WithError(err).Error("error parsing flags")
hub.Logger.Exit(2)
}
if err = k.Load(posflag.Provider(f, ".", k), nil); err != nil {
hub.Logger.WithError(err).Error("error loading flags")
2020-12-13 16:33:44 +01:00
f.Usage()
hub.Logger.Exit(2)
}
2020-12-13 17:00:54 +01:00
2020-12-17 12:27:44 +01:00
// Set log level.
2020-12-13 16:33:44 +01:00
if k.Bool("debug") {
// Set logger level
hub.Logger.SetLevel(logrus.DebugLevel)
} else {
hub.Logger.SetLevel(logrus.InfoLevel)
2020-12-09 18:11:09 +01:00
}
2020-12-13 17:16:27 +01:00
2020-12-17 12:27:44 +01:00
// Unmarshall flags to the hub.
err = k.Unmarshal("", &hub.QueryFlags)
if err != nil {
hub.Logger.WithError(err).Error("error loading args")
hub.Logger.Exit(2)
}
// Load all `non-flag` arguments
// which will be parsed separately.
hub.UnparsedArgs = f.Args()
2020-12-13 13:19:10 +01:00
2020-12-17 17:38:23 +01:00
// Parse Query Args.
2020-12-17 12:27:44 +01:00
err = hub.loadQueryArgs()
if err != nil {
hub.Logger.WithError(err).Error("error parsing flags/arguments")
hub.Logger.Exit(2)
}
2020-12-16 14:08:34 +01:00
2020-12-17 17:38:23 +01:00
// Load Nameservers.
2020-12-17 12:27:44 +01:00
err = hub.loadNameservers()
if err != nil {
hub.Logger.WithError(err).Error("error loading nameservers")
hub.Logger.Exit(2)
2020-12-16 14:08:34 +01:00
}
2020-12-17 17:38:23 +01:00
// Load Resolvers.
2020-12-17 12:27:44 +01:00
err = hub.loadResolvers()
2020-12-16 14:08:34 +01:00
if err != nil {
hub.Logger.WithError(err).Error("error loading resolver")
hub.Logger.Exit(2)
}
2020-12-17 12:27:44 +01:00
// Run the app.
hub.Logger.Debug("Starting doggo 🐶")
2020-12-13 13:19:10 +01:00
if len(hub.QueryFlags.QNames) == 0 {
2020-12-13 16:33:44 +01:00
f.Usage()
hub.Logger.Exit(0)
}
2020-12-17 12:27:44 +01:00
// Resolve Queries.
2020-12-17 17:38:23 +01:00
responses, err := hub.Lookup()
if err != nil {
hub.Logger.WithError(err).Error("error looking up DNS records")
hub.Logger.Exit(2)
2020-12-09 18:11:09 +01:00
}
2020-12-17 17:38:23 +01:00
//Send the output.
hub.Output(responses)
// Quitting.
hub.Logger.Exit(0)
2020-12-09 18:11:09 +01:00
}