Skip to content
This repository has been archived by the owner on May 6, 2021. It is now read-only.

Commit

Permalink
Read config from env vars (#24)
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
tinakurian authored and sbose78 committed Nov 13, 2018
1 parent 577e55e commit 22bd6ab
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 167 deletions.
37 changes: 0 additions & 37 deletions config-template.yml

This file was deleted.

23 changes: 0 additions & 23 deletions config/auth.go

This file was deleted.

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"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
115 changes: 109 additions & 6 deletions config/configuration.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,113 @@
/*
Package config implements a way to to
handle configuration management using
viper.
*/
package config

// Configuration for build tool detector
import (
"strings"

"github.com/spf13/viper"
)

const (
authURI = "auth.uri"
serverHost = "server.host"
serverPort = "server.port"
metricsPort = "server.port"
githubClientID = "github.client.id"
githubClientSecret = "github.client.secret"
sentryDSN = "sentry.dsn"
)

const (
defaultAuth = "https://auth.prod-preview.openshift.io"
defaultHost = "localhost"
defaultPort = "8099"
)

const (
prefix = "BUILD_TOOL_DETECTOR"
authKeysPath = "/api/token/keys"
)

// Configuration for build tool detector.
type Configuration struct {
Auth AuthConfiguration
Github GithubConfiguration
Sentry SentryConfiguration
Server ServerConfiguration
Metrics ServerConfiguration
viper *viper.Viper
}

// New returns a configuration with defaults set.
func New() *Configuration {

// Create new viper
configuration := Configuration{
viper: viper.New(),
}

// Setup configuration.
configuration.viper.SetEnvPrefix(prefix)
configuration.viper.AutomaticEnv()
configuration.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
configuration.viper.SetTypeByDefaultValue(true)
configuration.setConfigDefaults()

return &configuration
}

// GetAuthServiceURL returns the server's port.
func (c *Configuration) GetAuthServiceURL() string {
return c.viper.GetString(authURI)
}

// GetHost returns the server's host.
func (c *Configuration) GetHost() string {
return c.viper.GetString(serverHost)
}

// GetPort returns the server's port.
func (c *Configuration) GetPort() string {
return c.viper.GetString(serverPort)
}

// GetMetricsPort returns the server's port.
func (c *Configuration) GetMetricsPort() string {
return c.viper.GetString(metricsPort)
}

// GetGithubClientID returs the github client id.
func (c *Configuration) GetGithubClientID() string {
return c.viper.GetString(githubClientID)
}

// GetGithubClientSecret returs the github client id.
func (c *Configuration) GetGithubClientSecret() string {
return c.viper.GetString(githubClientSecret)
}

// GetSentryDSN returs the github client id.
func (c *Configuration) GetSentryDSN() string {
return c.viper.GetString(sentryDSN)
}

// GetAuthKeysPath provides a URL path to be called for retrieving the keys.
func (c *Configuration) GetAuthKeysPath() string {
// Fixed with https://github.com/fabric8-services/fabric8-common/pull/25.
return authKeysPath
}

// GetDevModePrivateKey not used right now.
func (c *Configuration) GetDevModePrivateKey() []byte {
// No need for now
return nil
}

// setConfigDefaults sets defaults for configuration.
func (c *Configuration) setConfigDefaults() {
c.viper.SetDefault(authURI, defaultAuth)
c.viper.SetDefault(serverHost, defaultHost)
c.viper.SetDefault(serverPort, defaultPort)
c.viper.SetDefault(metricsPort, defaultPort)
}
68 changes: 68 additions & 0 deletions config/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config_test

import (
"os"

"github.com/fabric8-services/build-tool-detector/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gopkg.in/h2non/gock.v1"
)

var _ = Describe("Configuration", func() {
Context("Configuration Defaults", func() {
var configuration *config.Configuration

BeforeEach(func() {
configuration = config.New()
})
AfterEach(func() {
gock.Off()
})

It("Configuration defaults - test defaults are set", func() {
Expect(configuration.GetHost()).Should(Equal("localhost"), "the host should default to localhost")
Expect(configuration.GetPort()).Should(Equal("8099"), "the port should default to 8099")
Expect(configuration.GetMetricsPort()).Should(Equal("8099"), "the metrics port should default to 8099")
Expect(configuration.GetAuthServiceURL()).Should(Equal("https://auth.prod-preview.openshift.io"), "the auth url should default to https://auth.prod-preview.openshift.io")
Expect(configuration.GetGithubClientID()).Should(Equal(""), "the github client id should default to empty")
Expect(configuration.GetGithubClientSecret()).Should(Equal(""), "the github client secret should default to empty")
Expect(configuration.GetSentryDSN()).Should(Equal(""), "the sentry dsn should default to empty")
Expect(configuration.GetAuthKeysPath()).Should(Equal("/api/token/keys"), "the sentry dsn should return /api/token/keys")
})
})

Context("Configuration Overriden Defaults", func() {
var configuration *config.Configuration

BeforeEach(func() {
os.Setenv("BUILD_TOOL_DETECTOR_METRICS_PORT", "1234")
os.Setenv("BUILD_TOOL_DETECTOR_SERVER_PORT", "1234")
os.Setenv("BUILD_TOOL_DETECTOR_SERVER_HOST", "test")
os.Setenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_ID", "test")
os.Setenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_SECRET", "test")
os.Setenv("BUILD_TOOL_DETECTOR_AUTH_URI", "test")
os.Setenv("BUILD_TOOL_DETECTOR_SENTRY_DSN", "test")
configuration = config.New()
})
AfterEach(func() {
gock.Off()
os.Unsetenv("BUILD_TOOL_DETECTOR_METRICS_PORT")
os.Unsetenv("BUILD_TOOL_DETECTOR_SERVER_PORT")
os.Unsetenv("BUILD_TOOL_DETECTOR_SERVER_HOST")
os.Unsetenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_ID")
os.Unsetenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_SECRET")
os.Unsetenv("BUILD_TOOL_DETECTOR_AUTH_URI")
os.Unsetenv("BUILD_TOOL_DETECTOR_SENTRY_DSN")
})
It("Configuration defaults - test defaults are set", func() {
Expect(configuration.GetHost()).Should(Equal("test"), "the host should default to test")
Expect(configuration.GetPort()).Should(Equal("1234"), "the port should default to 1234")
Expect(configuration.GetMetricsPort()).Should(Equal("1234"), "the metrics port should default to 1234")
Expect(configuration.GetAuthServiceURL()).Should(Equal("test"), "the auth url should default to test")
Expect(configuration.GetGithubClientID()).Should(Equal("test"), "the github client id should default to test")
Expect(configuration.GetGithubClientSecret()).Should(Equal("test"), "the github client secret should default to test")
Expect(configuration.GetSentryDSN()).Should(Equal("test"), "the sentry dsn should default to test")
})
})
})
7 changes: 0 additions & 7 deletions config/github.go

This file was deleted.

6 changes: 0 additions & 6 deletions config/sentry.go

This file was deleted.

7 changes: 0 additions & 7 deletions config/server.go

This file was deleted.

Loading

0 comments on commit 22bd6ab

Please sign in to comment.