Skip to content

Commit 1d38b62

Browse files
author
Alvaro Muñoz
committed
Add companion json file for each database installed
1 parent 170b1d9 commit 1d38b62

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

cmd/download.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"io"
@@ -83,14 +84,24 @@ func download() {
8384
}
8485

8586
// get the commit this DB was created from
86-
commitSha, primaryLanguage, err := utils.ExtractDBInfo(body)
87+
metadata, err := utils.ExtractDBInfo(body)
8788
if err != nil {
8889
log.Fatal(err)
8990
}
91+
metadata["provenance"] = nwoFlag
92+
commitSha := metadata["creationMetadata"].(map[string]interface{})["sha"].(string)
93+
shortCommitSha := commitSha[:8]
94+
primaryLanguage := metadata["primaryLanguage"].(string)
95+
fmt.Println()
96+
fmt.Println("Commit SHA:", commitSha)
97+
fmt.Println("Short Commit SHA:", shortCommitSha)
98+
fmt.Println("Primary language:", primaryLanguage)
9099

91-
filename := fmt.Sprintf("%s.zip", commitSha)
92-
dir := filepath.Join(utils.GetPath(nwoFlag), primaryLanguage)
93-
path := filepath.Join(dir, filename)
100+
zipFilename := fmt.Sprintf("%s-%s.zip", primaryLanguage, shortCommitSha)
101+
jsonFilename := fmt.Sprintf("%s-%s.json", primaryLanguage, shortCommitSha)
102+
dir := utils.GetPath(nwoFlag)
103+
zipPath := filepath.Join(dir, zipFilename)
104+
jsonPath := filepath.Join(dir, jsonFilename)
94105

95106
// create directory if not exists
96107
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
@@ -100,19 +111,34 @@ func download() {
100111
}
101112
}
102113

103-
// create file if not exists
104-
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
114+
// create DB file if doesnot exists
115+
if _, err := os.Stat(zipPath); errors.Is(err, os.ErrNotExist) {
105116
// write the DB to disk
106-
err = os.WriteFile(path, body, 0755)
117+
err = os.WriteFile(zipPath, body, 0755)
107118
if err != nil {
108119
log.Fatal(err)
109120
}
110-
fmt.Printf("Writing DB to %s\n", path)
121+
fmt.Printf("Writing DB to %s\n", zipPath)
111122

112123
} else {
113-
fmt.Printf("Aborting, DB %s already exists\n", path)
124+
fmt.Printf("Aborting, DB %s already exists\n", zipPath)
114125
}
115126

127+
// create Metadata file if doesnot exists
128+
if _, err := os.Stat(jsonPath); errors.Is(err, os.ErrNotExist) {
129+
// Convert the map to JSON
130+
jsonData, err := json.Marshal(metadata)
131+
if err != nil {
132+
log.Fatal(err)
133+
}
134+
// Write the JSON data to a file
135+
err = os.WriteFile(jsonPath, jsonData, 0644)
136+
if err != nil {
137+
log.Fatal(err)
138+
}
139+
} else {
140+
fmt.Printf("Aborting, DB metadata %s already exists\n", jsonPath)
141+
}
116142
}
117143
fmt.Println("Done")
118144
}

cmd/install.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"io"
@@ -32,23 +33,24 @@ func init() {
3233
}
3334

3435
func install(nwo string, dbPath string, remove bool) {
35-
fmt.Printf("Installing '%s' DB for '%s'\n", dbPath, nwo)
36+
fmt.Printf("Installing '%s' database for '%s'\n", dbPath, nwo)
3637

3738
// Check if the path exists
3839
fileinfo, err := os.Stat(dbPath)
3940
var zipPath string
4041
if os.IsNotExist(err) {
41-
log.Fatal(errors.New("DB path does not exist"))
42+
log.Fatal(errors.New("Database path does not exist"))
4243
}
4344
if fileinfo.IsDir() {
44-
fmt.Printf("Validating %s DB\n", dbPath)
45+
fmt.Printf("Validating '%s' database\n", dbPath)
4546
err := utils.ValidateDB(dbPath)
4647
if err != nil {
47-
fmt.Println("DB is not valid")
48+
fmt.Println("Database is not valid")
49+
return
4850
}
4951
// Compress DB
5052
zipfilename := filepath.Join(os.TempDir(), "qldb.zip")
51-
fmt.Println("Compressing DB to", zipfilename)
53+
fmt.Println("Compressing database")
5254
if err := utils.ZipDirectory(zipfilename, dbPath); err != nil {
5355
log.Fatal(err)
5456
}
@@ -57,7 +59,7 @@ func install(nwo string, dbPath string, remove bool) {
5759
} else {
5860
// Check if the file is a zip
5961
if !strings.HasSuffix(dbPath, ".zip") {
60-
log.Fatal(errors.New("DB path is not a zip file"))
62+
log.Fatal(errors.New("Database is not a zip file"))
6163
}
6264

6365
zipPath = dbPath
@@ -78,10 +80,10 @@ func install(nwo string, dbPath string, remove bool) {
7880
// if there is one directory in the tmpdir, use that as the tmpdir
7981
tmpdir = filepath.Join(tmpdir, dirEntries[0].Name())
8082
}
81-
fmt.Printf("Validating %s DB\n", tmpdir)
83+
fmt.Printf("Validating '%s' database\n", tmpdir)
8284
err = utils.ValidateDB(tmpdir)
8385
if err != nil {
84-
fmt.Println("DB is not valid")
86+
fmt.Println("Database is not valid")
8587
}
8688
}
8789

@@ -95,22 +97,35 @@ func install(nwo string, dbPath string, remove bool) {
9597
if err != nil {
9698
log.Fatal(err)
9799
}
98-
commitSha, primaryLanguage, err := utils.ExtractDBInfo(zipBytes)
100+
101+
metadata, err := utils.ExtractDBInfo(zipBytes)
102+
if err != nil {
103+
log.Fatal(err)
104+
}
105+
metadata["provenance"] = nwoFlag
106+
commitSha := metadata["creationMetadata"].(map[string]interface{})["sha"].(string)
99107
shortCommitSha := commitSha[:8]
108+
primaryLanguage := metadata["primaryLanguage"].(string)
109+
fmt.Println()
100110
fmt.Println("Commit SHA:", commitSha)
101111
fmt.Println("Short Commit SHA:", shortCommitSha)
102112
fmt.Println("Primary language:", primaryLanguage)
103113

114+
zipFilename := fmt.Sprintf("%s-%s.zip", primaryLanguage, shortCommitSha)
115+
jsonFilename := fmt.Sprintf("%s-%s.json", primaryLanguage, shortCommitSha)
116+
dir := utils.GetPath(nwoFlag)
117+
104118
// Destination path
105-
filename := fmt.Sprintf("%s-%s.zip", primaryLanguage, shortCommitSha)
106-
destPath := filepath.Join(utils.GetPath(nwo), filename)
107-
fmt.Println("Installing DB to", destPath)
119+
zipDestPath := filepath.Join(dir, zipFilename)
120+
jsonDestPath := filepath.Join(dir, jsonFilename)
121+
122+
fmt.Println("Installing database to '" + zipDestPath + "'")
108123

109124
// Check if the DB is already installed
110-
if _, err := os.Stat(destPath); errors.Is(err, os.ErrNotExist) {
125+
if _, err := os.Stat(zipDestPath); errors.Is(err, os.ErrNotExist) {
111126

112127
// Create the directory if it doesn't exist
113-
err = os.MkdirAll(filepath.Dir(destPath), 0755)
128+
err = os.MkdirAll(filepath.Dir(zipDestPath), 0755)
114129
if err != nil {
115130
log.Fatal(err)
116131
return
@@ -124,7 +139,7 @@ func install(nwo string, dbPath string, remove bool) {
124139
}
125140
defer srcFile.Close()
126141

127-
destFile, err := os.Create(destPath)
142+
destFile, err := os.Create(zipDestPath)
128143
if err != nil {
129144
log.Fatal(err)
130145
return
@@ -142,11 +157,28 @@ func install(nwo string, dbPath string, remove bool) {
142157
log.Fatal(err)
143158
}
144159
} else {
145-
fmt.Println("DB already installed for same commit")
160+
fmt.Println("Database already installed for same commit")
161+
}
162+
163+
if _, err := os.Stat(jsonDestPath); errors.Is(err, os.ErrNotExist) {
164+
// Convert the map to JSON
165+
jsonData, err := json.Marshal(metadata)
166+
if err != nil {
167+
log.Fatal(err)
168+
}
169+
170+
// Write the JSON data to a file
171+
err = os.WriteFile(jsonDestPath, jsonData, 0644)
172+
if err != nil {
173+
log.Fatal(err)
174+
}
175+
} else {
176+
fmt.Println("Database metadata already exists for same commit")
146177
}
178+
147179
// Remove DB from the current location if -r flag is set
148180
if remove {
149-
fmt.Println("Removing DB from", dbPath)
181+
fmt.Println("Removing database from '" + dbPath + "'")
150182
if err := os.RemoveAll(dbPath); err != nil {
151183
log.Fatal(err)
152184
}

utils/utils.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ func ValidateDB(dbPath string) error {
4848
return nil
4949
}
5050

51-
func ExtractDBInfo(body []byte) (string, string, error) {
51+
func ExtractDBInfo(body []byte) (map[string]interface{}, error) {
5252
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
5353
if err != nil {
5454
log.Fatal(err)
5555
}
56-
fmt.Print("Extracting commit version from DB ... ")
56+
fmt.Print("Extracting database information ... ")
5757
for _, zf := range zipReader.File {
5858
if strings.HasSuffix(zf.Name, "codeql-database.yml") {
5959
f, err := zf.Open()
@@ -70,14 +70,10 @@ func ExtractDBInfo(body []byte) (string, string, error) {
7070
if err != nil {
7171
log.Fatal(err)
7272
}
73-
commitSha := dbData["creationMetadata"].(map[string]interface{})["sha"].(string)
74-
fmt.Printf("%s\n", commitSha)
75-
// extractionDate := dbData["creationMetadata"].(map[string]interface{})["creationTime"].(string)
76-
primaryLanguage := dbData["primaryLanguage"].(string)
77-
return commitSha, primaryLanguage, nil
73+
return dbData, nil
7874
}
7975
}
80-
return "", "", errors.New("codeql-database.yml not found")
76+
return nil, errors.New("codeql-database.yml not found")
8177
}
8278

8379
// Unzip will decompress a zip archive, moving all files and folders

0 commit comments

Comments
 (0)