datasource.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. }
  19. func listDatasources(c *cli.Context) {
  20. setting.NewConfigContext()
  21. sqlstore.NewEngine()
  22. sqlstore.EnsureAdminUser()
  23. if !c.Args().Present() {
  24. log.ConsoleFatal("Account name arg is required")
  25. }
  26. name := c.Args().First()
  27. accountQuery := m.GetAccountByNameQuery{Name: name}
  28. if err := bus.Dispatch(&accountQuery); err != nil {
  29. log.ConsoleFatalf("Failed to find account: %s", err)
  30. }
  31. accountId := accountQuery.Result.Id
  32. query := m.GetDataSourcesQuery{AccountId: accountId}
  33. if err := bus.Dispatch(&query); err != nil {
  34. log.ConsoleFatalf("Failed to find datasources: %s", err)
  35. }
  36. w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
  37. fmt.Fprintf(w, "ID\tNAME\tURL\tTYPE\tACCESS\tDEFAULT\n")
  38. for _, ds := range query.Result {
  39. fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%t\n", ds.Id, ds.Name, ds.Url, ds.Type,
  40. ds.Access, ds.IsDefault)
  41. }
  42. w.Flush()
  43. }