-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check to detect duplicate endpoints in config.yaml
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters