Add config test and update from env
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-19 00:38:25 +01:00
parent d8817cc67f
commit 41e82fb21e
4 changed files with 115 additions and 0 deletions

View File

@ -3,6 +3,8 @@ package gpaste
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"strings"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
) )
@ -25,5 +27,29 @@ func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
return nil, fmt.Errorf("error decoding server config: %w", err) return nil, fmt.Errorf("error decoding server config: %w", err)
} }
if c.Store == nil {
c.Store = &ServerStoreConfig{}
}
c.updateFromEnv()
return &c, nil return &c, nil
} }
func (sc *ServerConfig) updateFromEnv() {
if value, ok := os.LookupEnv("GPASTE_LOGLEVEL"); ok {
sc.LogLevel = strings.ToUpper(value)
}
if value, ok := os.LookupEnv("GPASTE_URL"); ok {
sc.URL = value
}
if value, ok := os.LookupEnv("GPASTE_LISTENADDR"); ok {
sc.ListenAddr = value
}
if value, ok := os.LookupEnv("GPASTE_STORE_TYPE"); ok {
sc.Store.Type = value
}
}

84
config_test.go Normal file
View File

@ -0,0 +1,84 @@
package gpaste_test
import (
"os"
"strings"
"testing"
"git.t-juice.club/torjus/gpaste"
"github.com/google/go-cmp/cmp"
)
func TestServerConfig(t *testing.T) {
t.Run("FromReader", func(t *testing.T) {
clearEnv()
simpleConfig := `
LogLevel = "INFO"
URL = "http://paste.example.org"
ListenAddr = ":8080"
[Store]
Type = "memory"
`
expected := &gpaste.ServerConfig{
LogLevel: "INFO",
URL: "http://paste.example.org",
ListenAddr: ":8080",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
},
}
sr := strings.NewReader(simpleConfig)
c, err := gpaste.ServerConfigFromReader(sr)
if err != nil {
t.Fatalf("Error parsing config: %s", err)
}
if !cmp.Equal(c, expected) {
t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
}
})
t.Run("FromEnv", func(t *testing.T) {
clearEnv()
var envMap map[string]string = map[string]string{
"GPASTE_LOGLEVEL": "DEBUG",
"GPASTE_URL": "http://gpaste.example.org",
"GPASTE_STORE_TYPE": "memory",
"GPASTE_LISTENADDR": ":8000",
}
expected := &gpaste.ServerConfig{
LogLevel: "DEBUG",
URL: "http://gpaste.example.org",
ListenAddr: ":8000",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
},
}
for k, v := range envMap {
os.Setenv(k, v)
}
sr := strings.NewReader("")
c, err := gpaste.ServerConfigFromReader(sr)
if err != nil {
t.Fatalf("Error parsing empty config")
}
if !cmp.Equal(c, expected) {
t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
}
})
}
func clearEnv() {
for _, env := range os.Environ() {
result := strings.Split(env, "=")
value := result[0]
if strings.Contains(value, "GPASTE_") {
os.Unsetenv(value)
}
}
}

1
go.mod
View File

@ -7,6 +7,7 @@ require github.com/google/uuid v1.3.0
require github.com/go-chi/chi/v5 v5.0.7 require github.com/go-chi/chi/v5 v5.0.7
require ( require (
github.com/google/go-cmp v0.5.6
github.com/pelletier/go-toml v1.9.4 github.com/pelletier/go-toml v1.9.4
github.com/urfave/cli/v2 v2.3.0 github.com/urfave/cli/v2 v2.3.0
go.uber.org/zap v1.20.0 go.uber.org/zap v1.20.0

4
go.sum
View File

@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@ -65,6 +67,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=