mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 02:31:50 +08:00
CLI: Add datasource:delete command
This commit is contained in:
2
main.go
2
main.go
@ -33,7 +33,7 @@ func main() {
|
|||||||
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, cmd.CreateDataSource, cmd.DescribeDataSource}
|
cmd.ListDataSources, cmd.CreateDataSource, cmd.DescribeDataSource, cmd.DeleteDataSource}
|
||||||
app.Flags = append(app.Flags, []cli.Flag{
|
app.Flags = append(app.Flags, []cli.Flag{
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "config",
|
Name: "config",
|
||||||
|
@ -60,6 +60,12 @@ var (
|
|||||||
Description: "Describes the details of a datasource",
|
Description: "Describes the details of a datasource",
|
||||||
Action: describeDataSource,
|
Action: describeDataSource,
|
||||||
}
|
}
|
||||||
|
DeleteDataSource = cli.Command{
|
||||||
|
Name: "datasource:delete",
|
||||||
|
Usage: "Deletes a datasource",
|
||||||
|
Description: "Deletes a datasource",
|
||||||
|
Action: deleteDataSource,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func createDataSource(c *cli.Context) {
|
func createDataSource(c *cli.Context) {
|
||||||
@ -198,3 +204,36 @@ func describeDataSource(c *cli.Context) {
|
|||||||
}
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteDataSource(c *cli.Context) {
|
||||||
|
setting.NewConfigContext()
|
||||||
|
sqlstore.NewEngine()
|
||||||
|
sqlstore.EnsureAdminUser()
|
||||||
|
|
||||||
|
if len(c.Args()) != 2 {
|
||||||
|
log.ConsoleFatal("Account and datasource name args are required")
|
||||||
|
}
|
||||||
|
|
||||||
|
name := c.Args().First()
|
||||||
|
ds := c.Args()[1]
|
||||||
|
|
||||||
|
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.GetDataSourceByNameQuery{AccountId: accountId, Name: ds}
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
log.ConsoleFatalf("Failed to find datasource: %s", err)
|
||||||
|
}
|
||||||
|
datasource := query.Result
|
||||||
|
|
||||||
|
cmd := m.DeleteDataSourceCommand{AccountId: accountId, Id: datasource.Id}
|
||||||
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
|
log.ConsoleFatalf("Failed to delete datasource: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.ConsoleInfof("DataSource %s deleted", ds)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user