Browse Source

Reconfigure config package

master
Matei-Alexandru Gardus 10 months ago
parent
commit
ad125c8cab
Signed by: StormFireFox1 <matei@gardus.eu> GPG Key ID: F3D2B830AB9B94AA
1 changed files with 33 additions and 16 deletions
  1. 33
    16
      config/config.go

+ 33
- 16
config/config.go View File

@@ -2,44 +2,61 @@ package config

import (
"fmt"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stormfirefox1/GitHookParser/log"
)

var v = viper.New()
// Env is a struct that contains all the information for a server to run by getting the configuration necessary for it.
type Env struct {
config *viper.Viper
configPath string
}

// New returns a config object that can be used by the server struct.
func New(configPath string) Env {
filePath := strings.Split(configPath, "/")
configFile := strings.Split(filePath[len(filePath)-1], ".")
configName := configFile[0]
configFileType := configFile[1]

func init() {
v.SetConfigName("config")
v.SetConfigType("toml")
v.AddConfigPath("$HOME/.config/git-hook-parser/")
env := Env{
config: viper.New(),
configPath: configPath,
}

// setting up default variables for the whole shebang
env.config.SetConfigName(configName)
env.config.SetConfigType(configFileType)
env.config.AddConfigPath(strings.Join(filePath[:len(filePath)-1], "/"))

// setting up default variables for GitHub events (i.e. initial release support)
githubEvents := make([]string, 4)

githubEvents[0] = "push"
githubEvents[1] = "status"
githubEvents[2] = "pull_request"
githubEvents[3] = "issues"
githubEvents[3] = "ping"

// setting sensible defaults for project config
v.SetDefault("HANDLER_PORT", ":80")
v.SetDefault("REDIRECT_URL", "")
v.SetDefault("GITHUB_EVENTS", githubEvents)
env.config.SetDefault("HANDLER_PORT", ":80")
env.config.SetDefault("REDIRECT_URL", "")
env.config.SetDefault("GITHUB_EVENTS", githubEvents)
env.config.SetDefault("APP_VERSION", "0.1")

// allowing for override using envvars
v.AutomaticEnv()
env.config.AutomaticEnv()

// reading the config file
err := v.ReadInConfig()
err := env.config.ReadInConfig()

if err != nil {
log.Fatal(logrus.Fields{}, fmt.Errorf("Fatal error reading configuration file: %v", err))
}

return env
}

// Get will extract a value from the configuration constructs and returns it.
func Get(key string) interface{} {
return v.Get(key)
func (e *Env) Get(key string) interface{} {
return e.config.Get(key)
}

Loading…
Cancel
Save