This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
main.go
93 lines (81 loc) · 1.87 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"github.com/grafana/metrictank/logger"
"github.com/grafana/metrictank/schema"
log "github.com/sirupsen/logrus"
)
var (
version string
showVersion = flag.Bool("version", false, "print version string")
)
func init() {
formatter := &logger.TextFormatter{}
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
log.SetFormatter(formatter)
log.SetLevel(log.InfoLevel)
}
func main() {
flag.Usage = func() {
fmt.Println("mt-keygen")
fmt.Println()
fmt.Println("mt-keygen gives you the MKey for a specific MetricDefinition")
fmt.Println("It fills a temp file with a template MetricDefinition")
fmt.Println("It launches vim")
fmt.Println("You fill in the important details - name / interval / tags /...")
fmt.Println("It prints the MKey")
fmt.Println()
flag.PrintDefaults()
}
flag.Parse()
if *showVersion {
fmt.Printf("mt-keygen (version: %s - runtime: %s)\n", version, runtime.Version())
return
}
d := schema.MetricDefinition{
Unit: "unknown",
Mtype: "gauge",
}
var data []byte
var err error
var f *os.File
if data, err = json.MarshalIndent(d, "", " "); err != nil {
log.Fatal(err)
}
if f, err = ioutil.TempFile("", "mt-gen-metricid"); err != nil {
log.Fatal(err)
}
fmt.Println("using file", f.Name())
defer os.Remove(f.Name())
if _, err = f.Write(data); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
cmd := exec.Command("vim", f.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Start(); err != nil {
log.Fatal(err)
}
if err = cmd.Wait(); err != nil {
log.Fatal(err)
}
data, err = ioutil.ReadFile(f.Name())
if err != nil {
log.Fatal(err)
}
if err = json.Unmarshal(data, &d); err != nil {
log.Fatal(err)
}
d.SetId()
fmt.Println(d.Id.String())
}