-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (59 loc) · 1.55 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
package main
import (
"flag"
"fmt"
"net/url"
"os"
"path"
"strings"
"github.com/tigrannajaryan/telemetry-schema/schema"
"github.com/tigrannajaryan/telemetry-schema/schema/types"
)
func main() {
var inputFile string
flag.StringVar(&inputFile, "i", "", "schema file to check")
flag.Parse()
if inputFile == "" {
fmt.Print("Must specify a schema file to check.\n")
flag.PrintDefaults()
os.Exit(1)
}
ts, err := schema.Parse(inputFile)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
if ts.FileFormat != "1.0.0" {
fmt.Printf("file_format must be 1.0.0.\n")
os.Exit(1)
}
if ts == nil {
panic("Schema is empty")
}
schemaVerInFileName := path.Base(inputFile)
fmt.Printf("Schema version according to file name is %s.\n", schemaVerInFileName)
if _, ok := ts.Versions[types.TelemetryVersion(schemaVerInFileName)]; !ok {
fmt.Printf("Schema version %s is not found in the file.\n", schemaVerInFileName)
os.Exit(1)
}
if ts.SchemaURL == "" {
fmt.Printf("schema_url is missing.\n")
os.Exit(1)
}
surl, err := url.Parse(ts.SchemaURL)
if err != nil {
fmt.Printf("schema_url cannot be parsed: %v.\n", err)
os.Exit(1)
}
paths := strings.Split(surl.Path, "/")
if len(paths) == 0 {
fmt.Printf("schema_url path should not be empty.\n")
os.Exit(1)
}
schemaVerInPath := paths[len(paths)-1]
if schemaVerInPath != schemaVerInFileName {
fmt.Printf("The last part of schema_url path is %s but expected %s\n", schemaVerInPath, schemaVerInFileName)
os.Exit(5)
}
fmt.Printf("%s schema file checks are successful.\n", inputFile)
}