datasource.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ListDataSources = cli.Command{
  14. Name: "datasource",
  15. Usage: "list datasources",
  16. Description: "Lists the datasources in the system",
  17. Action: listDatasources,
  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 listDatasources(c *cli.Context) {
  27. setting.NewConfigContext()
  28. sqlstore.NewEngine()
  29. sqlstore.EnsureAdminUser()
  30. if !c.Args().Present() {
  31. log.ConsoleFatal("Account name arg is required")
  32. }
  33. name := c.Args().First()
  34. accountQuery := m.GetAccountByNameQuery{Name: name}
  35. if err := bus.Dispatch(&accountQuery); err != nil {
  36. log.ConsoleFatalf("Failed to find account: %s", err)
  37. }
  38. accountId := accountQuery.Result.Id
  39. query := m.GetDataSourcesQuery{AccountId: accountId}
  40. if err := bus.Dispatch(&query); err != nil {
  41. log.ConsoleFatalf("Failed to find datasources: %s", err)
  42. }
  43. w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
  44. fmt.Fprintf(w, "ID\tNAME\tURL\tTYPE\tACCESS\tDEFAULT\n")
  45. for _, ds := range query.Result {
  46. fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%t\n", ds.Id, ds.Name, ds.Url, ds.Type,
  47. ds.Access, ds.IsDefault)
  48. }
  49. w.Flush()
  50. }