11package cmd
22
33import (
4+ "encoding/json"
45 "errors"
56 "fmt"
67 "io"
@@ -32,23 +33,24 @@ func init() {
3233}
3334
3435func 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 }
0 commit comments