-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #5 codegangsta/cli -> spf13/cobra
Allowed configuration via config file, env variables and cli flags
- Loading branch information
Showing
6 changed files
with
147 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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() | ||
|
||
|
@@ -109,7 +137,7 @@ func daemon(c *cli.Context) { | |
AssetNames: web.AssetNames, | ||
Prefix: "web", | ||
}), | ||
Prefix: c.App.Version, | ||
Prefix: viper.GetString("version"), | ||
}, | ||
)) | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters