doggo/cmd/hub.go

74 lines
1.9 KiB
Go
Raw Normal View History

2020-12-09 18:11:09 +01:00
package main
import (
"time"
2020-12-10 17:14:04 +01:00
"github.com/miekg/dns"
2020-12-11 12:18:54 +01:00
"github.com/mr-karan/doggo/pkg/resolvers"
2020-12-09 18:11:09 +01:00
"github.com/sirupsen/logrus"
2020-12-17 12:27:44 +01:00
"github.com/spf13/pflag"
2020-12-09 18:11:09 +01:00
)
// Hub represents the structure for all app wide functions and structs.
type Hub struct {
2020-12-17 12:27:44 +01:00
Logger *logrus.Logger
Version string
QueryFlags QueryFlags
UnparsedArgs []string
Questions []dns.Question
Resolver []resolvers.Resolver
Nameservers []Nameserver
flag *pflag.FlagSet
2020-12-10 17:14:04 +01:00
}
// QueryFlags is used store the value of CLI flags.
type QueryFlags struct {
QNames []string `koanf:"query"`
QTypes []string `koanf:"type"`
QClasses []string `koanf:"class"`
Nameservers []string `koanf:"nameserver"`
UseIPv4 bool `koanf:"ipv4"`
UseIPv6 bool `koanf:"ipv6"`
DisplayTimeTaken bool `koanf:"time"`
ShowJSON bool `koanf:"json"`
UseSearchList bool `koanf:"search"`
Ndots int `koanf:"ndots"`
Color bool `koanf:"color"`
Timeout time.Duration `koanf:"timeout"`
2020-12-17 12:27:44 +01:00
isNdotsSet bool
2020-12-09 18:11:09 +01:00
}
2020-12-16 14:08:34 +01:00
// Nameserver represents the type of Nameserver
// along with it's address.
type Nameserver struct {
Address string
Type string
}
2020-12-09 18:11:09 +01:00
// NewHub initializes an instance of Hub which holds app wide configuration.
func NewHub(logger *logrus.Logger, buildVersion string) *Hub {
2020-12-10 17:14:04 +01:00
// Initialise Resolver
2020-12-09 18:11:09 +01:00
hub := &Hub{
Logger: logger,
Version: buildVersion,
2020-12-10 17:14:04 +01:00
QueryFlags: QueryFlags{
2020-12-13 13:19:10 +01:00
QNames: []string{},
QTypes: []string{},
QClasses: []string{},
Nameservers: []string{},
2020-12-10 17:14:04 +01:00
},
2020-12-16 14:08:34 +01:00
Nameservers: []Nameserver{},
2020-12-09 18:11:09 +01:00
}
return hub
}
2020-12-13 04:17:07 +01:00
// initLogger initializes logger
2020-12-13 08:15:45 +01:00
func initLogger() *logrus.Logger {
2020-12-13 04:17:07 +01:00
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
2020-12-13 04:17:07 +01:00
DisableLevelTruncation: true,
})
return logger
}