/
kubeconform.go
119 lines (100 loc) · 2.88 KB
/
kubeconform.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
113
114
115
116
117
118
119
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"os"
)
// homegrown command structures now but if this grows we may
// want to adopt whatever kubectl uses
type options struct {
// Flags only used for generating behaviors
schemaPath string
resource string
area string
// Flags only used for linking behaviors
testdata string
listAll bool
// Flags shared between CLI tools
behaviorsDir string
}
type actionFunc func(*options) error
func parseFlags() (actionFunc, *options) {
o := &options{}
f := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
f.StringVar(&o.schemaPath, "schema", "", "Path to the OpenAPI schema")
f.StringVar(&o.resource, "resource", "", "Resource name")
f.StringVar(&o.area, "area", "", "Area name to use")
f.StringVar(&o.testdata, "testdata", "test/conformance/testdata/conformance.yaml", "YAML file containing test linkage data")
f.BoolVar(&o.listAll, "all", false, "List all behaviors, not just those missing tests")
f.StringVar(&o.behaviorsDir, "dir", "test/conformance/behaviors/", "Path to the behaviors directory")
f.Usage = func() {
fmt.Fprintf(os.Stderr,
"USAGE\n-----\n%s [ options ] { link | gen }\n",
os.Args[0])
fmt.Fprintf(os.Stderr, "\nOPTIONS\n-------\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nACTIONS\n------------")
fmt.Fprintf(os.Stderr, `
'link' lists behaviors associated with tests
'gen' generates behaviors based on the API schema
`)
}
flag.CommandLine = f
flag.Parse()
if len(flag.Args()) != 1 {
flag.CommandLine.Usage()
os.Exit(2)
}
var action actionFunc
switch flag.Args()[0] {
case "gen":
action = gen
if o.schemaPath == "" {
action = nil
fmt.Fprintf(os.Stderr, "-schema is required for 'gen'\n")
}
if o.resource == "" {
action = nil
fmt.Fprintf(os.Stderr, "-resource is required for 'gen'\n")
}
if o.area == "" {
action = nil
fmt.Fprintf(os.Stderr, "-area is required for 'gen'\n")
}
case "link":
action = link
if o.testdata == "" {
action = nil
fmt.Fprintf(os.Stderr, "-testdata is required for 'link'\n")
}
}
if o.behaviorsDir == "" {
action = nil
fmt.Fprintf(os.Stderr, "-dir is required\n")
}
if action == nil {
flag.CommandLine.Usage()
os.Exit(2)
}
return action, o
}
func main() {
action, o := parseFlags()
err := action(o)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}