1
1
package cmd
2
2
3
3
import (
4
- "io"
5
4
"errors"
6
5
"fmt"
7
- "io/ioutil "
6
+ "io"
8
7
"log"
9
8
"os"
10
9
"path/filepath"
11
10
"strings"
12
11
13
- "github.com/spf13/cobra "
14
- "github.com/GitHubSecurityLab/gh-qldb/utils "
12
+ "github.com/GitHubSecurityLab/gh-qldb/utils "
13
+ "github.com/spf13/cobra "
15
14
)
16
15
17
16
var installCmd = & cobra.Command {
18
- Use : "install" ,
19
- Short : "Install a local CodeQL database in the QLDB directory" ,
20
- Long : `Install a local CodeQL database in the QLDB directory` ,
21
- Run : func (cmd * cobra.Command , args []string ) {
22
- install (nwoFlag , dbPathFlag , removeFlag )
23
- },
24
- }
17
+ Use : "install" ,
18
+ Short : "Install a local CodeQL database in the QLDB directory" ,
19
+ Long : `Install a local CodeQL database in the QLDB directory` ,
20
+ Run : func (cmd * cobra.Command , args []string ) {
21
+ install (nwoFlag , dbPathFlag , removeFlag )
22
+ },
23
+ }
25
24
26
25
func init () {
27
- rootCmd .AddCommand (installCmd )
28
- installCmd .Flags ().StringVarP (& nwoFlag , "nwo" , "n" , "" , "The NWO to associate the database to." )
29
- installCmd .Flags ().StringVarP (& dbPathFlag , "database" , "d" , "" , "The path to the database to install." )
30
- installCmd .Flags ().BoolVarP (& removeFlag , "remove" , "r" , false , "Remove the database after installing it." )
31
- installCmd .MarkFlagRequired ("nwo" )
32
- installCmd .MarkFlagRequired ("database" )
26
+ rootCmd .AddCommand (installCmd )
27
+ installCmd .Flags ().StringVarP (& nwoFlag , "nwo" , "n" , "" , "The NWO to associate the database to." )
28
+ installCmd .Flags ().StringVarP (& dbPathFlag , "database" , "d" , "" , "The path to the database to install." )
29
+ installCmd .Flags ().BoolVarP (& removeFlag , "remove" , "r" , false , "Remove the database after installing it." )
30
+ installCmd .MarkFlagRequired ("nwo" )
31
+ installCmd .MarkFlagRequired ("database" )
33
32
}
34
33
35
34
func install (nwo string , dbPath string , remove bool ) {
@@ -42,13 +41,14 @@ func install(nwo string, dbPath string, remove bool) {
42
41
log .Fatal (errors .New ("DB path does not exist" ))
43
42
}
44
43
if fileinfo .IsDir () {
44
+ fmt .Printf ("Validating %s DB\n " , dbPath )
45
45
err := utils .ValidateDB (dbPath )
46
46
if err != nil {
47
47
fmt .Println ("DB is not valid" )
48
48
}
49
49
// Compress DB
50
50
zipfilename := filepath .Join (os .TempDir (), "qldb.zip" )
51
- fmt .Println ("Zipping DB to" , zipfilename )
51
+ fmt .Println ("Compressing DB to" , zipfilename )
52
52
if err := utils .ZipDirectory (zipfilename , dbPath ); err != nil {
53
53
log .Fatal (err )
54
54
}
@@ -61,19 +61,24 @@ func install(nwo string, dbPath string, remove bool) {
61
61
}
62
62
63
63
zipPath = dbPath
64
- // Unzip to temporary directory
65
- tmpdir , _ := ioutil .TempDir ("" , "qldb" )
64
+ // Unzip to a temporary directory
65
+ tmpdir , _ := os .MkdirTemp ("" , "qldb" )
66
+
66
67
_ , err := utils .Unzip (dbPath , tmpdir )
67
68
if err != nil {
68
69
log .Fatal (err )
69
70
}
70
- files , err := ioutil .ReadDir (tmpdir )
71
+
72
+ // Read all files in the tmpdir directory using os.ReadDir
73
+ dirEntries , err := os .ReadDir (tmpdir )
71
74
if err != nil {
72
75
log .Fatal (err )
73
76
}
74
- if len (files ) == 1 {
75
- tmpdir = filepath .Join (tmpdir , files [0 ].Name ())
77
+ if len (dirEntries ) == 1 {
78
+ // if there is one directory in the tmpdir, use that as the tmpdir
79
+ tmpdir = filepath .Join (tmpdir , dirEntries [0 ].Name ())
76
80
}
81
+ fmt .Printf ("Validating %s DB\n " , tmpdir )
77
82
err = utils .ValidateDB (tmpdir )
78
83
if err != nil {
79
84
fmt .Println ("DB is not valid" )
@@ -86,44 +91,58 @@ func install(nwo string, dbPath string, remove bool) {
86
91
log .Fatal (err )
87
92
}
88
93
defer zipFile .Close ()
89
- zipBytes , err := ioutil .ReadAll (zipFile )
94
+ zipBytes , err := io .ReadAll (zipFile )
90
95
if err != nil {
91
96
log .Fatal (err )
92
97
}
93
98
commitSha , primaryLanguage , err := utils .ExtractDBInfo (zipBytes )
99
+ shortCommitSha := commitSha [:8 ]
100
+ fmt .Println ("Commit SHA:" , commitSha )
101
+ fmt .Println ("Short Commit SHA:" , shortCommitSha )
102
+ fmt .Println ("Primary language:" , primaryLanguage )
94
103
95
104
// Destination path
96
- dir := filepath .Join (utils .GetPath (nwo ), primaryLanguage )
97
- filename := fmt .Sprintf ("%s.zip" , commitSha )
98
- path := filepath .Join (dir , filename )
99
- fmt .Println ("Installing DB to" , 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 )
100
108
101
109
// Check if the DB is already installed
102
- if _ , err := os .Stat (path ); errors .Is (err , os .ErrNotExist ) {
103
- // Copy DB to the right place
104
- srcFile , err := os .Open (zipPath )
110
+ if _ , err := os .Stat (destPath ); errors .Is (err , os .ErrNotExist ) {
111
+
112
+ // Create the directory if it doesn't exist
113
+ err = os .MkdirAll (filepath .Dir (destPath ), 0755 )
105
114
if err != nil {
106
115
log .Fatal (err )
116
+ return
107
117
}
108
- defer srcFile .Close ()
109
- err = os .MkdirAll (filepath .Dir (path ), 0755 )
118
+
119
+ // Copy file from zipPath to destPath
120
+ srcFile , err := os .Open (zipPath )
110
121
if err != nil {
111
122
log .Fatal (err )
123
+ return
112
124
}
113
- destFile , err := os .Create (path )
125
+ defer srcFile .Close ()
126
+
127
+ destFile , err := os .Create (destPath )
114
128
if err != nil {
115
129
log .Fatal (err )
130
+ return
116
131
}
117
132
defer destFile .Close ()
118
- fmt .Println ("Copying DB to" , path )
119
- _ , err = io .Copy (srcFile , destFile ) // check first var for number of bytes copied
133
+
134
+ bytes , err := io .Copy (destFile , srcFile )
135
+ fmt .Println (fmt .Sprintf ("Copied %d bytes" , bytes ))
136
+
120
137
if err != nil {
121
138
log .Fatal (err )
122
139
}
123
140
err = destFile .Sync ()
124
141
if err != nil {
125
142
log .Fatal (err )
126
143
}
144
+ } else {
145
+ fmt .Println ("DB already installed for same commit" )
127
146
}
128
147
// Remove DB from the current location if -r flag is set
129
148
if remove {
0 commit comments