Skip to content

Commit

Permalink
feat: command id generator support
Browse files Browse the repository at this point in the history
Signed-off-by: chenk <[email protected]>
  • Loading branch information
chen-keinan committed Jun 3, 2024
1 parent e480349 commit 8b7b014
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
65 changes: 65 additions & 0 deletions cmd/command__id/main.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8b7b014

Please sign in to comment.