Skip to content

Commit

Permalink
refactor: replace logrus with slog
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan committed Jul 1, 2024
1 parent 0500bfe commit d2ef389
Show file tree
Hide file tree
Showing 26 changed files with 157 additions and 584 deletions.
19 changes: 8 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
CLI_BIN := ./bin/doggo.bin
API_BIN := ./bin/doggo-api.bin
WEB_BIN := ./bin/doggo-web.bin

HASH := $(shell git rev-parse --short HEAD)
BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S')
VERSION := ${HASH}

.PHONY: build-cli
build-cli:
go build -o ${CLI_BIN} -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" ./cmd/doggo/
go build -o ${CLI_BIN} -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" ./cmd/

.PHONY: build-api
build-api:
go build -o ${API_BIN} -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" ./cmd/api/

.PHONY: build
build: build-api build-cli
.PHONY: build-web
build-web:
go build -o ${WEB_BIN} -ldflags="-X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}'" ./web/

.PHONY: run-cli
run-cli: build-cli ## Build and Execute the CLI binary after the build step.
${CLI_BIN}

.PHONY: run-api
run-api: build-api ## Build and Execute the API binary after the build step.
${API_BIN} --config config-api-sample.toml
.PHONY: run-web
run-web: build-web ## Build and Execute the API binary after the build step.
${WEB_BIN} --config config-api-sample.toml

.PHONY: clean
clean:
Expand Down
39 changes: 9 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ to experiment with writing a DNS Client from scratch in `Go` myself. Hence the n
- Available as a web tool as well: [https://doggo.mrkaran.dev](https://doggo.mrkaran.dev).
- Shell completions for `zsh` and `fish`.
- Reverse DNS Lookups.
- Supports multiple query flags.

## Installation

Expand Down Expand Up @@ -105,8 +104,8 @@ The binary will be available at `$GOPATH/bin/doggo`.
**Do a simple DNS Lookup for `mrkaran.dev`**

```bash
$ doggo mrkaran.dev
NAME TYPE CLASS TTL ADDRESS NAMESERVER
$ doggo mrkaran.dev
NAME TYPE CLASS TTL ADDRESS NAMESERVER
mrkaran.dev. A IN 20s 13.250.205.9 127.0.0.1:53
mrkaran.dev. A IN 20s 206.189.89.118 127.0.0.1:53
```
Expand All @@ -115,7 +114,7 @@ mrkaran.dev. A IN 20s 206.189.89.118 127.0.0.1:53

```
doggo MX github.com @9.9.9.9
NAME TYPE CLASS TTL ADDRESS NAMESERVER
NAME TYPE CLASS TTL ADDRESS NAMESERVER
github.com. MX IN 3600s 10 alt3.aspmx.l.google.com. 9.9.9.9:53
github.com. MX IN 3600s 5 alt1.aspmx.l.google.com. 9.9.9.9:53
github.com. MX IN 3600s 10 alt4.aspmx.l.google.com. 9.9.9.9:53
Expand All @@ -127,7 +126,7 @@ or using _named parameters_:

```bash
$ doggo -t MX -n 9.9.9.9 github.com
NAME TYPE CLASS TTL ADDRESS NAMESERVER
NAME TYPE CLASS TTL ADDRESS NAMESERVER
github.com. MX IN 3600s 10 alt3.aspmx.l.google.com. 9.9.9.9:53
github.com. MX IN 3600s 5 alt1.aspmx.l.google.com. 9.9.9.9:53
github.com. MX IN 3600s 10 alt4.aspmx.l.google.com. 9.9.9.9:53
Expand All @@ -138,8 +137,8 @@ github.com. MX IN 3600s 1 aspmx.l.google.com. 9.9.9.9:
**Query DNS records for `archive.org` using Cloudflare DoH resolver**

```bash
$ doggo archive.org @https://cloudflare-dns.com/dns-query
NAME TYPE CLASS TTL ADDRESS NAMESERVER
$ doggo archive.org @https://cloudflare-dns.com/dns-query
NAME TYPE CLASS TTL ADDRESS NAMESERVER
archive.org. A IN 41s 207.241.224.2 https://cloudflare-dns.com/dns-query
```

Expand Down Expand Up @@ -192,9 +191,9 @@ $ doggo internetfreedom.in --json | jq
**Query DNS records for `duckduckgo.com` and show RTT (Round Trip Time)**

```bash
$ doggo duckduckgo.com --time
NAME TYPE CLASS TTL ADDRESS NAMESERVER TIME TAKEN
duckduckgo.com. A IN 30s 40.81.94.43 127.0.0.1:53 45ms
$ doggo duckduckgo.com --time
NAME TYPE CLASS TTL ADDRESS NAMESERVER TIME TAKEN
duckduckgo.com. A IN 30s 40.81.94.43 127.0.0.1:53 45ms
```

## Command-line Arguments
Expand Down Expand Up @@ -249,26 +248,6 @@ URL scheme of the server is used to identify which resolver to use for lookups.
--short Short output format. Shows only the response section.
```

### Query Flags

```
--aa Set Authoritative Answer flag.
--ad Set Authenticated Data flag.
--cd Set Checking Disabled flag.
--rd Set Recursion Desired flag (default: true).
--z Set Z flag (reserved for future use).
--do Set DNSSEC OK flag.
```

These flags allow you to control various aspects of the DNS query:

- `--aa`: Request an authoritative answer from the server.
- `--ad`: Request authenticated data in the response.
- `--cd`: Disable DNSSEC validation.
- `--rd`: Enable recursive querying (enabled by default).
- `--z`: Set the Z bit, which is reserved for future use.
- `--do`: Set the DNSSEC OK bit to request DNSSEC records.

---

## Contributing
Expand Down
File renamed without changes.
56 changes: 21 additions & 35 deletions cmd/doggo/cli.go → cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package main
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"time"

"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/v2"
"github.com/mr-karan/doggo/internal/app"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/mr-karan/doggo/pkg/utils"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)

var (
buildVersion = "unknown"
buildDate = "unknown"
logger = utils.InitLogger()
k = koanf.New(".")
)

Expand All @@ -28,24 +27,24 @@ func main() {
return
}

app := app.New(logger, buildVersion)
f := setupFlags()

if err := parseAndLoadFlags(f); err != nil {
app.Logger.WithError(err).Error("Error parsing or loading flags")
app.Logger.Exit(2)
fmt.Println("Error parsing or loading flags", "error", err)
os.Exit(2)
}

if k.Bool("version") {
fmt.Printf("%s - %s\n", buildVersion, buildDate)
app.Logger.Exit(0)
os.Exit(0)
}

setupLogging(&app)
logger := utils.InitLogger(k.Bool("debug"))
app := app.New(logger, buildVersion)

if err := k.Unmarshal("", &app.QueryFlags); err != nil {
app.Logger.WithError(err).Error("Error loading args")
app.Logger.Exit(2)
app.Logger.Error("Error loading args", "error", err)
os.Exit(2)
}

loadNameservers(&app, f.Args())
Expand All @@ -58,8 +57,8 @@ func main() {
app.PrepareQuestions()

if err := app.LoadNameservers(); err != nil {
app.Logger.WithError(err).Error("Error loading nameservers")
app.Logger.Exit(2)
app.Logger.Error("Error loading nameservers", "error", err)
os.Exit(2)
}

rslvrs, err := resolvers.LoadResolvers(resolvers.Options{
Expand All @@ -75,15 +74,15 @@ func main() {
TLSHostname: app.QueryFlags.TLSHostname,
})
if err != nil {
app.Logger.WithError(err).Error("Error loading resolver")
app.Logger.Exit(2)
app.Logger.Error("Error loading resolver", "error", err)
os.Exit(2)
}
app.Resolvers = rslvrs

app.Logger.Debug("Starting doggo 🐶")
if len(app.QueryFlags.QNames) == 0 {
f.Usage()
app.Logger.Exit(0)
os.Exit(0)
}

queryFlags := resolvers.QueryFlags{
Expand All @@ -99,7 +98,7 @@ func main() {

outputResults(&app, responses, responseErrors)

app.Logger.Exit(0)
os.Exit(0)
}

func setupFlags() *flag.FlagSet {
Expand Down Expand Up @@ -150,20 +149,9 @@ func parseAndLoadFlags(f *flag.FlagSet) error {
return nil
}

func setupLogging(app *app.App) {
if k.Bool("debug") {
app.Logger.SetLevel(logrus.DebugLevel)
} else {
app.Logger.SetLevel(logrus.InfoLevel)
}
}

func loadNameservers(app *app.App, args []string) {
flagNameservers := k.Strings("nameserver")
app.Logger.WithField("flagNameservers", flagNameservers).Debug("Nameservers from -n flag")

unparsedNameservers, qt, qc, qn := loadUnparsedArgs(args)
app.Logger.WithField("unparsedNameservers", unparsedNameservers).Debug("Nameservers from unparsed arguments")

if len(flagNameservers) > 0 {
app.QueryFlags.Nameservers = flagNameservers
Expand All @@ -174,8 +162,6 @@ func loadNameservers(app *app.App, args []string) {
app.QueryFlags.QTypes = append(app.QueryFlags.QTypes, qt...)
app.QueryFlags.QClasses = append(app.QueryFlags.QClasses, qc...)
app.QueryFlags.QNames = append(app.QueryFlags.QNames, qn...)

app.Logger.WithField("finalNameservers", app.QueryFlags.Nameservers).Debug("Final nameservers")
}

func resolveQueries(app *app.App, flags resolvers.QueryFlags) ([]resolvers.Response, []error) {
Expand All @@ -197,17 +183,17 @@ func resolveQueries(app *app.App, flags resolvers.QueryFlags) ([]resolvers.Respo

func outputResults(app *app.App, responses []resolvers.Response, responseErrors []error) {
if app.QueryFlags.ShowJSON {
outputJSON(responses, responseErrors)
outputJSON(app.Logger, responses, responseErrors)
} else {
if len(responseErrors) > 0 {
app.Logger.WithError(responseErrors[0]).Error("Error looking up DNS records")
app.Logger.Exit(9)
app.Logger.Error("Error looking up DNS records", "error", responseErrors[0])
os.Exit(9)
}
app.Output(responses)
}
}

func outputJSON(responses []resolvers.Response, responseErrors []error) {
func outputJSON(logger *slog.Logger, responses []resolvers.Response, responseErrors []error) {
jsonOutput := struct {
Responses []resolvers.Response `json:"responses,omitempty"`
Error string `json:"error,omitempty"`
Expand All @@ -221,8 +207,8 @@ func outputJSON(responses []resolvers.Response, responseErrors []error) {

jsonData, err := json.MarshalIndent(jsonOutput, "", " ")
if err != nil {
logger.WithError(err).Error("Error marshaling JSON")
logger.Exit(1)
logger.Error("Error marshaling JSON")
os.Exit(1)
}
fmt.Println(string(jsonData))
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ require (
github.com/ameshkov/dnscrypt/v2 v2.3.0
github.com/ameshkov/dnsstamps v1.0.3
github.com/fatih/color v1.17.0
github.com/go-chi/chi v4.1.2+incompatible
github.com/knadh/koanf v1.5.0
github.com/go-chi/chi/v5 v5.1.0
github.com/knadh/koanf/parsers/toml v0.1.0
github.com/knadh/koanf/providers/env v0.1.0
github.com/knadh/koanf/providers/file v0.1.0
github.com/knadh/koanf/providers/posflag v0.1.0
github.com/knadh/koanf/v2 v2.1.1
github.com/miekg/dns v1.1.61
github.com/olekukonko/tablewriter v0.0.5
github.com/quic-go/quic-go v0.45.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
golang.org/x/sys v0.21.0
)
Expand All @@ -21,18 +24,17 @@ require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/onsi/ginkgo/v2 v2.19.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
go.uber.org/mock v0.4.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
Expand Down
Loading

0 comments on commit d2ef389

Please sign in to comment.