Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: fix invalid key in example cfg file and support log_level from cfg file #1330

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/nsqadmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ func (p *program) Start() error {
os.Exit(0)
}

var cfg map[string]interface{}
var cfg config
configFile := flagSet.Lookup("config").Value.String()
if configFile != "" {
_, err := toml.DecodeFile(configFile, &cfg)
if err != nil {
logFatal("failed to load config file %s - %s", configFile, err)
}
}
cfg.Validate()

options.Resolve(opts, flagSet, cfg)
nsqadmin, err := nsqadmin.New(opts)
Expand Down
26 changes: 26 additions & 0 deletions apps/nsqadmin/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"testing"

"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/lg"
"github.com/nsqio/nsq/internal/test"
"github.com/nsqio/nsq/nsqadmin"
)

func TestConfigFlagParsing(t *testing.T) {
opts := nsqadmin.NewOptions()
opts.Logger = test.NewTestLogger(t)

flagSet := nsqadminFlagSet(opts)
flagSet.Parse([]string{})

cfg := config{"log_level": "debug"}
cfg.Validate()

options.Resolve(opts, flagSet, cfg)
if opts.LogLevel != lg.DEBUG {
t.Fatalf("log level: want debug, got %s", opts.LogLevel.String())
}
}
22 changes: 22 additions & 0 deletions apps/nsqadmin/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/nsqio/nsq/internal/lg"
)

type config map[string]interface{}

// Validate settings in the config file, and fatal on errors
func (cfg config) Validate() {
if v, exists := cfg["log_level"]; exists {
var t lg.LogLevel
err := t.Set(fmt.Sprintf("%v", v))
if err == nil {
cfg["log_level"] = t
} else {
logFatal("failed parsing log_level %+v", v)
}
}
}
5 changes: 5 additions & 0 deletions apps/nsqd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/BurntSushi/toml"
"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/lg"
"github.com/nsqio/nsq/internal/test"
"github.com/nsqio/nsq/nsqd"
)
Expand All @@ -25,6 +26,7 @@ func TestConfigFlagParsing(t *testing.T) {
}
defer f.Close()
toml.DecodeReader(f, &cfg)
cfg["log_level"] = "debug"
cfg.Validate()

options.Resolve(opts, flagSet, cfg)
Expand All @@ -33,4 +35,7 @@ func TestConfigFlagParsing(t *testing.T) {
if opts.TLSMinVersion != tls.VersionTLS10 {
t.Errorf("min %#v not expected %#v", opts.TLSMinVersion, tls.VersionTLS10)
}
if opts.LogLevel != lg.DEBUG {
t.Fatalf("log level: want debug, got %s", opts.LogLevel.String())
}
}
10 changes: 10 additions & 0 deletions apps/nsqd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/nsqio/nsq/internal/app"
"github.com/nsqio/nsq/internal/lg"
"github.com/nsqio/nsq/nsqd"
)

Expand Down Expand Up @@ -91,6 +92,15 @@ func (cfg config) Validate() {
logFatal("failed parsing tls_min_version %+v", v)
}
}
if v, exists := cfg["log_level"]; exists {
var t lg.LogLevel
err := t.Set(fmt.Sprintf("%v", v))
if err == nil {
cfg["log_level"] = t
} else {
logFatal("failed parsing log_level %+v", v)
}
}
}

func nsqdFlagSet(opts *nsqd.Options) *flag.FlagSet {
Expand Down
3 changes: 2 additions & 1 deletion apps/nsqlookupd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ func (p *program) Start() error {
os.Exit(0)
}

var cfg map[string]interface{}
var cfg config
configFile := flagSet.Lookup("config").Value.String()
if configFile != "" {
_, err := toml.DecodeFile(configFile, &cfg)
if err != nil {
logFatal("failed to load config file %s - %s", configFile, err)
}
}
cfg.Validate()

options.Resolve(opts, flagSet, cfg)
nsqlookupd, err := nsqlookupd.New(opts)
Expand Down
26 changes: 26 additions & 0 deletions apps/nsqlookupd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"testing"

"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/lg"
"github.com/nsqio/nsq/internal/test"
"github.com/nsqio/nsq/nsqlookupd"
)

func TestConfigFlagParsing(t *testing.T) {
opts := nsqlookupd.NewOptions()
opts.Logger = test.NewTestLogger(t)

flagSet := nsqlookupdFlagSet(opts)
flagSet.Parse([]string{})

cfg := config{"log_level": "debug"}
cfg.Validate()

options.Resolve(opts, flagSet, cfg)
if opts.LogLevel != lg.DEBUG {
t.Fatalf("log level: want debug, got %s", opts.LogLevel.String())
}
}
22 changes: 22 additions & 0 deletions apps/nsqlookupd/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/nsqio/nsq/internal/lg"
)

type config map[string]interface{}

// Validate settings in the config file, and fatal on errors
func (cfg config) Validate() {
if v, exists := cfg["log_level"]; exists {
var t lg.LogLevel
err := t.Set(fmt.Sprintf("%v", v))
if err == nil {
cfg["log_level"] = t
} else {
logFatal("failed parsing log_level %+v", v)
}
}
}
2 changes: 1 addition & 1 deletion contrib/nsqadmin.cfg.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## log verbosity level: debug, info, warn, error, or fatal
log-level = "info"
log_level = "info"

## <addr>:<port> to listen on for HTTP clients
http_address = "0.0.0.0:4171"
Expand Down
2 changes: 1 addition & 1 deletion contrib/nsqd.cfg.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## log verbosity level: debug, info, warn, error, or fatal
log-level = "info"
log_level = "info"

## unique identifier (int) for this worker (will default to a hash of hostname)
# id = 5150
Expand Down
2 changes: 1 addition & 1 deletion contrib/nsqlookupd.cfg.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## log verbosity level: debug, info, warn, error, or fatal
log-level = "info"
log_level = "info"

## <addr>:<port> to listen on for TCP clients
tcp_address = "0.0.0.0:4160"
Expand Down