forked from grafana-tools/grafana-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-file-list.go
108 lines (102 loc) · 2.86 KB
/
do-file-list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Backup tool for Grafana.
// Copyright (C) 2016 Alexander I.Grafov <[email protected]>
//
// 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 <http://www.gnu.org/licenses/>.
//
// ॐ तारे तुत्तारे तुरे स्व
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/grafana-tools/sdk"
)
// TODO adapt for apply-for
func doFileList(opts ...option) {
var (
cmd = initCommand(opts...)
file *os.File
db sdk.Board
ds sdk.Datasource
err error
)
for _, filename := range cmd.filenames {
select {
case <-cancel:
fmt.Fprintf(os.Stderr, "Execution was cancelled.")
goto Exit
default:
if strings.HasSuffix(filename, ".json") {
if file, err = os.Open(filename); err != nil {
if cmd.verbose {
fmt.Fprintf(os.Stderr, "file read error %s\n", err)
}
continue
}
s := bufio.NewScanner(file)
s.Split(scanJSONLines)
for s.Scan() {
if err = json.Unmarshal(s.Bytes(), &ds); err == nil {
if ds.Name != "" && ds.URL != "" {
fmt.Printf("%s:\t source id:%d \"%s\" %s\n", filename, ds.ID, ds.Name, ds.URL)
continue
}
}
if err = json.Unmarshal(s.Bytes(), &db); err == nil {
fmt.Printf("%s:\t board id:%d \"%s\"", filename, db.ID, db.Title)
if len(db.Tags) > 0 {
fmt.Printf(" %v", db.Tags)
}
fmt.Println()
}
}
}
// if cmd.verbose {
// fmt.Printf("Found %d dashboards.\n", len(foundBoards))
// }
}
}
Exit:
fmt.Println()
}
func scanJSONLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte("}{")); i >= 0 {
// We have probably a full JSON object followed by another object
// that came obviously from another file.
return i + 1, data[0 : i+1], nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, dropCR(data[0:i]), nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), dropCR(data), nil
}
// Request more data.
return 0, nil, nil
}
// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}