/
file_listtags.go
94 lines (80 loc) · 2.93 KB
/
file_listtags.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
package cmd
import (
"fmt"
"log"
"strings"
"github.com/kong/go-apiops/filebasics"
"github.com/kong/go-apiops/logbasics"
"github.com/kong/go-apiops/tags"
"github.com/spf13/cobra"
)
const PlainOutputFormat = "PLAIN"
var (
cmdListTagsInputFilename string
cmdListTagsOutputFilename string
cmdListTagsOutputFormat string
cmdListTagsSelectors []string
)
// Executes the CLI command "list-tags"
func executeListTags(cmd *cobra.Command, _ []string) error {
verbosity, _ := cmd.Flags().GetInt("verbose")
logbasics.Initialize(log.LstdFlags, verbosity)
_ = sendAnalytics("file-list-tags", "", modeLocal)
cmdListTagsOutputFormat = strings.ToUpper(cmdListTagsOutputFormat)
// do the work: read/list-tags/write
data, err := filebasics.DeserializeFile(cmdListTagsInputFilename)
if err != nil {
return fmt.Errorf("failed to read input file '%s'; %w", cmdListTagsInputFilename, err)
}
tagger := tags.Tagger{}
tagger.SetData(data)
err = tagger.SetSelectors(cmdListTagsSelectors)
if err != nil {
return fmt.Errorf("failed to set selectors; %w", err)
}
list, err := tagger.ListTags()
if err != nil {
return fmt.Errorf("failed to list tags; %w", err)
}
if cmdListTagsOutputFormat == PlainOutputFormat {
// return as a plain text format, unix style; line separated
result := []byte(strings.Join(list, "\n"))
return filebasics.WriteFile(cmdListTagsOutputFilename, result)
}
// return as yaml/json, create an object containing only a tags-array
result := make(map[string]interface{})
result["tags"] = list
return filebasics.WriteSerializedFile(
cmdListTagsOutputFilename,
result,
filebasics.OutputFormat(cmdListTagsOutputFormat))
}
//
//
// Define the CLI data for the list-tags command
//
//
func newListTagsCmd() *cobra.Command {
ListTagsCmd := &cobra.Command{
Use: "list-tags [flags]",
Short: "List current tags from objects in a decK file",
Long: `List current tags from objects in a decK file.
The tags are collected from all objects that match the selector expressions. If no
selectors are given, all Kong entities will be scanned.`,
RunE: executeListTags,
Example: "# list all tags used on services\n" +
"cat kong.yml | deck file list-tags --selector='services[*]'",
Args: cobra.NoArgs,
}
ListTagsCmd.Flags().StringVarP(&cmdListTagsInputFilename, "state", "s", "-",
"decK file to process. Use - to read from stdin.")
ListTagsCmd.Flags().StringArrayVar(&cmdListTagsSelectors, "selector", []string{},
"JSON path expression to select objects to scan for tags.\n"+
"Defaults to all Kong entities. Repeat for multiple selectors.")
ListTagsCmd.Flags().StringVarP(&cmdListTagsOutputFilename, "output-file", "o", "-",
"Output file to write to. Use - to write to stdout.")
ListTagsCmd.Flags().StringVarP(&cmdListTagsOutputFormat, "format", "", PlainOutputFormat,
"Output format: "+string(filebasics.OutputFormatJSON)+", "+string(filebasics.OutputFormatYaml)+
", or "+string(PlainOutputFormat))
return ListTagsCmd
}