From 7b6f4d5a5a01715c3dd7c05db2ce9f7ad70b23d3 Mon Sep 17 00:00:00 2001 From: Timo Riski Date: Thu, 19 Sep 2024 09:43:56 +0300 Subject: [PATCH] feat: add check to detect duplicate endpoints in config.yaml --- .trunk/check_duplicate_config.go | 65 ++++++++++++++++++++++++++++++++ .trunk/trunk.yaml | 11 ++++++ config.yaml | 1 - 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 .trunk/check_duplicate_config.go diff --git a/.trunk/check_duplicate_config.go b/.trunk/check_duplicate_config.go new file mode 100644 index 0000000..19919fc --- /dev/null +++ b/.trunk/check_duplicate_config.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "gopkg.in/yaml.v3" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println("missing filename") + os.Exit(1) + } + + filename := os.Args[1] + + // Check if the filename is config.yaml + if filepath.Base(filename) != "config.yaml" { + fmt.Println("skipping non-config.yaml file") + os.Exit(0) + } + + fileContents, err := os.ReadFile(filename) + if err != nil { + fmt.Printf("error reading file: %v\n", err) + os.Exit(1) + } + + var config map[string][]string + if err := yaml.Unmarshal(fileContents, &config); err != nil { + fmt.Printf("error unmarshalling YAML: %v\n", err) + os.Exit(1) + } + + endpoints := make(map[string]struct{}) + duplicates := make(map[string]struct{}) + + for _, methods := range config { + for _, method := range methods { + if _, exists := endpoints[method]; exists { + duplicates[method] = struct{}{} + } else { + endpoints[method] = struct{}{} + } + } + } + + if len(duplicates) > 0 { + fmt.Printf("Duplicate endpoints found: %v\n", keys(duplicates)) + os.Exit(1) + } else { + fmt.Println("No duplicate endpoints found.") + os.Exit(0) + } +} + +func keys(m map[string]struct{}) []string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + return keys +} diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 60f390b..130b443 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -17,7 +17,18 @@ runtimes: - python@3.10.8 # This is the section where you manage your linters. (https://docs.trunk.io/check/configuration) lint: + definitions: + - name: check_duplicate_config + description: Check for duplicate config keys + files: [yaml] + commands: + - name: lint + output: pass_fail + success_codes: [0, 1] + run: go run ${workspace}/.trunk/check_duplicate_config.go ${target} + read_output_from: stdout enabled: + - check_duplicate_config - actionlint@1.7.1 - checkov@3.2.253 - git-diff-check diff --git a/config.yaml b/config.yaml index a56a30e..220bf8f 100644 --- a/config.yaml +++ b/config.yaml @@ -292,7 +292,6 @@ Service: - ServiceTaskCreate - ServiceTaskGet - ServiceUpdate - - ServiceUpdate ServiceUser: - ServiceUserCreate - ServiceUserCredentialsModify