-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
900 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// | ||
// | ||
// Define the CLI data for the file sub-command | ||
// | ||
// | ||
|
||
func newAddFileCmd() *cobra.Command { | ||
addFileCmd := &cobra.Command{ | ||
Use: "file [sub-command]...", | ||
Short: "Sub-command to host the decK file manipulation operations", | ||
Long: `Sub-command to host the decK file manipulation operations`, | ||
} | ||
|
||
return addFileCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/kong/go-apiops/deckformat" | ||
"github.com/kong/go-apiops/filebasics" | ||
"github.com/kong/go-apiops/jsonbasics" | ||
"github.com/kong/go-apiops/logbasics" | ||
"github.com/kong/go-apiops/plugins" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
cmdAddPluginsOverwrite bool | ||
cmdAddPluginsInputFilename string | ||
cmdAddPluginOutputFilename string | ||
cmdAddPluginOutputFormat string | ||
cmdAddPluginsSelectors []string | ||
cmdAddPluginsStrConfigs []string | ||
) | ||
|
||
// Executes the CLI command "add-plugins" | ||
func executeAddPlugins(cmd *cobra.Command, cfgFiles []string) error { | ||
verbosity, _ := cmd.Flags().GetInt("verbose") | ||
logbasics.Initialize(log.LstdFlags, verbosity) | ||
|
||
cmdAddPluginOutputFormat = strings.ToUpper(cmdAddPluginOutputFormat) | ||
|
||
var pluginConfigs []map[string]interface{} | ||
{ | ||
for _, strConfig := range cmdAddPluginsStrConfigs { | ||
pluginConfig, err := filebasics.Deserialize([]byte(strConfig)) | ||
if err != nil { | ||
return fmt.Errorf("failed to deserialize plugin config '%s'; %w", strConfig, err) | ||
} | ||
pluginConfigs = append(pluginConfigs, pluginConfig) | ||
} | ||
} | ||
|
||
var pluginFiles []plugins.DeckPluginFile | ||
{ | ||
for _, filename := range cfgFiles { | ||
var file plugins.DeckPluginFile | ||
if err := file.ParseFile(filename); err != nil { | ||
return fmt.Errorf("failed to parse plugin file '%s'; %w", filename, err) | ||
} | ||
pluginFiles = append(pluginFiles, file) | ||
} | ||
} | ||
|
||
// do the work: read/add-plugins/write | ||
jsondata, err := filebasics.DeserializeFile(cmdAddPluginsInputFilename) | ||
if err != nil { | ||
return fmt.Errorf("failed to read input file '%s'; %w", cmdAddPluginsInputFilename, err) | ||
} | ||
yamlNode := jsonbasics.ConvertToYamlNode(jsondata) | ||
|
||
// apply CLI flags | ||
plugger := plugins.Plugger{} | ||
plugger.SetYamlData(yamlNode) | ||
err = plugger.SetSelectors(cmdAddPluginsSelectors) | ||
if err != nil { | ||
return fmt.Errorf("failed to set selectors; %w", err) | ||
} | ||
err = plugger.AddPlugins(pluginConfigs, cmdAddPluginsOverwrite) | ||
if err != nil { | ||
return fmt.Errorf("failed to add plugins; %w", err) | ||
} | ||
yamlNode = plugger.GetYamlData() | ||
|
||
// apply plugin-files | ||
for i, pluginFile := range pluginFiles { | ||
err = pluginFile.Apply(yamlNode) | ||
if err != nil { | ||
return fmt.Errorf("failed to apply plugin file '%s'; %w", cfgFiles[i], err) | ||
} | ||
} | ||
jsondata = plugger.GetData() | ||
|
||
trackInfo := deckformat.HistoryNewEntry("add-plugins") | ||
trackInfo["input"] = cmdAddPluginsInputFilename | ||
trackInfo["output"] = cmdAddPluginOutputFilename | ||
trackInfo["overwrite"] = cmdAddPluginsOverwrite | ||
if len(pluginConfigs) > 0 { | ||
trackInfo["configs"] = pluginConfigs | ||
} | ||
if len(cfgFiles) > 0 { | ||
trackInfo["pluginfiles"] = cfgFiles | ||
} | ||
trackInfo["selectors"] = cmdAddPluginsSelectors | ||
deckformat.HistoryAppend(jsondata, trackInfo) | ||
|
||
return filebasics.WriteSerializedFile(cmdAddPluginOutputFilename, jsondata, cmdAddPluginOutputFormat) | ||
} | ||
|
||
// | ||
// | ||
// Define the CLI data for the add-plugins command | ||
// | ||
// | ||
|
||
func newAddPluginsCmd() *cobra.Command { | ||
addPluginsCmd := &cobra.Command{ | ||
Use: "add-plugins [flags] [...plugin-files]", | ||
Short: "Add plugins to objects in a decK file", | ||
Long: `Add plugins to objects in a decK file. | ||
The plugins are added to all objects that match the selector expressions. If no | ||
selectors are given, they will be added to the top-level 'plugins' array. | ||
The plugin-files have the following format (JSON or YAML) and are applied in the | ||
order they are given; | ||
{ "_format_version": "1.0", | ||
"add-plugins": [ | ||
{ "selectors": [ | ||
"$..services[*]" | ||
], | ||
"overwrite": false, | ||
"plugins": [ | ||
{ "name": "my-plugin", | ||
"config": { | ||
"my-property": "value" | ||
} | ||
} | ||
], | ||
} | ||
] | ||
} | ||
`, | ||
RunE: executeAddPlugins, | ||
Args: cobra.MinimumNArgs(0), | ||
} | ||
|
||
addPluginsCmd.Flags().StringVarP(&cmdAddPluginsInputFilename, "state", "s", "-", | ||
"decK file to process. Use - to read from stdin") | ||
addPluginsCmd.Flags().StringArrayVar(&cmdAddPluginsSelectors, "selector", []string{}, | ||
"JSON path expression to select plugin-owning objects to add plugins to,\n"+ | ||
"defaults to the top-level (selector '$'). Repeat for multiple selectors.") | ||
addPluginsCmd.Flags().StringArrayVar(&cmdAddPluginsStrConfigs, "config", []string{}, | ||
"JSON snippet containing the plugin configuration to add. Repeat to add\n"+ | ||
"multiple plugins.") | ||
addPluginsCmd.Flags().BoolVar(&cmdAddPluginsOverwrite, "overwrite", false, | ||
"specifying this flag will overwrite plugins by the same name if they already\n"+ | ||
"exist in an array. The default is to skip existing plugins.") | ||
addPluginsCmd.Flags().StringVarP(&cmdAddPluginOutputFilename, "output-file", "o", "-", | ||
"output file to write. Use - to write to stdout") | ||
addPluginsCmd.Flags().StringVarP(&cmdAddPluginOutputFormat, "format", "", filebasics.OutputFormatYaml, | ||
"output format: "+filebasics.OutputFormatJSON+" or "+filebasics.OutputFormatYaml) | ||
|
||
return addPluginsCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/kong/go-apiops/deckformat" | ||
"github.com/kong/go-apiops/filebasics" | ||
"github.com/kong/go-apiops/logbasics" | ||
"github.com/kong/go-apiops/tags" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
cmdAddTagsInputFilename string | ||
cmdAddTagsOutputFilename string | ||
cmdAddTagsOutputFormat string | ||
cmdAddTagsSelectors []string | ||
) | ||
|
||
// Executes the CLI command "add-tags" | ||
func executeAddTags(cmd *cobra.Command, tagsToAdd []string) error { | ||
verbosity, _ := cmd.Flags().GetInt("verbose") | ||
logbasics.Initialize(log.LstdFlags, verbosity) | ||
|
||
cmdAddTagsOutputFormat = strings.ToUpper(cmdAddTagsOutputFormat) | ||
|
||
// do the work: read/add-tags/write | ||
data, err := filebasics.DeserializeFile(cmdAddTagsInputFilename) | ||
if err != nil { | ||
return fmt.Errorf("failed to read input file '%s'; %w", cmdAddTagsInputFilename, err) | ||
} | ||
|
||
tagger := tags.Tagger{} | ||
tagger.SetData(data) | ||
err = tagger.SetSelectors(cmdAddTagsSelectors) | ||
if err != nil { | ||
return fmt.Errorf("failed to set selectors; %w", err) | ||
} | ||
err = tagger.AddTags(tagsToAdd) | ||
if err != nil { | ||
return fmt.Errorf("failed to add tags; %w", err) | ||
} | ||
data = tagger.GetData() | ||
|
||
trackInfo := deckformat.HistoryNewEntry("add-tags") | ||
trackInfo["input"] = cmdAddTagsInputFilename | ||
trackInfo["output"] = cmdAddTagsOutputFilename | ||
trackInfo["tags"] = tagsToAdd | ||
trackInfo["selectors"] = cmdAddTagsSelectors | ||
deckformat.HistoryAppend(data, trackInfo) | ||
|
||
return filebasics.WriteSerializedFile(cmdAddTagsOutputFilename, data, cmdAddTagsOutputFormat) | ||
} | ||
|
||
// | ||
// | ||
// Define the CLI data for the add-tags command | ||
// | ||
// | ||
|
||
func newAddTagsCmd() *cobra.Command { | ||
addTagsCmd := &cobra.Command{ | ||
Use: "add-tags [flags] tag [...tag]", | ||
Short: "Add tags to objects in a decK file", | ||
Long: `Add tags to objects in a decK file. | ||
The tags are added to all objects that match the selector expressions. If no | ||
selectors are given, all Kong entities are tagged.`, | ||
RunE: executeAddTags, | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
|
||
addTagsCmd.Flags().StringVarP(&cmdAddTagsInputFilename, "state", "s", "-", | ||
"decK file to process. Use - to read from stdin") | ||
addTagsCmd.Flags().StringArrayVar(&cmdAddTagsSelectors, "selector", []string{}, | ||
"JSON path expression to select objects to add tags to,\n"+ | ||
"defaults to all Kong entities (repeat for multiple selectors)") | ||
addTagsCmd.Flags().StringVarP(&cmdAddTagsOutputFilename, "output-file", "o", "-", | ||
"output file to write. Use - to write to stdout") | ||
addTagsCmd.Flags().StringVarP(&cmdAddTagsOutputFormat, "format", "", filebasics.OutputFormatYaml, | ||
"output format: "+filebasics.OutputFormatJSON+" or "+filebasics.OutputFormatYaml) | ||
|
||
return addTagsCmd | ||
} |
Oops, something went wrong.