From 42896b3711419683197a25b4f6abb0bc1477a4fa Mon Sep 17 00:00:00 2001
From: Ben Schumacher <benschumacher@gmail.com>
Date: Thu, 28 May 2015 01:18:46 -0600
Subject: [PATCH] Replace pkg/settings with pkg/config

---
 cmd/drone-server/drone.go     | 10 +++++-----
 pkg/config/config.go          |  6 +++++-
 pkg/remote/github/github.go   | 18 +++++++++---------
 pkg/server/login.go           | 22 +++++++++++-----------
 pkg/server/server.go          | 14 +++++++-------
 pkg/server/session/session.go |  6 +++---
 pkg/server/ws.go              |  2 +-
 7 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/cmd/drone-server/drone.go b/cmd/drone-server/drone.go
index bb34b8027..8f80ea6fb 100644
--- a/cmd/drone-server/drone.go
+++ b/cmd/drone-server/drone.go
@@ -8,10 +8,10 @@ import (
 	"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
 
 	"github.com/drone/drone/Godeps/_workspace/src/github.com/elazarl/go-bindata-assetfs"
+	"github.com/drone/drone/pkg/config"
 	"github.com/drone/drone/pkg/remote/github"
 	"github.com/drone/drone/pkg/server"
 	"github.com/drone/drone/pkg/server/session"
-	"github.com/drone/drone/pkg/settings"
 
 	log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
 	eventbus "github.com/drone/drone/pkg/bus/builtin"
@@ -37,7 +37,7 @@ var (
 func main() {
 	flag.Parse()
 
-	settings, err := settings.Parse(*conf)
+	settings, err := config.Load(*conf)
 	if err != nil {
 		panic(err)
 	}
@@ -46,8 +46,8 @@ func main() {
 	store := store.New(db)
 	defer db.Close()
 
-	remote := github.New(settings.Service)
-	session := session.New(settings.Session)
+	remote := github.New(settings)
+	session := session.New(settings)
 	eventbus_ := eventbus.New()
 	queue_ := queue.New()
 	updater := runner.NewUpdater(eventbus_, store, remote)
@@ -55,7 +55,7 @@ func main() {
 
 	// launch the local queue runner if the system
 	// is not conifugred to run in agent mode
-	if settings.Agents != nil && settings.Agents.Secret != "" {
+	if len(settings.Agents.Secret) != 0 {
 		log.Infof("Run builds using remote build agents")
 	} else {
 		log.Infof("Run builds using the embedded build runner")
diff --git a/pkg/config/config.go b/pkg/config/config.go
index b6b938698..074b9c6d0 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -40,6 +40,10 @@ type Config struct {
 		Expires int64  `envconfig:"optional"`
 	}
 
+	Agents struct {
+		Secret string `envconfig:"optional"`
+	}
+
 	Database struct {
 		Driver     string `envconfig:"optional"`
 		Datasource string `envconfig:"optional"`
@@ -56,7 +60,7 @@ type Config struct {
 
 	// Plugins represents a white-list of plugins
 	// that the system is authorized to load.
-	Plugins []string `envconfig:"white_list"`
+	Plugins []string `envconfig:"optional"`
 }
 
 // Load loads the configuration file and reads
diff --git a/pkg/remote/github/github.go b/pkg/remote/github/github.go
index 8374f759f..c10728bed 100644
--- a/pkg/remote/github/github.go
+++ b/pkg/remote/github/github.go
@@ -10,7 +10,7 @@ import (
 	"time"
 
 	"github.com/drone/drone/Godeps/_workspace/src/github.com/hashicorp/golang-lru"
-	"github.com/drone/drone/pkg/settings"
+	"github.com/drone/drone/pkg/config"
 	common "github.com/drone/drone/pkg/types"
 
 	"github.com/drone/drone/Godeps/_workspace/src/github.com/google/go-github/github"
@@ -33,14 +33,14 @@ type GitHub struct {
 	cache *lru.Cache
 }
 
-func New(service *settings.Service) *GitHub {
+func New(conf *config.Config) *GitHub {
 	var github = GitHub{
 		API:         DefaultAPI,
 		URL:         DefaultURL,
-		Client:      service.OAuth.Client,
-		Secret:      service.OAuth.Secret,
-		PrivateMode: service.PrivateMode,
-		SkipVerify:  service.SkipVerify,
+		Client:      conf.Auth.Client,
+		Secret:      conf.Auth.Secret,
+		PrivateMode: conf.Remote.Private,
+		SkipVerify:  conf.Remote.SkipVerify,
 	}
 	var err error
 	github.cache, err = lru.New(1028)
@@ -50,9 +50,9 @@ func New(service *settings.Service) *GitHub {
 
 	// if GitHub enterprise then ensure we're using the
 	// appropriate URLs
-	if !strings.HasPrefix(service.Base, DefaultURL) && len(service.Base) != 0 {
-		github.URL = service.Base
-		github.API = service.Base + "/api/v3/"
+	if !strings.HasPrefix(conf.Remote.Base, DefaultURL) && len(conf.Remote.Base) != 0 {
+		github.URL = conf.Remote.Base
+		github.API = conf.Remote.Base + "/api/v3/"
 	}
 	// the API must have a trailing slash
 	if !strings.HasSuffix(github.API, "/") {
diff --git a/pkg/server/login.go b/pkg/server/login.go
index 002fe69b1..b38a33a49 100644
--- a/pkg/server/login.go
+++ b/pkg/server/login.go
@@ -36,9 +36,9 @@ func GetLogin(c *gin.Context) {
 	// Auth (username and password). This will delegate
 	// authorization accordingly.
 	switch {
-	case settings.Service.OAuth == nil:
-		getLoginBasic(c)
-	case settings.Service.OAuth.RequestToken != "":
+	// case settings.Auth == nil:
+	// 	getLoginBasic(c)
+	case settings.Auth.RequestToken != "":
 		getLoginOauth1(c)
 	default:
 		getLoginOauth2(c)
@@ -52,9 +52,9 @@ func GetLogin(c *gin.Context) {
 	login := ToUser(c)
 
 	// check organization membership, if applicable
-	if len(settings.Service.Orgs) != 0 {
+	if len(settings.Remote.Orgs) != 0 {
 		orgs, _ := remote.Orgs(login)
-		if !checkMembership(orgs, settings.Service.Orgs) {
+		if !checkMembership(orgs, settings.Remote.Orgs) {
 			c.Redirect(303, "/login#error=access_denied_org")
 			return
 		}
@@ -73,7 +73,7 @@ func GetLogin(c *gin.Context) {
 		// if self-registration is disabled we should
 		// return a notAuthorized error. the only exception
 		// is if no users exist yet in the system we'll proceed.
-		if !settings.Service.Open && count != 0 {
+		if !settings.Remote.Open && count != 0 {
 			log.Errorf("cannot register %s. registration closed", login.Login)
 			c.Redirect(303, "/login#error=access_denied")
 			return
@@ -137,11 +137,11 @@ func getLoginOauth2(c *gin.Context) {
 	var remote = ToRemote(c)
 
 	var config = &oauth2.Config{
-		ClientId:     settings.Service.OAuth.Client,
-		ClientSecret: settings.Service.OAuth.Secret,
-		Scope:        strings.Join(settings.Service.OAuth.Scope, ","),
-		AuthURL:      settings.Service.OAuth.Authorize,
-		TokenURL:     settings.Service.OAuth.AccessToken,
+		ClientId:     settings.Auth.Client,
+		ClientSecret: settings.Auth.Secret,
+		Scope:        strings.Join(settings.Auth.Scope, ","),
+		AuthURL:      settings.Auth.Authorize,
+		TokenURL:     settings.Auth.AccessToken,
 		RedirectURL:  fmt.Sprintf("%s/authorize", httputil.GetURL(c.Request)),
 		//settings.Server.Scheme, settings.Server.Hostname),
 	}
diff --git a/pkg/server/server.go b/pkg/server/server.go
index 8df4b5793..3ae4e87b1 100644
--- a/pkg/server/server.go
+++ b/pkg/server/server.go
@@ -7,11 +7,11 @@ import (
 	"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
 
 	"github.com/drone/drone/pkg/bus"
+	"github.com/drone/drone/pkg/config"
 	"github.com/drone/drone/pkg/queue"
 	"github.com/drone/drone/pkg/remote"
 	"github.com/drone/drone/pkg/runner"
 	"github.com/drone/drone/pkg/server/session"
-	"github.com/drone/drone/pkg/settings"
 	"github.com/drone/drone/pkg/store"
 	common "github.com/drone/drone/pkg/types"
 )
@@ -91,17 +91,17 @@ func SetUpdater(u runner.Updater) gin.HandlerFunc {
 	}
 }
 
-func ToSettings(c *gin.Context) *settings.Settings {
-	v, ok := c.Get("settings")
+func ToSettings(c *gin.Context) *config.Config {
+	v, ok := c.Get("config")
 	if !ok {
 		return nil
 	}
-	return v.(*settings.Settings)
+	return v.(*config.Config)
 }
 
-func SetSettings(s *settings.Settings) gin.HandlerFunc {
+func SetSettings(s *config.Config) gin.HandlerFunc {
 	return func(c *gin.Context) {
-		c.Set("settings", s)
+		c.Set("config", s)
 		c.Next()
 	}
 }
@@ -249,7 +249,7 @@ func MustAgent() gin.HandlerFunc {
 		conf := ToSettings(c)
 
 		// verify remote agents are enabled
-		if conf.Agents == nil || len(conf.Agents.Secret) == 0 {
+		if len(conf.Agents.Secret) == 0 {
 			c.AbortWithStatus(405)
 			return
 		}
diff --git a/pkg/server/session/session.go b/pkg/server/session/session.go
index 1182bb18d..686c9fbf5 100644
--- a/pkg/server/session/session.go
+++ b/pkg/server/session/session.go
@@ -6,7 +6,7 @@ import (
 	"time"
 
 	"github.com/drone/drone/Godeps/_workspace/src/github.com/dgrijalva/jwt-go"
-	"github.com/drone/drone/pkg/settings"
+	"github.com/drone/drone/pkg/config"
 	common "github.com/drone/drone/pkg/types"
 )
 
@@ -20,8 +20,8 @@ type session struct {
 	expire time.Duration
 }
 
-func New(s *settings.Session) Session {
-	secret := []byte(s.Secret)
+func New(s *config.Config) Session {
+	secret := []byte(s.Session.Secret)
 	expire := time.Hour * 72
 	return &session{
 		secret: secret,
diff --git a/pkg/server/ws.go b/pkg/server/ws.go
index a56d1a35b..55adde1c1 100644
--- a/pkg/server/ws.go
+++ b/pkg/server/ws.go
@@ -76,7 +76,7 @@ func GetStream(c *gin.Context) {
 	// if the commit is being executed by an agent
 	// we'll proxy the build output directly to the
 	// remote Docker client, through the agent.
-	if conf.Agents != nil && conf.Agents.Secret != "" {
+	if conf.Agents.Secret != "" {
 		addr, err := store.Agent(commit)
 		if err != nil {
 			c.Fail(500, err)