Skip to content

Commit

Permalink
Add basic configuration object to set global channel and per repo
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranza committed Sep 23, 2018
1 parent af860f7 commit 81b472f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
32 changes: 32 additions & 0 deletions configuration/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package configuration

import (
"fmt"
"gopkg.in/yaml.v2"
)

// Configuration holds the general configuration for the executable
type Configuration struct {
DefaultChannel string `yaml:"default_channel"`
Repositories map[string]string `yaml:"repositories"`
}

var config Configuration

// Load loads the configuration and sets it globally if loading succeeded
func Load(in []byte) error {
c := Configuration{}
err := yaml.Unmarshal(in, &c)
if err != nil {
return fmt.Errorf("failed to parse configuration: %s", err)
}

config = c

return nil
}

// GetConfiguration returns the current configuration
func GetConfiguration() Configuration {
return config
}
27 changes: 27 additions & 0 deletions configuration/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package configuration_test

import (
"github.com/stretchr/testify/assert"
"gitlab.com/yakshaving.art/propaganda/configuration"
"io/ioutil"
"testing"
)

func TestLoadingValidConfiguration(t *testing.T) {
a := assert.New(t)

b, err := ioutil.ReadFile("fixtures/valid.yml")
a.NoError(err)

a.NoError(configuration.Load(b))

a.EqualValues(
configuration.Configuration{
DefaultChannel: "#propaganda",
Repositories: map[string]string{
"pcarranza/test-webhooks": "#private_channel",
},
},
configuration.GetConfiguration(),
)
}
4 changes: 4 additions & 0 deletions configuration/fixtures/valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
default_channel: "#propaganda"
repositories:
pcarranza/test-webhooks: "#private_channel"

0 comments on commit 81b472f

Please sign in to comment.