Skip to content

Commit 4c996c3

Browse files
committed
Sort tools/runtimes in codacy yaml
1 parent b5dd1b1 commit 4c996c3

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

cmd/init.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"net/http"
1616
"os"
1717
"path/filepath"
18+
"slices"
1819
"strings"
1920
"time"
2021

@@ -180,26 +181,42 @@ func configFileTemplate(tools []tools.Tool) string {
180181
var sb strings.Builder
181182
sb.WriteString("runtimes:\n")
182183

184+
// Create a slice to store runtimes for sorting
185+
var runtimes []string
186+
183187
// Only include runtimes needed by the enabled tools
184188
if len(tools) > 0 {
185189
if needsNode {
186-
sb.WriteString(" - node@22.2.0\n")
190+
runtimes = append(runtimes, "node@22.2.0")
187191
}
188192
if needsPython {
189-
sb.WriteString(" - python@3.11.11\n")
193+
runtimes = append(runtimes, "python@3.11.11")
190194
}
191195
if needsDart {
192-
sb.WriteString(" - dart@3.7.2\n")
196+
runtimes = append(runtimes, "dart@3.7.2")
193197
}
194198
} else {
195199
// In local mode with no tools specified, include all runtimes
196-
sb.WriteString(" - node@22.2.0\n")
197-
sb.WriteString(" - python@3.11.11\n")
198-
sb.WriteString(" - dart@3.7.2\n")
200+
runtimes = []string{
201+
"node@22.2.0",
202+
"python@3.11.11",
203+
"dart@3.7.2",
204+
}
205+
}
206+
207+
// Sort runtimes alphabetically
208+
slices.Sort(runtimes)
209+
210+
// Write sorted runtimes
211+
for _, runtime := range runtimes {
212+
sb.WriteString(fmt.Sprintf(" - %s\n", runtime))
199213
}
200214

201215
sb.WriteString("tools:\n")
202216

217+
// Create a slice to store tools for sorting
218+
var toolEntries []string
219+
203220
// If we have tools from the API (enabled tools), use only those
204221
if len(tools) > 0 {
205222
// Add only the tools that are in the API response (enabled tools)
@@ -215,18 +232,30 @@ func configFileTemplate(tools []tools.Tool) string {
215232

216233
for uuid, name := range uuidToName {
217234
if toolsMap[uuid] {
218-
sb.WriteString(fmt.Sprintf(" - %s@%s\n", name, toolVersions[uuid]))
235+
toolEntries = append(toolEntries, fmt.Sprintf("%s@%s", name, toolVersions[uuid]))
219236
}
220237
}
221238
} else {
222239
// If no tools were specified (local mode), include all defaults
223-
sb.WriteString(fmt.Sprintf(" - eslint@%s\n", defaultVersions[ESLint]))
224-
sb.WriteString(fmt.Sprintf(" - trivy@%s\n", defaultVersions[Trivy]))
225-
sb.WriteString(fmt.Sprintf(" - pylint@%s\n", defaultVersions[PyLint]))
226-
sb.WriteString(fmt.Sprintf(" - pmd@%s\n", defaultVersions[PMD]))
227-
sb.WriteString(fmt.Sprintf(" - dartanalyzer@%s\n", defaultVersions[DartAnalyzer]))
228-
sb.WriteString(fmt.Sprintf(" - semgrep@%s\n", defaultVersions[Semgrep]))
229-
sb.WriteString(fmt.Sprintf(" - lizard@%s\n", defaultVersions[Lizard]))
240+
for uuid, name := range map[string]string{
241+
ESLint: "eslint",
242+
Trivy: "trivy",
243+
PyLint: "pylint",
244+
PMD: "pmd",
245+
DartAnalyzer: "dartanalyzer",
246+
Semgrep: "semgrep",
247+
Lizard: "lizard",
248+
} {
249+
toolEntries = append(toolEntries, fmt.Sprintf("%s@%s", name, defaultVersions[uuid]))
250+
}
251+
}
252+
253+
// Sort tools alphabetically
254+
slices.Sort(toolEntries)
255+
256+
// Write sorted tools
257+
for _, tool := range toolEntries {
258+
sb.WriteString(fmt.Sprintf(" - %s\n", tool))
230259
}
231260

232261
return sb.String()

0 commit comments

Comments
 (0)