accounts.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/codegangsta/cli"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/log"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/sqlstore"
  9. "github.com/grafana/grafana/pkg/setting"
  10. "os"
  11. "text/tabwriter"
  12. )
  13. var CmdListAccounts = cli.Command{
  14. Name: "account",
  15. Usage: "list accounts",
  16. Description: "Lists the accounts in the system",
  17. Action: listAccounts,
  18. Flags: []cli.Flag{
  19. cli.StringFlag{
  20. Name: "config",
  21. Value: "grafana.ini",
  22. Usage: "path to config file",
  23. },
  24. },
  25. }
  26. func listAccounts(c *cli.Context) {
  27. setting.NewConfigContext()
  28. sqlstore.NewEngine()
  29. sqlstore.EnsureAdminUser()
  30. accountsQuery := m.GetAccountsQuery{}
  31. if err := bus.Dispatch(&accountsQuery); err != nil {
  32. log.Error(3, "Failed to find accounts", err)
  33. return
  34. }
  35. w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
  36. fmt.Fprintf(w, "ID\tNAME\n")
  37. for _, account := range accountsQuery.Result {
  38. fmt.Fprintf(w, "%d\t%s\n", account.Id, account.Name)
  39. }
  40. w.Flush()
  41. }