Skip to content

Commit 6fdfe46

Browse files
committed
Add CLI overrides and log config option
Fixes #3
1 parent 2480592 commit 6fdfe46

File tree

6 files changed

+103
-37
lines changed

6 files changed

+103
-37
lines changed

config.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const defaultCertPath = "/etc/bashrpc/"
1313
type config struct {
1414
Cert string `yaml:"cert"`
1515
Key string `yaml:"key"`
16+
Log string `yaml:"log"`
1617
Port string `yaml:"port"`
1718
Routes []route `yaml:"routes"`
1819
Secret string `yaml:"secret"`
@@ -39,12 +40,13 @@ func loadConfig(p string) (config, error) {
3940

4041
func setConfigDefaults(cfg *config) {
4142
defaultPKIPath := "/etc/bashrpc/pki"
42-
cfg.Key = defaultPKIPath + "/bashrpc.key"
4343
cfg.Cert = defaultPKIPath + "/bashrpc.cert"
44+
cfg.Key = defaultPKIPath + "/bashrpc.key"
45+
cfg.Log = "bashrpc.log"
4446
cfg.Port = "8675"
4547
}
4648

47-
func validateConfig(cfg config) error {
49+
func validateConfig(cfg *config) (err error) {
4850
var issues []string
4951

5052
if cfg.Port == "" {
@@ -63,13 +65,17 @@ func validateConfig(cfg config) error {
6365
issues = append(issues, "cert is missing")
6466
}
6567

68+
if cfg.Log == "" {
69+
issues = append(issues, "log is missing")
70+
}
71+
6672
if len(cfg.Whitelisted) == 0 {
6773
issues = append(issues, "no whitelisted clients are specified")
6874
}
6975

7076
if len(issues) > 0 {
71-
return errors.New("config validation errors: " + strings.Join(issues, ", "))
77+
err = errors.New("config validation errors: " + strings.Join(issues, ", "))
7278
}
7379

74-
return nil
80+
return
7581
}

config_test.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestSetConfigDefaults(t *testing.T) {
2424
Assert(cfg.Port, "8675", t)
2525
Assert(cfg.Key, "/etc/bashrpc/pki/bashrpc.key", t)
2626
Assert(cfg.Cert, "/etc/bashrpc/pki/bashrpc.cert", t)
27+
Assert(cfg.Log, "bashrpc.log", t)
2728
}
2829

2930
var validConfig = config{
@@ -32,12 +33,13 @@ var validConfig = config{
3233
Whitelisted: []string{"127.0.0.1"},
3334
Key: "/path/to/key",
3435
Cert: "/path/to/cert",
36+
Log: "/path/to/log",
3537
}
3638

3739
func TestValidConfig(t *testing.T) {
3840
When("config is valid")
3941
Then("there should be NO errors")
40-
if err := validateConfig(validConfig); err != nil {
42+
if err := validateConfig(&validConfig); err != nil {
4143
t.Errorf("expected NO errors but received %v", err)
4244
}
4345
}
@@ -48,7 +50,7 @@ func TestConfigMissingPort(t *testing.T) {
4850
cfg.Port = ""
4951

5052
Then("an error is returned")
51-
if err := validateConfig(cfg); err == nil {
53+
if err := validateConfig(&cfg); err == nil {
5254
t.Error("expected errors but received none")
5355
}
5456
}
@@ -59,7 +61,7 @@ func TestConfigMissingSecret(t *testing.T) {
5961
cfg.Secret = ""
6062

6163
Then("an error is returned")
62-
if err := validateConfig(cfg); err == nil {
64+
if err := validateConfig(&cfg); err == nil {
6365
t.Error("expected errors but received none")
6466
}
6567
}
@@ -70,7 +72,7 @@ func TestConfigMissingWhitelisted(t *testing.T) {
7072
cfg.Whitelisted = []string{}
7173

7274
Then("an error is returned")
73-
if err := validateConfig(cfg); err == nil {
75+
if err := validateConfig(&cfg); err == nil {
7476
t.Error("expected errors but received none")
7577
}
7678
}
@@ -81,7 +83,7 @@ func TestConfigMissingKey(t *testing.T) {
8183
cfg.Key = ""
8284

8385
Then("an error is returned")
84-
if err := validateConfig(cfg); err == nil {
86+
if err := validateConfig(&cfg); err == nil {
8587
t.Error("expected errors but received none")
8688
}
8789
}
@@ -92,7 +94,18 @@ func TestConfigMissingCert(t *testing.T) {
9294
cfg.Cert = ""
9395

9496
Then("an error is returned")
95-
if err := validateConfig(cfg); err == nil {
97+
if err := validateConfig(&cfg); err == nil {
98+
t.Error("expected errors but received none")
99+
}
100+
}
101+
102+
func TestConfigMissingLog(t *testing.T) {
103+
When("log file is not specified")
104+
cfg := validConfig
105+
cfg.Log = ""
106+
107+
Then("an error is returned")
108+
if err := validateConfig(&cfg); err == nil {
96109
t.Error("expected errors but received none")
97110
}
98111
}

logging.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package main
33
import (
44
"log"
55
"net/http"
6+
"os"
67
)
78

9+
func initLog(logPath string) (*os.File, error) {
10+
return os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
11+
}
12+
813
type loggingResponseWriter struct {
914
http.ResponseWriter
1015
statusCode int

main.go

+43-14
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,73 @@ import (
88
"os"
99
)
1010

11-
var (
11+
type options struct {
1212
configPath string
1313
logPath string
14-
)
14+
port string
15+
}
16+
17+
var opts options
1518

1619
func init() {
1720
c := flag.String("c", "", "specify bashRPC config file")
21+
p := flag.String("p", "", "specify bashRPC port")
1822
l := flag.String("log", "bashrpc.log", "specify log file")
1923
flag.Parse()
2024

21-
configPath = *c
22-
logPath = *l
25+
opts = options{
26+
configPath: *c,
27+
logPath: *l,
28+
port: *p,
29+
}
2330
}
2431

2532
func main() {
26-
if configPath == "" {
33+
if opts.configPath == "" {
2734
fmt.Println("config file argument is required")
2835
flag.Usage()
2936
os.Exit(1)
3037
}
3138

32-
logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
39+
var (
40+
logFile *os.File
41+
cfg config
42+
err error
43+
rtr router
44+
)
45+
46+
if cfg, err = loadConfig(opts.configPath); err != nil {
47+
panic(err)
48+
}
49+
50+
overrideConfigWithOptions(&cfg, opts)
3351

34-
if err != nil {
52+
if err = validateConfig(&cfg); err != nil {
53+
panic(err)
54+
}
55+
56+
if logFile, err = initLog(opts.logPath); err != nil {
3557
panic(err)
3658
}
3759

3860
defer logFile.Close()
3961
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
62+
log.Println("logging to", opts.logPath)
4063

41-
rtr, err := newRouter(configPath)
42-
if err != nil {
43-
log.Fatal(fmt.Sprintf("%v", err))
44-
os.Exit(1)
64+
if rtr, err = newRouter(cfg); err != nil {
65+
panic(err)
4566
}
4667

47-
if err := rtr.listen(); err != nil {
48-
log.Fatal(fmt.Sprintf("%v", err))
49-
os.Exit(1)
68+
if err = rtr.listen(); err != nil {
69+
panic(err)
70+
}
71+
}
72+
73+
func overrideConfigWithOptions(cfg *config, opts options) {
74+
if opts.logPath != "" {
75+
cfg.Log = opts.logPath
76+
}
77+
if opts.port != "" {
78+
cfg.Port = opts.port
5079
}
5180
}

main_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/binarymason/bashRPC/internal/testhelpers"
7+
)
8+
9+
func TestCLIOverrides(t *testing.T) {
10+
cfg := config{
11+
Log: "/path/to/log",
12+
Port: "8443",
13+
}
14+
15+
opts := options{
16+
logPath: "/path/to/another/log",
17+
port: "3210",
18+
}
19+
20+
overrideConfigWithOptions(&cfg, opts)
21+
22+
Assert(cfg.Log, "/path/to/another/log", t)
23+
Assert(cfg.Port, "3210", t)
24+
}

router.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,9 @@ type router struct {
1212
config config
1313
}
1414

15-
func newRouter(p string) (router, error) {
16-
rtr := router{}
17-
cfg, err := loadConfig(p)
18-
19-
if err != nil {
20-
return rtr, err
21-
}
22-
23-
if err := validateConfig(cfg); err != nil {
24-
return rtr, err
25-
}
26-
15+
// TODO: combine config struct with router
16+
func newRouter(cfg config) (rtr router, err error) {
2717
rtr.config = cfg
28-
2918
return rtr, nil
3019
}
3120

0 commit comments

Comments
 (0)