mirror of
https://github.com/grafana/grafana.git
synced 2025-09-27 23:43:39 +08:00
CLI: Add datasource list command
Lists all the datasources for an account via the CLI
This commit is contained in:
3
main.go
3
main.go
@ -32,7 +32,8 @@ func main() {
|
|||||||
app.Usage = "grafana web"
|
app.Usage = "grafana web"
|
||||||
app.Version = version
|
app.Version = version
|
||||||
app.Commands = []cli.Command{cmd.Web, cmd.ImportJson,
|
app.Commands = []cli.Command{cmd.Web, cmd.ImportJson,
|
||||||
cmd.ListAccounts, cmd.CreateAccount, cmd.DeleteAccount}
|
cmd.ListAccounts, cmd.CreateAccount, cmd.DeleteAccount,
|
||||||
|
cmd.ListDataSources}
|
||||||
app.Flags = append(app.Flags, []cli.Flag{}...)
|
app.Flags = append(app.Flags, []cli.Flag{}...)
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|
||||||
|
59
pkg/cmd/datasource.go
Normal file
59
pkg/cmd/datasource.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
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 ListDataSources = cli.Command{
|
||||||
|
Name: "datasource",
|
||||||
|
Usage: "list datasources",
|
||||||
|
Description: "Lists the datasources in the system",
|
||||||
|
Action: listDatasources,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "config",
|
||||||
|
Value: "grafana.ini",
|
||||||
|
Usage: "path to config file",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func listDatasources(c *cli.Context) {
|
||||||
|
setting.NewConfigContext()
|
||||||
|
sqlstore.NewEngine()
|
||||||
|
sqlstore.EnsureAdminUser()
|
||||||
|
|
||||||
|
if !c.Args().Present() {
|
||||||
|
log.ConsoleFatal("Account name arg is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
name := c.Args().First()
|
||||||
|
accountQuery := m.GetAccountByNameQuery{Name: name}
|
||||||
|
if err := bus.Dispatch(&accountQuery); err != nil {
|
||||||
|
log.ConsoleFatalf("Failed to find account: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
accountId := accountQuery.Result.Id
|
||||||
|
|
||||||
|
query := m.GetDataSourcesQuery{AccountId: accountId}
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
log.ConsoleFatalf("Failed to find datasources: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := tabwriter.NewWriter(os.Stdout, 20, 1, 4, ' ', 0)
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "ID\tNAME\tURL\tTYPE\tACCESS\tDEFAULT\n")
|
||||||
|
for _, ds := range query.Result {
|
||||||
|
fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%t\n", ds.Id, ds.Name, ds.Url, ds.Type,
|
||||||
|
ds.Access, ds.IsDefault)
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
}
|
Reference in New Issue
Block a user