Skip to content

Commit f0095ba

Browse files
author
Alvaro Muñoz
committed
Add list command to retrieve CodeQL databases
This commit adds a new command "list" to retrieve a list of CodeQL databases stored in the QLDB structure. The command takes optional flags for filtering the results based on repository and language. The command also supports outputting the results in JSON format.
1 parent 1d8054a commit f0095ba

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

cmd/list.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/GitHubSecurityLab/gh-qldb/utils"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var listCmd = &cobra.Command{
16+
Use: "list",
17+
Short: "Returns a list of CodeQL databases stored in the QLDB structure",
18+
Long: `Returns a list of CodeQL databases stored in the QLDB structure`,
19+
Run: func(cmd *cobra.Command, args []string) {
20+
list()
21+
},
22+
}
23+
24+
func init() {
25+
rootCmd.AddCommand(listCmd)
26+
listCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to get the databases for.")
27+
listCmd.Flags().StringVarP(&languageFlag, "language", "l", "", "The primary language you want the databases for.")
28+
listCmd.Flags().BoolVarP(&jsonFlag, "json", "j", false, "Use json as the output format.")
29+
}
30+
31+
func list() {
32+
var results []string
33+
basePath := utils.GetBasePath()
34+
dirEntries, err := os.ReadDir(basePath)
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
for _, dirEntry := range dirEntries {
39+
if dirEntry.IsDir() {
40+
user := dirEntry.Name()
41+
userPath := filepath.Join(basePath, user)
42+
repoEntries, err := os.ReadDir(userPath)
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
for _, repoEntry := range repoEntries {
47+
if repoEntry.IsDir() {
48+
repo := repoEntry.Name()
49+
nwoPath := filepath.Join(userPath, repo)
50+
dbEntries, err := os.ReadDir(nwoPath)
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
for _, dbEntry := range dbEntries {
55+
if dbEntry.IsDir() || filepath.Ext(dbEntry.Name()) == ".zip" {
56+
dbPath := filepath.Join(nwoPath, dbEntry.Name())
57+
results = append(results, dbPath)
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
// if languageFlag is set, filter the results
66+
// so that the entries in results only contains elements which filename starts with languageFlag-
67+
if languageFlag != "" {
68+
var filteredResults []string
69+
for _, result := range results {
70+
if strings.HasPrefix(filepath.Base(result), languageFlag+"-") {
71+
filteredResults = append(filteredResults, result)
72+
}
73+
}
74+
results = filteredResults
75+
}
76+
77+
// if nwoFlag is set, filter the results so that the results only contains elements which contains nwoFlag
78+
if nwoFlag != "" {
79+
var filteredResults []string
80+
for _, result := range results {
81+
if strings.Contains(strings.ToLower(result), strings.ToLower(nwoFlag)) {
82+
filteredResults = append(filteredResults, result)
83+
}
84+
}
85+
results = filteredResults
86+
}
87+
88+
// if jsonFlag is set, print the results as json
89+
if jsonFlag {
90+
jsonBytes, err := json.MarshalIndent(results, "", " ")
91+
if err != nil {
92+
log.Fatal(err)
93+
}
94+
fmt.Println(string(jsonBytes))
95+
} else {
96+
for _, result := range results {
97+
fmt.Println(result)
98+
}
99+
}
100+
101+
}

0 commit comments

Comments
 (0)