blocky/cmd/root.go

115 lines
2.5 KiB
Go
Raw Normal View History

2020-04-08 23:03:07 +02:00
package cmd
import (
"fmt"
2022-11-03 23:47:31 +01:00
"net"
2020-04-08 23:03:07 +02:00
"os"
2022-11-03 23:47:31 +01:00
"strconv"
"strings"
2020-04-08 23:03:07 +02:00
2021-08-25 22:06:34 +02:00
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/util"
2020-04-08 23:03:07 +02:00
"github.com/spf13/cobra"
)
//nolint:gochecknoglobals
var (
configPath string
apiHost string
apiPort uint16
)
const (
2022-09-08 22:34:08 +02:00
defaultPort = 4000
defaultHost = "localhost"
defaultConfigPath = "./config.yml"
configFileEnvVar = "BLOCKY_CONFIG_FILE"
configFileEnvVarOld = "CONFIG_FILE"
)
// NewRootCommand creates a new root cli command instance
2021-02-08 21:57:59 +01:00
func NewRootCommand() *cobra.Command {
c := &cobra.Command{
Use: "blocky",
Short: "blocky is a DNS proxy ",
Long: `A fast and configurable DNS Proxy
2020-04-08 23:03:07 +02:00
and ad-blocker for local network.
2020-04-08 23:03:07 +02:00
Complete documentation is available at https://github.com/0xERR0R/blocky`,
RunE: func(cmd *cobra.Command, args []string) error {
return newServeCommand().RunE(cmd, args)
2021-02-08 21:57:59 +01:00
},
SilenceUsage: true,
2021-02-08 21:57:59 +01:00
}
c.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "path to config file or folder")
c.PersistentFlags().StringVar(&apiHost, "apiHost", defaultHost, "host of blocky (API). Default overridden by config and CLI.") //nolint:lll
c.PersistentFlags().Uint16Var(&apiPort, "apiPort", defaultPort, "port of blocky (API). Default overridden by config and CLI.") //nolint:lll
2021-02-08 21:57:59 +01:00
c.AddCommand(newRefreshCommand(),
NewQueryCommand(),
NewVersionCommand(),
newServeCommand(),
newBlockingCommand(),
2022-09-08 22:34:08 +02:00
NewListsCommand(),
NewHealthcheckCommand())
2021-02-08 21:57:59 +01:00
return c
2020-04-08 23:03:07 +02:00
}
func apiURL() string {
return fmt.Sprintf("http://%s%s", net.JoinHostPort(apiHost, strconv.Itoa(int(apiPort))), "/api")
2020-04-08 23:03:07 +02:00
}
//nolint:gochecknoinits
func init() {
cobra.OnInitialize(initConfig)
}
func initConfig() {
2022-09-08 22:34:08 +02:00
if configPath == defaultConfigPath {
val, present := os.LookupEnv(configFileEnvVar)
if present {
configPath = val
} else {
val, present = os.LookupEnv(configFileEnvVarOld)
if present {
configPath = val
}
}
}
cfg, err := config.LoadConfig(configPath, false)
2022-04-01 08:58:09 +02:00
if err != nil {
util.FatalOnError("unable to load configuration: ", err)
}
log.ConfigureLogger(&cfg.Log)
2020-04-08 23:03:07 +02:00
if len(cfg.Ports.HTTP) != 0 {
split := strings.Split(cfg.Ports.HTTP[0], ":")
lastIdx := len(split) - 1
apiHost = strings.Join(split[:lastIdx], ":")
port, err := config.ConvertPort(split[lastIdx])
2021-07-16 23:13:32 +02:00
if err != nil {
util.FatalOnError("can't convert port to number (1 - 65535)", err)
2021-07-16 23:13:32 +02:00
return
}
2021-07-16 23:13:32 +02:00
apiPort = port
2020-04-08 23:03:07 +02:00
}
}
// Execute starts the command
2020-04-08 23:03:07 +02:00
func Execute() {
2021-02-08 21:57:59 +01:00
if err := NewRootCommand().Execute(); err != nil {
2020-04-08 23:03:07 +02:00
os.Exit(1)
}
}