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

feat: Switch to viper for configuration #70

Merged
merged 11 commits into from
Jul 14, 2023
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
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"path/filepath"

"github.com/rs/zerolog"
"github.com/underdog-tech/vulnbot/internal"
"github.com/underdog-tech/vulnbot/logger"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,7 +47,9 @@ func Execute() {
func init() {
persistent := rootCmd.PersistentFlags()
persistent.BoolP("disable-slack", "d", false, "Disable Slack alerts.")
persistent.StringP("config", "c", "config.toml", "Config file path.")

projectRootDir := internal.GetProjectRootDir()
persistent.StringP("config", "c", filepath.Join(projectRootDir, "config.toml"), "Config file path.")

persistent.BoolP("quiet", "q", false, "Suppress all console output. (Mutually exclusive with 'verbose'.)")
persistent.CountP("verbose", "v", "More verbose output. Specifying multiple times increases verbosity. (Mutually exclusive with 'quiet'.)")
Expand Down
81 changes: 73 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import (
"fmt"
"path/filepath"
"strings"

"github.com/underdog-tech/vulnbot/logger"

"github.com/BurntSushi/toml"
"github.com/spf13/viper"
)

type SeverityConfig struct {
Expand All @@ -24,23 +26,86 @@
Slack_channel string
}

type TomlConfig struct {
type Config struct {
Default_slack_channel string
Severity []SeverityConfig
Ecosystem []EcosystemConfig
Team []TeamConfig
}

func LoadConfig(configFilePath *string) TomlConfig {
var config TomlConfig
type Env struct {
GithubOrg string `mapstructure:"GITHUB_ORG"`
SlackAuthToken string `mapstructure:"SLACK_AUTH_TOKEN"`
GithubToken string `mapstructure:"GITHUB_TOKEN"`
}

var viperClient *viper.Viper

type ViperParams struct {
ConfigPath *string
Output interface{}
EnvFileName *string
}

func getViper() *viper.Viper {
if viperClient == nil {
viperClient = viper.New()
}
return viperClient
}

func LoadConfig(params ViperParams) error {
log := logger.Get()

_, err := toml.DecodeFile(*configFilePath, &config)
v := getViper()

filename := filepath.Base(*params.ConfigPath)
extension := filepath.Ext(*params.ConfigPath)
configDir := filepath.Dir(*params.ConfigPath)

v.SetConfigName(filename)
v.AddConfigPath(configDir)
v.SetConfigType(strings.TrimLeft(extension, "."))

err := v.ReadInConfig()
if err != nil {
log.Fatal().Err(err).Msg("Unable to read config.")
return err
}

Check warning on line 74 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L72-L74

Added lines #L72 - L74 were not covered by tests

err = v.Unmarshal(&params.Output)
if err != nil {
log.Fatal().Err(err).Msg("Error loading config file.")
log.Fatal().Err(err).Msg("Unable to unmarshal config.")
return err

Check warning on line 79 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L78-L79

Added lines #L78 - L79 were not covered by tests
}
log.Debug().Any("config", config).Msg("Config loaded.")
return config

log.Debug().Any("config", params.Output).Msg("Config loaded.")
return nil
}

func LoadEnv(params ViperParams) error {
log := logger.Get()

v := getViper()

// Read in environment variables that match
v.SetConfigFile(*params.EnvFileName)
v.AutomaticEnv()

err := v.ReadInConfig()
if err != nil {
log.Fatal().Err(err).Msg("Unable to read ENV file.")
return err
}

Check warning on line 99 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L97-L99

Added lines #L97 - L99 were not covered by tests

err = v.Unmarshal(&params.Output)
if err != nil {
log.Fatal().Err(err).Msg("Unable to unmarshal ENV.")
return err
}

Check warning on line 105 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L103-L105

Added lines #L103 - L105 were not covered by tests

log.Debug().Any("env", params.Output).Msg("ENV loaded.")
return nil
}

func GetIconForSeverity(severity string, severities []SeverityConfig) (string, error) {
Expand Down
49 changes: 49 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -54,3 +57,49 @@ func TestGetUnconfiguredTeamConfigBySlug(t *testing.T) {
assert.Empty(t, team)
assert.Error(t, err)
}

func getCurrentDir() (string, error) {
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Failed to get current working directory:", err)
return "", err
}

return cwd, nil
}

func TestLoadConfig(t *testing.T) {
expectedSlackChannel := "testing_slack_channel"
currentDir, err := getCurrentDir()
if err != nil {
assert.Error(t, err)
}
testDataPath := filepath.Join(currentDir, "/testdata/test_config.toml")

var config Config
LoadConfig(ViperParams{
Output: &config,
ConfigPath: &testDataPath,
})

assert.IsType(t, Config{}, config)
assert.Equal(t, expectedSlackChannel, config.Default_slack_channel)
}

func TestLoadEnv(t *testing.T) {
expectedSlackAuthToken := "testing"
currentDir, err := getCurrentDir()
if err != nil {
assert.Error(t, err)
}
testDataPath := filepath.Join(currentDir, "/testdata/config.env")

var env Env
LoadEnv(ViperParams{
EnvFileName: &testDataPath,
Output: &env,
})

assert.IsType(t, Env{}, env)
assert.Equal(t, expectedSlackAuthToken, env.SlackAuthToken)
}
1 change: 1 addition & 0 deletions config/testdata/config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SLACK_AUTH_TOKEN="testing"
5 changes: 5 additions & 0 deletions config/testdata/test_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default_slack_channel = "testing_slack_channel"

[[ecosystem]]
label = "Go"
slack_emoji = ":golang:"
35 changes: 33 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@ module github.com/underdog-tech/vulnbot
go 1.20

require (
github.com/BurntSushi/toml v1.2.1
github.com/gookit/color v1.5.3
github.com/joho/godotenv v1.5.1
github.com/rs/zerolog v1.29.1
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9
github.com/slack-go/slack v0.12.2
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.3
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/oauth2 v0.8.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand All @@ -41,4 +51,25 @@ require (
exclude (
golang.org/x/text v0.3.0
golang.org/x/text v0.3.2
golang.org/x/text v0.3.3
golang.org/x/text v0.3.4
golang.org/x/text v0.3.6
golang.org/x/text v0.3.7
google.golang.org/grpc v1.19.0
google.golang.org/grpc v1.20.1
google.golang.org/grpc v1.21.1
google.golang.org/grpc v1.23.0
google.golang.org/grpc v1.25.1
google.golang.org/grpc v1.26.0
google.golang.org/grpc v1.27.0
google.golang.org/grpc v1.27.1
google.golang.org/grpc v1.28.0
google.golang.org/grpc v1.29.1
google.golang.org/grpc v1.30.0
google.golang.org/grpc v1.31.0
google.golang.org/grpc v1.31.1
google.golang.org/grpc v1.33.2
google.golang.org/grpc v1.34.0
google.golang.org/grpc v1.35.0
gopkg.in/yaml.v2 v2.2.2
)
Loading