From 8b7b0147a65f2a71abb4e23af2b77abfb6e63c45 Mon Sep 17 00:00:00 2001 From: chenk Date: Sun, 2 Jun 2024 16:21:55 +0300 Subject: [PATCH] feat: command id generator support Signed-off-by: chenk --- cmd/command__id/main.go | 65 +++++++++++++++++++++++++++++++++++++++++ embed.go | 4 +-- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 cmd/command__id/main.go diff --git a/cmd/command__id/main.go b/cmd/command__id/main.go new file mode 100644 index 00000000..80bd95bb --- /dev/null +++ b/cmd/command__id/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "path/filepath" + "strconv" + "strings" + + trivy_checks "github.com/aquasecurity/trivy-checks" + "gopkg.in/yaml.v2" +) + +const ( + commandPath = "commands/kubernetes" +) + +func main() { + ids, err := GetCommandIDRange() + if err != nil { + panic(err) + } + for i := 0; i < len(ids); i++ { + if !ids[i] { + println(fmt.Sprintf("%s-%04d", "CMD", i+1)) + } + } +} +func GetCommandIDRange() ([]bool, error) { + commandsIds := make([]bool, 9999) + entries, err := trivy_checks.EmbeddedK8sCommandsFileSystem.ReadDir(commandPath) + if err != nil { + panic(err) + } + for _, entry := range entries { + if entry.IsDir() { + continue + } + fContent, err := trivy_checks.EmbeddedK8sCommandsFileSystem.ReadFile(filepath.Join(commandPath, entry.Name())) + if err != nil { + return nil, err + } + var fileCommand any + err = yaml.Unmarshal(fContent, &fileCommand) + if err != nil { + panic(err) + } + + if commandArr, ok := fileCommand.([]interface{}); ok { + if commandMap, ok := commandArr[0].(map[any]any); ok { + if id, ok := commandMap["id"]; ok { + idStr := id.(string) + idWithPrefix := strings.TrimPrefix(idStr, "CMD-") + idNum, err := strconv.Atoi(idWithPrefix) + if err != nil { + return nil, err + } + if idNum > 0 && idNum <= 9999 { + commandsIds[idNum-1] = true + } + } + } + } + } + return commandsIds, nil +} diff --git a/embed.go b/embed.go index 05016737..b2fe69b9 100644 --- a/embed.go +++ b/embed.go @@ -10,8 +10,8 @@ var EmbeddedPolicyFileSystem embed.FS //go:embed lib/* var EmbeddedLibraryFileSystem embed.FS -//go:embed commands/kubernetes/* +//go:embed commands/kubernetes var EmbeddedK8sCommandsFileSystem embed.FS -//go:embed commands/config/* +//go:embed commands/config var EmbeddedConfigCommandsFileSystem embed.FS