Jason Wilder 11 лет назад
Родитель
Сommit
ca37b24455
4 измененных файлов с 58 добавлено и 1 удалено
  1. 2 1
      main.go
  2. 47 0
      pkg/cmd/accounts.go
  3. 4 0
      pkg/models/account.go
  4. 5 0
      pkg/services/sqlstore/account.go

+ 2 - 1
main.go

@@ -31,7 +31,8 @@ func main() {
 	app.Name = "Grafana Backend"
 	app.Usage = "grafana web"
 	app.Version = version
-	app.Commands = []cli.Command{cmd.CmdWeb, cmd.CmdImportJson}
+	app.Commands = []cli.Command{cmd.CmdWeb, cmd.CmdImportJson,
+		cmd.CmdListAccounts}
 	app.Flags = append(app.Flags, []cli.Flag{}...)
 	app.Run(os.Args)
 

+ 47 - 0
pkg/cmd/accounts.go

@@ -0,0 +1,47 @@
+package cmd
+
+import (
+	"fmt"
+	"github.com/codegangsta/cli"
+	"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"
+)
+
+var CmdListAccounts = cli.Command{
+	Name:        "account",
+	Usage:       "list accounts",
+	Description: "Lists the accounts in the system",
+	Action:      listAccounts,
+	Flags: []cli.Flag{
+		cli.StringFlag{
+			Name:  "config",
+			Value: "grafana.ini",
+			Usage: "path to config file",
+		},
+	},
+}
+
+func listAccounts(c *cli.Context) {
+	setting.NewConfigContext()
+	sqlstore.NewEngine()
+	sqlstore.EnsureAdminUser()
+
+	accountsQuery := m.GetAccountsQuery{}
+	if err := bus.Dispatch(&accountsQuery); err != nil {
+		log.Error(3, "Failed to find accounts", err)
+		return
+	}
+
+	w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
+
+	fmt.Fprintf(w, "ID\tNAME\n")
+	for _, account := range accountsQuery.Result {
+		fmt.Fprintf(w, "%d\t%s\n", account.Id, account.Name)
+	}
+	w.Flush()
+}

+ 4 - 0
pkg/models/account.go

@@ -49,6 +49,10 @@ type GetAccountByNameQuery struct {
 	Result *Account
 }
 
+type GetAccountsQuery struct {
+	Result []*Account
+}
+
 type AccountDTO struct {
 	Id   int64  `json:"id"`
 	Name string `json:"name"`

+ 5 - 0
pkg/services/sqlstore/account.go

@@ -14,6 +14,11 @@ func init() {
 	bus.AddHandler("sql", SetUsingAccount)
 	bus.AddHandler("sql", UpdateAccount)
 	bus.AddHandler("sql", GetAccountByName)
+	bus.AddHandler("sql", GetAccountsQuery)
+}
+
+func GetAccountsQuery(query *m.GetAccountsQuery) error {
+	return x.Find(&query.Result)
 }
 
 func GetAccountById(query *m.GetAccountByIdQuery) error {