1
1
package cmd
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"io"
@@ -32,23 +33,24 @@ func init() {
32
33
}
33
34
34
35
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 )
36
37
37
38
// Check if the path exists
38
39
fileinfo , err := os .Stat (dbPath )
39
40
var zipPath string
40
41
if os .IsNotExist (err ) {
41
- log .Fatal (errors .New ("DB path does not exist" ))
42
+ log .Fatal (errors .New ("Database path does not exist" ))
42
43
}
43
44
if fileinfo .IsDir () {
44
- fmt .Printf ("Validating %s DB \n " , dbPath )
45
+ fmt .Printf ("Validating '%s' database \n " , dbPath )
45
46
err := utils .ValidateDB (dbPath )
46
47
if err != nil {
47
- fmt .Println ("DB is not valid" )
48
+ fmt .Println ("Database is not valid" )
49
+ return
48
50
}
49
51
// Compress DB
50
52
zipfilename := filepath .Join (os .TempDir (), "qldb.zip" )
51
- fmt .Println ("Compressing DB to" , zipfilename )
53
+ fmt .Println ("Compressing database" )
52
54
if err := utils .ZipDirectory (zipfilename , dbPath ); err != nil {
53
55
log .Fatal (err )
54
56
}
@@ -57,7 +59,7 @@ func install(nwo string, dbPath string, remove bool) {
57
59
} else {
58
60
// Check if the file is a zip
59
61
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" ))
61
63
}
62
64
63
65
zipPath = dbPath
@@ -78,10 +80,10 @@ func install(nwo string, dbPath string, remove bool) {
78
80
// if there is one directory in the tmpdir, use that as the tmpdir
79
81
tmpdir = filepath .Join (tmpdir , dirEntries [0 ].Name ())
80
82
}
81
- fmt .Printf ("Validating %s DB \n " , tmpdir )
83
+ fmt .Printf ("Validating '%s' database \n " , tmpdir )
82
84
err = utils .ValidateDB (tmpdir )
83
85
if err != nil {
84
- fmt .Println ("DB is not valid" )
86
+ fmt .Println ("Database is not valid" )
85
87
}
86
88
}
87
89
@@ -95,22 +97,35 @@ func install(nwo string, dbPath string, remove bool) {
95
97
if err != nil {
96
98
log .Fatal (err )
97
99
}
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 )
99
107
shortCommitSha := commitSha [:8 ]
108
+ primaryLanguage := metadata ["primaryLanguage" ].(string )
109
+ fmt .Println ()
100
110
fmt .Println ("Commit SHA:" , commitSha )
101
111
fmt .Println ("Short Commit SHA:" , shortCommitSha )
102
112
fmt .Println ("Primary language:" , primaryLanguage )
103
113
114
+ zipFilename := fmt .Sprintf ("%s-%s.zip" , primaryLanguage , shortCommitSha )
115
+ jsonFilename := fmt .Sprintf ("%s-%s.json" , primaryLanguage , shortCommitSha )
116
+ dir := utils .GetPath (nwoFlag )
117
+
104
118
// 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 + "'" )
108
123
109
124
// 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 ) {
111
126
112
127
// Create the directory if it doesn't exist
113
- err = os .MkdirAll (filepath .Dir (destPath ), 0755 )
128
+ err = os .MkdirAll (filepath .Dir (zipDestPath ), 0755 )
114
129
if err != nil {
115
130
log .Fatal (err )
116
131
return
@@ -124,7 +139,7 @@ func install(nwo string, dbPath string, remove bool) {
124
139
}
125
140
defer srcFile .Close ()
126
141
127
- destFile , err := os .Create (destPath )
142
+ destFile , err := os .Create (zipDestPath )
128
143
if err != nil {
129
144
log .Fatal (err )
130
145
return
@@ -142,11 +157,28 @@ func install(nwo string, dbPath string, remove bool) {
142
157
log .Fatal (err )
143
158
}
144
159
} 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" )
146
177
}
178
+
147
179
// Remove DB from the current location if -r flag is set
148
180
if remove {
149
- fmt .Println ("Removing DB from" , dbPath )
181
+ fmt .Println ("Removing database from '" + dbPath + "'" )
150
182
if err := os .RemoveAll (dbPath ); err != nil {
151
183
log .Fatal (err )
152
184
}
0 commit comments