#147: Blocky commands do not run without local ./config.yml file

This commit is contained in:
Dimitri Herzog 2021-02-26 22:42:00 +01:00
parent 59c650ff6a
commit efb5ac37af
4 changed files with 28 additions and 8 deletions

View File

@ -35,7 +35,7 @@ Complete documentation is available at https://github.com/0xERR0R/blocky`,
c.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yml", "path to config file")
c.PersistentFlags().StringVar(&apiHost, "apiHost", "localhost", "host of blocky (API)")
c.PersistentFlags().Uint16Var(&apiPort, "apiPort", 0, "port of blocky (API)")
c.PersistentFlags().Uint16Var(&apiPort, "apiPort", 4000, "port of blocky (API)")
c.AddCommand(newRefreshCommand(),
NewQueryCommand(),
@ -57,10 +57,10 @@ func init() {
}
func initConfig() {
cfg = config.NewConfig(configPath)
cfg = config.NewConfig(configPath, false)
log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat)
if apiPort == 0 {
if cfg.HTTPPort != 0 {
apiPort = cfg.HTTPPort
}
}

View File

@ -36,6 +36,9 @@ func newServeCommand() *cobra.Command {
func startServer(_ *cobra.Command, _ []string) {
printBanner()
cfg = config.NewConfig(configPath, true)
log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat)
configureHTTPClient(&cfg)
signals := make(chan os.Signal)

View File

@ -1,9 +1,11 @@
package config
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"regexp"
"strconv"
"strings"
@ -242,13 +244,19 @@ type QueryLogConfig struct {
}
// NewConfig creates new config from YAML file
func NewConfig(path string) Config {
func NewConfig(path string, mandatory bool) Config {
cfg := Config{}
setDefaultValues(&cfg)
data, err := ioutil.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) && !mandatory {
// config file does not exist
// return config with default values
return cfg
}
log.Log().Fatal("Can't read config file: ", err)
}

View File

@ -18,7 +18,7 @@ var _ = Describe("Config", func() {
err := os.Chdir("../testdata")
Expect(err).Should(Succeed())
cfg := NewConfig("config.yml")
cfg := NewConfig("config.yml", true)
Expect(cfg.Port).Should(Equal("55555"))
Expect(cfg.Upstream.ExternalResolvers).Should(HaveLen(3))
@ -57,12 +57,12 @@ var _ = Describe("Config", func() {
Log().ExitFunc = func(int) { fatal = true }
_ = NewConfig("config.yml")
_ = NewConfig("config.yml", true)
Expect(fatal).Should(BeTrue())
})
})
When("config directory does not exist", func() {
It("should log with fatal and exit", func() {
It("should log with fatal and exit if config is mandatory", func() {
err := os.Chdir("../..")
Expect(err).Should(Succeed())
@ -71,10 +71,19 @@ var _ = Describe("Config", func() {
var fatal bool
Log().ExitFunc = func(int) { fatal = true }
_ = NewConfig("config.yml")
_ = NewConfig("config.yml", true)
Expect(fatal).Should(BeTrue())
})
It("should use default config if config is not mandatory", func() {
err := os.Chdir("../..")
Expect(err).Should(Succeed())
cfg := NewConfig("config.yml", false)
Expect(cfg.LogLevel).Should(Equal("info"))
})
})
})