Skip to content

Commit

Permalink
feat: add check to detect duplicate endpoints in config.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
rriski committed Sep 19, 2024
1 parent 6c4af16 commit 7b6f4d5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
65 changes: 65 additions & 0 deletions .trunk/check_duplicate_config.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ runtimes:
- [email protected]
# 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
- [email protected]
- [email protected]
- git-diff-check
Expand Down
1 change: 0 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ Service:
- ServiceTaskCreate
- ServiceTaskGet
- ServiceUpdate
- ServiceUpdate
ServiceUser:
- ServiceUserCreate
- ServiceUserCredentialsModify
Expand Down

0 comments on commit 7b6f4d5

Please sign in to comment.