1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 17:37:12 +00:00
kyverno/cmd/cli/kubectl-kyverno/jp/function/function.go
Charles-Edouard Brétéché b28c16fe99
refactor: split CLI jp command (#5566)
* refactor: split CLI jp command

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix sonatype comments

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
2022-12-21 06:06:13 +00:00

54 lines
1.3 KiB
Go

package function
import (
"fmt"
"strings"
"github.com/kyverno/kyverno/pkg/engine/jmespath"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/util/sets"
)
var description = []string{
"Provides function informations",
"For more information visit: https://kyverno.io/docs/writing-policies/jmespath/ ",
}
var examples = []string{
" # List functions \n kyverno jp function",
" # Get function infos\n kyverno jp function <function name>",
}
func Command() *cobra.Command {
return &cobra.Command{
Use: "function [function_name]...",
Short: strings.Join(description, "\n"),
Example: strings.Join(examples, "\n\n"),
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
printFunctions(args...)
},
}
}
func printFunctions(names ...string) {
functions := jmespath.GetFunctions()
slices.SortFunc(functions, func(a, b *jmespath.FunctionEntry) bool {
return a.String() < b.String()
})
namesSet := sets.NewString(names...)
for _, function := range functions {
if len(namesSet) == 0 || namesSet.Has(function.Entry.Name) {
function := *function
note := function.Note
function.Note = ""
fmt.Println("Name:", function.Entry.Name)
fmt.Println(" Signature:", function.String())
if note != "" {
fmt.Println(" Note: ", note)
}
fmt.Println()
}
}
}