-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
112 lines (99 loc) · 2.8 KB
/
detect.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
109
110
111
112
package main
/*
Displays on terminal brief info about Grafana dashboards and datasources.
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/>.
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"github.com/grafov/autograf/grafana"
istty "github.com/mattn/go-isatty"
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
)
func main() {
var (
key string
db grafana.Board
ds grafana.Datasource
err error
isTerminal = istty.IsTerminal(os.Stdout.Fd())
)
flag.StringVar(&key, "key", "", "API key of Grafana server")
flag.Parse()
args := flag.Args()
if len(args) > 0 {
// Read from file(s).
} else {
// Read from stdin.
s := bufio.NewScanner(os.Stdin)
s.Split(scanJSONLines)
for s.Scan() {
if err = json.Unmarshal(s.Bytes(), &ds); err == nil {
if ds.Name != "" && ds.URL != "" {
outputDatasource(ds, isTerminal)
continue
}
}
if err = json.Unmarshal(s.Bytes(), &db); err == nil {
outputDashboard(db, isTerminal)
continue
}
fmt.Fprintln(os.Stderr, "unknown data")
}
}
}
func outputDatasource(ds grafana.Datasource, isTerminal bool) {
if isTerminal {
datasourceDisplay.Execute(os.Stdout, ds)
} else {
// kv
}
}
func outputDashboard(db grafana.Board, isTerminal bool) {
if isTerminal {
dashboardDisplay.Execute(os.Stdout, db)
} else {
// kv
}
}
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
}