Skip to content

Commit

Permalink
issue #5 codegangsta/cli -> spf13/cobra
Browse files Browse the repository at this point in the history
Allowed configuration via config file, env variables and cli flags
  • Loading branch information
vasiliy-t committed Oct 24, 2015
1 parent a564c33 commit bbc2386
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 97 deletions.
105 changes: 67 additions & 38 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cmd

import (
"github.com/Unknwon/macaron"
"github.com/codegangsta/cli"
"github.com/macaron-contrib/bindata"
"github.com/macaron-contrib/binding"
"github.com/macaron-contrib/sockets"
"github.com/spf13/cobra"
"gitlab.com/kanban/kanban/templates"
"gitlab.com/kanban/kanban/web"
"gitlab.com/kanban/kanban/ws"
Expand All @@ -22,45 +22,73 @@ import (
"gitlab.com/kanban/kanban/routers/user"

"github.com/spf13/viper"

"strings"
)

// DaemonCmd is implementation of command to run application in daemon mode
var DaemonCmd = cli.Command{
Name: "daemon",
Usage: "Start serving web traffic",
Flags: []cli.Flag{
cli.StringFlag{
Name: "listen",
Value: "0.0.0.0:80",
Usage: "IP:PORT to listen on",
},
cli.StringFlag{
Name: "config",
Value: "",
Usage: "Custom config file",
},
cli.StringFlag{
Name: "redis",
Value: "",
Usage: "Redis host and port 127.0.0.1:6379",
},
cli.StringFlag{
Name: "gitlab-client-id",
Value: "",
Usage: "Gitlab oauth2 client id",
},
cli.StringFlag{
Name: "gitlab-client-secret",
Value: "",
Usage: "Gitlab oauth2 client secret",
},
},
Action: daemon,
var DaemonCmd = cobra.Command{
Use: "server",
Short: "Starts LeanLabs Kanban board application",
Long: `Start LeanLabs Kanban board application.
Please refer to http://kanban.leanlabs.io/documentation/Home for full documentation.
Report bugs to <[email protected]> or https://gitter.im/leanlabsio/kanban.
`,
Run: daemon,
}

func daemon(c *cli.Context) {
m := macaron.New()
func init() {
DaemonCmd.Flags().String(
"listen",
"0.0.0.0:80",
"IP:PORT to listen on",
)
DaemonCmd.Flags().String(
"hostname",
"http://localhost",
"URL on which Leanlabs Kanban will be reachable",
)
DaemonCmd.Flags().String(
"security-secret",
"qwerty",
"This string is used to generate user auth tokens",
)
DaemonCmd.Flags().String(
"gitlab-url",
"https://gitlab.com",
"Your GitLab host URL",
)
DaemonCmd.Flags().String(
"gitlab-client",
"qwerty",
"Your GitLab OAuth client ID",
)
DaemonCmd.Flags().String(
"gitlab-secret",
"qwerty",
"Your GitLab OAuth client secret key",
)
DaemonCmd.Flags().String(
"redis-addr",
"127.0.0.1:6379",
"Redis server address - IP:PORT",
)
DaemonCmd.Flags().String(
"redis-password",
"",
"Redis server password, empty string if none",
)
DaemonCmd.Flags().Int64(
"redis-db",
0,
"Redis server database numeric index, from 0 to 16",
)
}

func daemon(c *cobra.Command, a []string) {
m := macaron.New()
setting.NewContext(c)
err := models.NewEngine()

Expand Down Expand Up @@ -109,7 +137,7 @@ func daemon(c *cli.Context) {
AssetNames: web.AssetNames,
Prefix: "web",
}),
Prefix: c.App.Version,
Prefix: viper.GetString("version"),
},
))

Expand All @@ -120,7 +148,6 @@ func daemon(c *cli.Context) {

m.Post("/api/login", binding.Json(auth.SignIn{}), user.SignIn)
m.Post("/api/register", binding.Json(auth.SignUp{}), user.SignUp)

m.Group("/api", func() {
m.Get("/boards", board.ListBoards)
m.Post("/boards/configure", binding.Json(models.BoardRequest{}), board.Configure)
Expand All @@ -144,8 +171,10 @@ func daemon(c *cli.Context) {
}, middleware.Auther())
m.Get("/*", routers.Home)
m.Get("/ws/", sockets.Messages(), ws.ListenAndServe)
log.Printf("Listen: %s", viper.GetString("listen"))
err = http.ListenAndServe(viper.GetString("listen"), m)

listen := strings.TrimSuffix(viper.GetString("server.listen"), "/")
log.Printf("Listen: %s", listen)
err = http.ListenAndServe(listen, m)

if err != nil {
log.Fatalf("Failed to start: %s", err)
Expand Down
23 changes: 9 additions & 14 deletions conf/config.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
[server]

host = "127.0.0.1"
domain = "http://localhost:9000"
port = 9000
listen = "0.0.0.0:80"
hostname = "http://localhost"

[security]

secret_key = "qwerty"
secret = "qwerty"

[gitlab]

oauth_client_id = "224ca1e7fcd83afa7368bf45d73d52bee1e8279ee8e322eaf7c66735ee157cce"
oauth_client_secret = "6d532fbbfdcad6ed9efb0bf84407a0092e20fef3e51b9f12c3537c0c48ed6e78"
host = "https://gitlab.com"
url = "https://gitlab.com"
client = "qwerty"
secret = "qwerty"

[redis]

host = "10.0.3.10:6379"
passwd = ""
DB = 0
addr = "127.0.0.1:6379"
password = ""
db = 0
27 changes: 7 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
package main // import "gitlab.com/kanban/kanban"

import (
"github.com/codegangsta/cli"
"github.com/spf13/cobra"
"gitlab.com/kanban/kanban/cmd"
"os"
)

// AppVer defines application version
const AppVer = "1.2.4"

func main() {
app := cli.NewApp()
app.Name = "kanban"
app.Email = "[email protected]"
app.Usage = "Leanlab.io kanban board"
app.Version = AppVer
app.Commands = []cli.Command{
cmd.DaemonCmd,
kbCmd := &cobra.Command{
Use: "kanban",
Long: "Here should be brief desc http://kanban.leanlabs.io",
}
app.Authors = []cli.Author{
cli.Author{
Name: "V",
Email: "[email protected]",
},
cli.Author{
Name: "cnam",
Email: "[email protected]",
},
}
app.Run(os.Args)

kbCmd.AddCommand(&cmd.DaemonCmd)
kbCmd.Execute()
}
15 changes: 7 additions & 8 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"gitlab.com/kanban/kanban/modules/gitlab"
"golang.org/x/oauth2"
"gopkg.in/redis.v3"
"strings"
"log"
"strings"
)

var (
Expand All @@ -15,16 +15,15 @@ var (

// NewEngine creates new services for data from config settings
func NewEngine() error {

gh := strings.TrimSuffix(viper.GetString("gitlab.host"), "/")
d := strings.TrimSuffix(viper.GetString("server.domain"), "/")
gh := strings.TrimSuffix(viper.GetString("gitlab.url"), "/")
d := strings.TrimSuffix(viper.GetString("server.hostname"), "/")

gitlab.NewEngine(&gitlab.Config{
BasePath: gh + "/api/v3",
Domain: d,
Oauth2: &oauth2.Config{
ClientID: viper.GetString("gitlab.oauth_client_id"),
ClientSecret: viper.GetString("gitlab.oauth_client_secret"),
ClientID: viper.GetString("gitlab.client"),
ClientSecret: viper.GetString("gitlab.secret"),
Endpoint: oauth2.Endpoint{
AuthURL: gh + "/oauth/authorize",
TokenURL: gh + "/oauth/token",
Expand All @@ -34,8 +33,8 @@ func NewEngine() error {
})

c = redis.NewClient(&redis.Options{
Addr: viper.GetString("redis.host"),
Password: viper.GetString("redis.passwd"),
Addr: viper.GetString("redis.addr"),
Password: viper.GetString("redis.password"),
DB: int64(viper.GetInt("redis.db")),
})

Expand Down
72 changes: 56 additions & 16 deletions modules/setting/settings.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
package setting

import (
"github.com/codegangsta/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"strings"
)

// NewContext created new context for settings
func NewContext(c *cli.Context) {
func NewContext(c *cobra.Command) {

viper.SetConfigName("config")
viper.AddConfigPath("conf")
viper.AddConfigPath(c.String("config"))
viper.SetConfigType("toml")
viper.ReadInConfig()

repl := strings.NewReplacer(".", "_")

viper.SetEnvPrefix("kanban")
viper.SetEnvKeyReplacer(repl)

viper.SetDefault("server.listen", "0.0.0.0:80")
viper.BindEnv("server.listen")
if c.Flags().Lookup("listen").Changed {
viper.BindPFlag("server.listen", c.Flags().Lookup("listen"))
}

viper.SetDefault("server.hostname", "http://localhost")
viper.BindEnv("server.hostname")
if c.Flags().Lookup("hostname").Changed {
viper.BindPFlag("server.hostname", c.Flags().Lookup("hostname"))
}

viper.SetDefault("security.secret", "qwerty")
viper.BindEnv("security.secret")
if c.Flags().Lookup("security-secret").Changed {
viper.BindPFlag("security.secret", c.Flags().Lookup("security-secret"))
}

err := viper.ReadInConfig()
viper.SetDefault("gitlab.url", "https://gitlab.com")
viper.BindEnv("gitlab.url")
if c.Flags().Lookup("gitlab-url").Changed {
viper.BindPFlag("gitlab.url", c.Flags().Lookup("gitlab-url"))
}

if err != nil {
log.Fatal("Fatal error config file: %s \n", err)
viper.SetDefault("gitlab.client", "qwerty")
viper.BindEnv("gitlab.client")
if c.Flags().Lookup("gitlab-client").Changed {
viper.BindPFlag("gitlab.client", c.Flags().Lookup("gitlab-client"))
}

viper.Set("Version", c.App.Version)
if "" != c.String("redis") {
viper.Set("redis.host", c.String("redis"))
viper.SetDefault("gitlab.secret", "qwerty")
viper.BindEnv("gitlab.secret")
if c.Flags().Lookup("gitlab-secret").Changed {
viper.BindPFlag("gitlab.secret", c.Flags().Lookup("gitlab-secret"))
}
if "" != c.String("gitlab-client-id") {
viper.Set("gitlab.oauth_client_id", c.String("gitlab-client-id"))

viper.SetDefault("redis.addr", "127.0.0.1:6379")
viper.BindEnv("redis.addr")
if c.Flags().Lookup("redis-addr").Changed {
viper.BindPFlag("redis.addr", c.Flags().Lookup("redis-addr"))
}
if "" != c.String("gitlab-client-secret") {
viper.Set("gitlab.oauth_client_secret", c.String("gitlab-client-secret"))

viper.SetDefault("redis.password", "")
viper.BindEnv("redis.password")
if c.Flags().Lookup("redis-password").Changed {
viper.BindPFlag("redis.password", c.Flags().Lookup("redis-password"))
}

if l := c.String("listen"); l != "" {
viper.Set("listen", l)
viper.SetDefault("redis.db", 0)
viper.BindEnv("redis.db")
if c.Flags().Lookup("redis-db").Changed {
viper.BindPFlag("redis.db", c.Flags().Lookup("redis-db"))
}

viper.SetDefault("version", "1.3")
}
2 changes: 1 addition & 1 deletion routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
// Home returns main page
func Home(ctx *macaron.Context) {
ctx.Data["Version"] = viper.GetString("version")
ctx.Data["GitlabHost"] = viper.GetString("gitlab.host")
ctx.Data["GitlabHost"] = viper.GetString("gitlab.url")
ctx.HTML(200, "templates/index")
}

0 comments on commit bbc2386

Please sign in to comment.