From c35b0c29354284275dfff37d854a64d8399a2263 Mon Sep 17 00:00:00 2001 From: Alexander Grafov Date: Sun, 22 Jan 2017 02:19:16 +0300 Subject: [PATCH] Move to root of the repo. --- README.md | 2 +- .../do-backup.go => do-backup.go | 0 .../do-file-list.go => do-file-list.go | 0 do-object-list.go | 116 ++++++++++++++++++ .../do-restore.go => do-restore.go | 0 cmd/grafana-backup/main.go => main.go | 0 6 files changed, 117 insertions(+), 1 deletion(-) rename cmd/grafana-backup/do-backup.go => do-backup.go (100%) rename cmd/grafana-backup/do-file-list.go => do-file-list.go (100%) create mode 100644 do-object-list.go rename cmd/grafana-backup/do-restore.go => do-restore.go (100%) rename cmd/grafana-backup/main.go => main.go (100%) diff --git a/README.md b/README.md index 4ab8980..97194d6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ CLI for the simple backup/restore operations on Grafana dashboards and datasources. It uses [Grafana client SDK](https://github.com/grafana-tools/sdk). -**Work in progress. Current state: it works partially.** +**Work in progress. Current state: it may works, may not. Depends on build. Don't use it yet!** ## Examples diff --git a/cmd/grafana-backup/do-backup.go b/do-backup.go similarity index 100% rename from cmd/grafana-backup/do-backup.go rename to do-backup.go diff --git a/cmd/grafana-backup/do-file-list.go b/do-file-list.go similarity index 100% rename from cmd/grafana-backup/do-file-list.go rename to do-file-list.go diff --git a/do-object-list.go b/do-object-list.go new file mode 100644 index 0000000..dd70e40 --- /dev/null +++ b/do-object-list.go @@ -0,0 +1,116 @@ +// Backup tool for Grafana. +// Copyright (C) 2016-2017 Alexander I.Grafov +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ॐ तारे तुत्तारे तुरे स्व + +package main + +import ( + "fmt" + "os" + + "github.com/grafana-tools/sdk" +) + +func doObjectList(opts ...option) { + var ( + cmd = initCommand(opts...) + err error + ) + if cmd.applyForBoards { + listDashboards(cmd) + } + if cmd.applyForDs { + listDatasources(cmd) + } + if cmd.applyForUsers { + listUsers(cmd) + } +} + +func listDashboards(cmd *command) { + var ( + foundBoards []sdk.FoundBoard + err error + ) + if foundBoards, err = cmd.grafana.SearchDashboards(cmd.boardTitle, cmd.starred, cmd.tags...); err != nil { + fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err)) + return + } + for _, meta := range foundBoards { + select { + case <-cancel: + exit() + default: + fmt.Printf("<%d> \"%s\" %v ", meta.ID, meta.Title, meta.Tags) + if meta.IsStarred { + fmt.Print("*") + } + fmt.Println() + } + if cmd.verbose { + fmt.Printf("Found %d dashboards.\n", len(foundBoards)) + } + } +} + +func listDatasources(cmd *command) { + var ( + datasources []sdk.Datasource + err error + ) + if datasources, err = cmd.grafana.GetAllDatasources(); err != nil { + fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err)) + return + } + for _, ds := range datasources { + select { + case <-cancel: + exit() + default: + fmt.Printf("<%d> \"%s\" (%s) %s\n", ds.ID, ds.Name, ds.Type, ds.URL) + } + if cmd.verbose { + fmt.Printf("Found %d datasources.\n", len(datasources)) + } + } +} + +func listUsers(cmd *command) { + var ( + allUsers []sdk.User + err error + ) + if allUsers, err = cmd.grafana.GetAllUsers(); err != nil { + fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", err)) + return + } + for _, user := range allUsers { + select { + case <-cancel: + exit() + default: + fmt.Printf("%s \"%s\" <%s>", user.Login, user.Name, user.Email) + if user.IsGrafanaAdmin { + fmt.Print(" admin") + } + fmt.Println() + } + if cmd.verbose { + fmt.Printf("Found %d users.\n", len(users)) + } + } +} diff --git a/cmd/grafana-backup/do-restore.go b/do-restore.go similarity index 100% rename from cmd/grafana-backup/do-restore.go rename to do-restore.go diff --git a/cmd/grafana-backup/main.go b/main.go similarity index 100% rename from cmd/grafana-backup/main.go rename to main.go