Browse Source

CLI: Fix config flag being ignored

Passing --config had no effect when passed.  It will now be applied as
the last config file and before any env var overrrides.
Jason Wilder 11 năm trước cách đây
mục cha
commit
b6428b08d0
8 tập tin đã thay đổi với 31 bổ sung38 xóa
  1. 1 2
      main.go
  2. 3 10
      pkg/cmd/accounts.go
  3. 13 0
      pkg/cmd/common.go
  4. 1 5
      pkg/cmd/dashboard.go
  5. 4 14
      pkg/cmd/datasource.go
  6. 2 4
      pkg/cmd/web.go
  7. 5 1
      pkg/setting/setting.go
  8. 2 2
      pkg/setting/setting_test.go

+ 1 - 2
main.go

@@ -39,8 +39,7 @@ func main() {
 	app.Flags = append(app.Flags, []cli.Flag{
 		cli.StringFlag{
 			Name:  "config",
-			Value: "grafana.ini",
-			Usage: "path to config file",
+			Usage: "path to grafana.ini config file",
 		},
 	}...)
 	app.Run(os.Args)

+ 3 - 10
pkg/cmd/accounts.go

@@ -6,7 +6,6 @@ import (
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/log"
 	m "github.com/grafana/grafana/pkg/models"
-	"github.com/grafana/grafana/pkg/services/sqlstore"
 	"github.com/grafana/grafana/pkg/setting"
 	"os"
 	"text/tabwriter"
@@ -34,9 +33,7 @@ var DeleteAccount = cli.Command{
 }
 
 func listAccounts(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	accountsQuery := m.GetAccountsQuery{}
 	if err := bus.Dispatch(&accountsQuery); err != nil {
@@ -53,9 +50,7 @@ func listAccounts(c *cli.Context) {
 }
 
 func createAccount(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	if !c.Args().Present() {
 		log.ConsoleFatal("Account name arg is required")
@@ -80,9 +75,7 @@ func createAccount(c *cli.Context) {
 }
 
 func deleteAccount(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	if !c.Args().Present() {
 		log.ConsoleFatal("Account name arg is required")

+ 13 - 0
pkg/cmd/common.go

@@ -0,0 +1,13 @@
+package cmd
+
+import (
+	"github.com/codegangsta/cli"
+	"github.com/grafana/grafana/pkg/services/sqlstore"
+	"github.com/grafana/grafana/pkg/setting"
+)
+
+func initRuntime(c *cli.Context) {
+	setting.NewConfigContext(c.GlobalString("config"))
+	sqlstore.NewEngine()
+	sqlstore.EnsureAdminUser()
+}

+ 1 - 5
pkg/cmd/dashboard.go

@@ -10,8 +10,6 @@ import (
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/log"
 	m "github.com/grafana/grafana/pkg/models"
-	"github.com/grafana/grafana/pkg/services/sqlstore"
-	"github.com/grafana/grafana/pkg/setting"
 )
 
 var ImportDashboard = cli.Command{
@@ -48,9 +46,7 @@ func runImport(c *cli.Context) {
 
 	accountName := c.Args().First()
 
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	accountQuery := m.GetAccountByNameQuery{Name: accountName}
 	if err := bus.Dispatch(&accountQuery); err != nil {

+ 4 - 14
pkg/cmd/datasource.go

@@ -6,8 +6,6 @@ import (
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/log"
 	m "github.com/grafana/grafana/pkg/models"
-	"github.com/grafana/grafana/pkg/services/sqlstore"
-	"github.com/grafana/grafana/pkg/setting"
 	"os"
 	"text/tabwriter"
 )
@@ -69,9 +67,7 @@ var (
 )
 
 func createDataSource(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	if len(c.Args()) != 3 {
 		log.ConsoleFatal("Missing required arguments")
@@ -131,9 +127,7 @@ func createDataSource(c *cli.Context) {
 }
 
 func listDatasources(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	if !c.Args().Present() {
 		log.ConsoleFatal("Account name arg is required")
@@ -163,9 +157,7 @@ func listDatasources(c *cli.Context) {
 }
 
 func describeDataSource(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	if len(c.Args()) != 2 {
 		log.ConsoleFatal("Account and datasource name args are required")
@@ -206,9 +198,7 @@ func describeDataSource(c *cli.Context) {
 }
 
 func deleteDataSource(c *cli.Context) {
-	setting.NewConfigContext()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
+	initRuntime(c)
 
 	if len(c.Args()) != 2 {
 		log.ConsoleFatal("Account and datasource name args are required")

+ 2 - 4
pkg/cmd/web.go

@@ -17,7 +17,6 @@ import (
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/middleware"
 	"github.com/grafana/grafana/pkg/services/eventpublisher"
-	"github.com/grafana/grafana/pkg/services/sqlstore"
 	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/social"
 )
@@ -71,10 +70,9 @@ func runWeb(c *cli.Context) {
 	log.Info("Starting Grafana")
 	log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
 
-	setting.NewConfigContext()
+	initRuntime(c)
+
 	social.NewOAuthService()
-	sqlstore.NewEngine()
-	sqlstore.EnsureAdminUser()
 	eventpublisher.Init()
 
 	var err error

+ 5 - 1
pkg/setting/setting.go

@@ -164,9 +164,13 @@ func loadEnvVariableOverrides() {
 	}
 }
 
-func NewConfigContext() {
+func NewConfigContext(config string) {
 	configFiles := findConfigFiles()
 
+	if config != "" {
+		configFiles = append(configFiles, config)
+	}
+
 	//log.Info("Loading config files: %v", configFiles)
 	var err error
 

+ 2 - 2
pkg/setting/setting_test.go

@@ -15,7 +15,7 @@ func TestLoadingSettings(t *testing.T) {
 	Convey("Testing loading settings from ini file", t, func() {
 
 		Convey("Given the default ini files", func() {
-			NewConfigContext()
+			NewConfigContext("")
 
 			So(AppName, ShouldEqual, "Grafana")
 			So(AdminUser, ShouldEqual, "admin")
@@ -23,7 +23,7 @@ func TestLoadingSettings(t *testing.T) {
 
 		Convey("Should be able to override via environment variables", func() {
 			os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
-			NewConfigContext()
+			NewConfigContext("")
 
 			So(AdminUser, ShouldEqual, "superduper")
 		})