kata-containers/tests/cmd/github-labels/main.go
Chelsea Mafrica 66f3944b52 tests: move github-labels to main repo
Move tool as part of static checks migration.

Fixes #8187

Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
Signed-off-by: Derek Lee <derlee@redhat.com>
Signed-off-by: Gabriela Cervantes <gabriela.cervantes.tellez@intel.com>
Signed-off-by: Graham Whaley <graham.whaley@intel.com>
Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
Signed-off-by: Marco Vedovati <mvedovati@suse.com>
Signed-off-by: Peng Tao <bergwolf@hyper.sh>
Signed-off-by: Shiming Zhang <wzshiming@foxmail.com>
Signed-off-by: Snir Sheriber <ssheribe@redhat.com>
Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
2023-11-28 11:13:55 -08:00

158 lines
3.3 KiB
Go

// 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)
}
}