Skip to content

Commit

Permalink
Feat: updated to use config via viper (#8)
Browse files Browse the repository at this point in the history
* feat: updated to use config via viper

* feat: Reconciled changes with main

* Feat: Add support for checking PR message format (#25)

- fixes #24

Signed-off-by: Anoop Gopalakrishnan <[email protected]>

* Create codeql.yml (#27)

* Feat: Add container scanning workflow

Signed-off-by: Anoop Gopalakrishnan <[email protected]>

* Fix: Run scorecard on every push to main

Signed-off-by: Anoop Gopalakrishnan <[email protected]>

* Fix: Use correct go version in codeql analysis

- Remove temp branch from build workflow

---------

Signed-off-by: Anoop Gopalakrishnan <[email protected]>
Co-authored-by: Finsen Varghese <[email protected]>
Co-authored-by: Anoop Gopalakrishnan <[email protected]>
Co-authored-by: Anoop Gopalakrishnan <[email protected]>
  • Loading branch information
4 people authored Jan 16, 2024
1 parent 1f09cfa commit 9d07bef
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 32 deletions.
44 changes: 28 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package config

import (
"bytes"
"embed"
"fmt"
"os"

"github.com/go-yaml/yaml"
"github.com/spf13/viper"
)

type config struct {
Expand All @@ -13,36 +15,45 @@ type config struct {
}

type dbConfig struct {
Driver string `yaml:"driver"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Database string `yaml:"database"`
DetailLog bool `yaml:"detail-log"`
MaxOpenConns int `yaml:"max-open-conns"`
MaxIdleConns int `yaml:"max-idle-conns"`
Driver string `mapstructure:"driver"`
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
DetailLog bool `mapstructure:"detail-log"`
MaxOpenConns int `mapstructure:"max-open-conns"`
MaxIdleConns int `mapstructure:"max-idle-conns"`
}

type serverConfig struct {
Port string `yaml:"port"`
Port string `mapstructure:"port"`
}

var configuration *config

//go:embed config.yaml
var configPath embed.FS

func LoadConfig() error {
func LoadConfig() (*config, error) {
v := viper.New()
v.SetConfigType("yaml")
data, err := configPath.ReadFile("config.yaml")
if err != nil {
return err
return nil, fmt.Errorf("error reading embedded config file:: %w", err)
}
err = yaml.Unmarshal(data, &configuration)
err = v.ReadConfig(bytes.NewReader(data))
if err != nil {
return err
return nil, fmt.Errorf("fatal error config file: %w", err)
}

err = v.Unmarshal(&configuration)
if err != nil {
return nil, err
}

fmt.Println("Successfully loaded config file - ", v.ConfigFileUsed())

if os.Getenv("FERN_USERNAME") != "" {
configuration.Db.Username = os.Getenv("FERN_USERNAME")
}
Expand All @@ -58,7 +69,8 @@ func LoadConfig() error {
if os.Getenv("FERN_DATABASE") != "" {
configuration.Db.Database = os.Getenv("FERN_DATABASE")
}
return err

return configuration, nil
}

func GetDb() *dbConfig {
Expand Down
13 changes: 13 additions & 0 deletions config/config_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
30 changes: 30 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config_test

import (
"github.com/guidewire/fern-reporter/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Config", func() {
Context("LoadConfig", func() {
It("should load the configuration from a file", func() {

appConfig, err := config.LoadConfig()
Expect(err).NotTo(HaveOccurred())

Expect(appConfig.Server.Port).To(Equal(":8080"))
Expect(appConfig.Db.Driver).To(Equal("postgres"))
Expect(appConfig.Db.Host).To(Equal("localhost"))
Expect(appConfig.Db.Port).To(Equal("5432"))
Expect(appConfig.Db.Database).To(Equal("fern"))
Expect(appConfig.Db.Username).To(Equal("fern"))
Expect(appConfig.Db.Password).To(Equal("fern"))
Expect(appConfig.Db.DetailLog).To(BeTrue())
Expect(appConfig.Db.MaxOpenConns).To(Equal(100))
Expect(appConfig.Db.MaxIdleConns).To(Equal(10))
})

})

})
28 changes: 23 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.21
require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/golang-migrate/migrate/v4 v4.16.2
github.com/markbates/pkger v0.15.1
github.com/onsi/ginkgo/v2 v2.13.2
github.com/onsi/gomega v1.30.0
github.com/spf13/viper v1.18.2
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
)
Expand All @@ -17,15 +18,21 @@ require (
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gobuffalo/here v0.6.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
Expand All @@ -35,21 +42,32 @@ require (
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9d07bef

Please sign in to comment.