doggo/cmd/cli.go

120 lines
2.8 KiB
Go
Raw Normal View History

2020-12-09 18:11:09 +01:00
package main
import (
"os"
2020-12-10 10:39:05 +01:00
"github.com/urfave/cli/v2"
2020-12-09 18:11:09 +01:00
)
var (
// Version and date of the build. This is injected at build-time.
2020-12-10 17:14:04 +01:00
buildVersion = "unknown"
buildDate = "unknown"
verboseEnabled = false
2020-12-09 18:11:09 +01:00
)
func main() {
2020-12-10 10:39:05 +01:00
var (
2020-12-10 17:14:04 +01:00
logger = initLogger(verboseEnabled)
app = cli.NewApp()
2020-12-10 10:39:05 +01:00
)
// Initialize hub.
hub := NewHub(logger, buildVersion)
2020-12-10 17:14:04 +01:00
// Configure CLI app.
app.Name = "doggo"
app.Usage = "Command-line DNS Client"
app.Version = buildVersion
// Register command line flags.
2020-12-09 18:11:09 +01:00
app.Flags = []cli.Flag{
2020-12-10 10:39:05 +01:00
&cli.StringSliceFlag{
Name: "query",
Usage: "Domain name to query",
2020-12-12 07:46:54 +01:00
Destination: hub.QueryFlags.QNames,
2020-12-10 17:14:04 +01:00
},
&cli.StringSliceFlag{
Name: "type",
Usage: "Type of DNS record to be queried (A, AAAA, MX etc)",
2020-12-12 07:46:54 +01:00
Destination: hub.QueryFlags.QTypes,
2020-12-10 17:14:04 +01:00
},
&cli.StringSliceFlag{
Name: "nameserver",
Usage: "Address of the nameserver to send packets to",
2020-12-12 07:46:54 +01:00
Destination: hub.QueryFlags.Nameservers,
2020-12-10 17:14:04 +01:00
},
&cli.StringSliceFlag{
Name: "class",
Usage: "Network class of the DNS record to be queried (IN, CH, HS etc)",
2020-12-12 07:46:54 +01:00
Destination: hub.QueryFlags.QClasses,
2020-12-10 10:39:05 +01:00
},
2020-12-11 12:18:54 +01:00
&cli.BoolFlag{
2020-12-12 08:46:45 +01:00
Name: "udp",
Usage: "Use the DNS protocol over UDP",
Aliases: []string{"U"},
2020-12-12 07:46:54 +01:00
},
&cli.BoolFlag{
2020-12-12 08:46:45 +01:00
Name: "tcp",
Usage: "Use the DNS protocol over TCP",
Aliases: []string{"T"},
Destination: &hub.QueryFlags.UseTCP,
2020-12-12 07:46:54 +01:00
},
&cli.BoolFlag{
Name: "https",
Usage: "Use the DNS-over-HTTPS protocol",
2020-12-12 08:46:45 +01:00
Aliases: []string{"H"},
2020-12-12 07:46:54 +01:00
Destination: &hub.QueryFlags.IsDOH,
},
2020-12-12 08:46:45 +01:00
&cli.BoolFlag{
Name: "tls",
Usage: "Use the DNS-over-TLS",
Aliases: []string{"S"},
Destination: &hub.QueryFlags.IsDOT,
},
2020-12-12 07:46:54 +01:00
&cli.BoolFlag{
Name: "ipv6",
Aliases: []string{"6"},
Usage: "Use IPv6 only",
Destination: &hub.QueryFlags.UseIPv6,
},
&cli.BoolFlag{
Name: "ipv4",
Aliases: []string{"4"},
Usage: "Use IPv4 only",
Destination: &hub.QueryFlags.UseIPv4,
2020-12-11 12:18:54 +01:00
},
2020-12-13 04:17:07 +01:00
&cli.BoolFlag{
Name: "time",
Usage: "Display how long it took for the response to arrive",
Destination: &hub.QueryFlags.DisplayTimeTaken,
},
2020-12-13 05:04:53 +01:00
&cli.BoolFlag{
Name: "json",
Aliases: []string{"J"},
Usage: "Set the output format as JSON",
Destination: &hub.QueryFlags.ShowJSON,
},
2020-12-10 10:39:05 +01:00
&cli.BoolFlag{
2020-12-10 17:14:04 +01:00
Name: "verbose",
Usage: "Enable verbose logging",
Destination: &verboseEnabled,
DefaultText: "false",
2020-12-09 18:11:09 +01:00
},
}
2020-12-10 10:39:05 +01:00
2020-12-10 17:14:04 +01:00
app.Before = hub.loadQueryArgs
app.Action = func(c *cli.Context) error {
if len(hub.QueryFlags.QNames.Value()) == 0 {
cli.ShowAppHelpAndExit(c, 0)
2020-12-09 18:11:09 +01:00
}
2020-12-10 17:14:04 +01:00
hub.Lookup(c)
2020-12-09 18:11:09 +01:00
return nil
}
// Run the app.
2020-12-10 17:14:04 +01:00
hub.Logger.Debug("Starting doggo...")
2020-12-09 18:11:09 +01:00
err := app.Run(os.Args)
if err != nil {
2020-12-10 17:14:04 +01:00
logger.Errorf("oops! we encountered an issue: %s", err)
2020-12-09 18:11:09 +01:00
}
}