forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (131 loc) · 3.3 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
// Description: Program to check and summarise the Kata GitHub
// labels YAML file.
package main
import (
"errors"
"fmt"
"os"
"github.com/urfave/cli"
)
type DataToShow int
const (
showLabels DataToShow = iota
showCategories DataToShow = iota
textFormat = "text"
defaultOutputFormat = textFormat
)
var errNeedYAMLFile = errors.New("need YAML file")
var (
// set by the build
name = ""
version = ""
commit = ""
debug = false
)
var formatFlag = cli.StringFlag{
Name: "format",
Usage: "display in specified format ('help' to show all)",
Value: defaultOutputFormat,
}
func commonHandler(context *cli.Context, what DataToShow, withLabels bool) error {
handlers := NewDisplayHandlers()
format := context.String("format")
if format == "help" {
availableFormats := handlers.Get()
for _, format := range availableFormats {
fmt.Fprintf(outputFile, "%s\n", format)
}
return nil
}
handler := handlers.find(format)
if handler == nil {
return fmt.Errorf("no handler for format %q", format)
}
if context.NArg() == 0 {
return errNeedYAMLFile
}
file := context.Args().Get(0)
return show(file, handler, what, withLabels)
}
func main() {
app := cli.NewApp()
app.Description = "tool to manipulate Kata GitHub labels"
app.Usage = app.Description
app.Version = fmt.Sprintf("%s %s (commit %v)", name, version, commit)
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "enable debug output",
Destination: &debug,
},
}
app.Commands = []cli.Command{
{
Name: "check",
Usage: "Perform tests on the labels database",
Description: "Exit code denotes success",
Action: func(context *cli.Context) error {
if context.NArg() == 0 {
return errNeedYAMLFile
}
file := context.Args().Get(0)
return checkYAML(file)
},
},
{
Name: "show",
Usage: "Display labels database details",
Subcommands: []cli.Command{
{
Name: "categories",
Usage: "Display categories from labels database",
Flags: []cli.Flag{
formatFlag,
cli.BoolFlag{
Name: "with-labels",
Usage: "Add labels in each category to output",
},
},
Action: func(context *cli.Context) error {
withLabels := context.Bool("with-labels")
return commonHandler(context, showCategories, withLabels)
},
},
{
Name: "labels",
Usage: "Display labels from labels database",
Flags: []cli.Flag{
formatFlag,
},
Action: func(context *cli.Context) error {
withLabels := context.Bool("with-labels")
return commonHandler(context, showLabels, withLabels)
},
},
},
},
{
Name: "sort",
Usage: "Sort the specified YAML labels file and write to a new file",
Description: "Can be used to keep the master labels file sorted",
ArgsUsage: "<input-file> <output-file>",
Action: func(context *cli.Context) error {
if context.NArg() != 2 {
return errors.New("need two YAML files: <input-file> <output-file>")
}
from := context.Args().Get(0)
to := context.Args().Get(1)
return sortYAML(from, to)
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}