Skip to content

Commit 4ffacf0

Browse files
author
Alvaro Muñoz
committed
Refactor create and download commands
1 parent 9e837e6 commit 4ffacf0

File tree

4 files changed

+69
-66
lines changed

4 files changed

+69
-66
lines changed

cmd/create.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
package cmd
22

33
import (
4-
"log"
54
"fmt"
6-
"path/filepath"
7-
"os/exec"
8-
"os"
5+
"log"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
99

10-
"github.com/spf13/cobra"
10+
"github.com/spf13/cobra"
1111
)
1212

1313
var createCmd = &cobra.Command{
14-
Use: "create",
15-
Short: "Extracts a CodeQL database from a source path",
16-
Long: `Extracts a CodeQL database from a source path. Pass the CodeQL arguments after a '--' separator.
14+
Use: "create",
15+
Short: "Extracts a CodeQL database from a source path",
16+
Long: `Extracts a CodeQL database from a source path. Pass the CodeQL arguments after a '--' separator.
1717
1818
eg: gh-qldb create --nwo foo/bar -- -s /path/to/src -l javascript`,
19-
Run: func(cmd *cobra.Command, args []string) {
20-
// --nwo foo/bar -- -s /path/to/src -l javascript
21-
create(nwoFlag, args)
22-
},
23-
}
19+
Run: func(cmd *cobra.Command, args []string) {
20+
// --nwo foo/bar -- -s /path/to/src -l javascript
21+
create(nwoFlag, args)
22+
},
23+
}
2424

2525
func init() {
26-
rootCmd.AddCommand(createCmd)
27-
createCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to create the database for. If omitted, it will be inferred from git remotes.")
26+
rootCmd.AddCommand(createCmd)
27+
createCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to create the database for. If omitted, it will be inferred from git remotes.")
2828
}
2929

3030
func create(nwo string, codeqlArgs []string) {
3131
fmt.Printf("Creating DB for '%s'. CodeQL args: '%v'", nwo, codeqlArgs)
32-
destPath := filepath.Join(os.TempDir(), "codeql-db")
33-
if err := os.MkdirAll(destPath, 0755); err != nil {
34-
log.Fatal(err)
35-
}
36-
args := []string{"database", "create"}
37-
args = append(args, codeqlArgs...)
38-
args = append(args, "--")
39-
args = append(args, destPath)
32+
destPath := filepath.Join(os.TempDir(), "codeql-db")
33+
if err := os.MkdirAll(destPath, 0755); err != nil {
34+
log.Fatal(err)
35+
}
36+
args := []string{"database", "create"}
37+
args = append(args, codeqlArgs...)
38+
args = append(args, "--")
39+
args = append(args, destPath)
4040
cmd := exec.Command("codeql", args...)
4141
cmd.Env = os.Environ()
4242
_, err := cmd.CombinedOutput()
4343
if err != nil {
4444
log.Fatalln(err)
4545
}
4646

47-
install(nwo, destPath, true)
47+
install(nwo, destPath, true)
4848
}

cmd/download.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"log"
88
"os"
99
"path/filepath"
1010

11+
"github.com/GitHubSecurityLab/gh-qldb/utils"
1112
"github.com/cli/go-gh"
1213
"github.com/cli/go-gh/pkg/api"
13-
"github.com/spf13/cobra"
14-
"github.com/GitHubSecurityLab/gh-qldb/utils"
14+
"github.com/spf13/cobra"
1515
)
1616

1717
var downloadCmd = &cobra.Command{
18-
Use: "download",
19-
Short: "Downloads a CodeQL database from GitHub Code Scanning",
20-
Long: `Downloads a CodeQL database from GitHub Code Scanning`,
21-
Run: func(cmd *cobra.Command, args []string) {
22-
download()
23-
},
24-
}
18+
Use: "download",
19+
Short: "Downloads a CodeQL database from GitHub Code Scanning",
20+
Long: `Downloads a CodeQL database from GitHub Code Scanning`,
21+
Run: func(cmd *cobra.Command, args []string) {
22+
download()
23+
},
24+
}
2525

2626
func init() {
27-
rootCmd.AddCommand(downloadCmd)
28-
downloadCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to download the database for.")
29-
downloadCmd.Flags().StringVarP(&languageFlag, "language", "l", "", "The primary language you want the database for.")
30-
downloadCmd.MarkFlagRequired("nwo")
31-
downloadCmd.MarkFlagRequired("language")
27+
rootCmd.AddCommand(downloadCmd)
28+
downloadCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO of the repository to download the database for.")
29+
downloadCmd.Flags().StringVarP(&languageFlag, "language", "l", "", "The primary language you want the database for.")
30+
downloadCmd.MarkFlagRequired("nwo")
31+
downloadCmd.MarkFlagRequired("language")
3232

3333
}
3434

@@ -77,7 +77,7 @@ func download() {
7777
}
7878
defer resp.Body.Close()
7979

80-
body, err := ioutil.ReadAll(resp.Body)
80+
body, err := io.ReadAll(resp.Body)
8181
if err != nil {
8282
log.Fatal(err)
8383
}
@@ -103,7 +103,7 @@ func download() {
103103
// create file if not exists
104104
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
105105
// write the DB to disk
106-
err = ioutil.WriteFile(path, body, 0755)
106+
err = os.WriteFile(path, body, 0755)
107107
if err != nil {
108108
log.Fatal(err)
109109
}
@@ -116,4 +116,3 @@ func download() {
116116
}
117117
fmt.Println("Done")
118118
}
119-

cmd/info.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/GitHubSecurityLab/gh-qldb/utils"
7-
"github.com/spf13/cobra"
8-
"io/ioutil"
96
"log"
7+
"os"
108
"path/filepath"
9+
10+
"github.com/GitHubSecurityLab/gh-qldb/utils"
11+
"github.com/spf13/cobra"
1112
)
1213

1314
var infoCmd = &cobra.Command{
@@ -30,7 +31,7 @@ func init() {
3031

3132
func info(nwo string, language string) {
3233
dir := filepath.Join(utils.GetPath(nwo), language)
33-
files, err := ioutil.ReadDir(dir)
34+
files, err := os.ReadDir(dir)
3435
if err != nil {
3536
log.Fatal(err)
3637
}
@@ -56,7 +57,7 @@ func info(nwo string, language string) {
5657
fmt.Printf("%s", jsonStr)
5758
} else {
5859
for _, path := range pathList {
59-
fmt.Printf("%s (%s)", path["path"], path["committedDate"])
60+
fmt.Println(path["path"])
6061
}
6162
}
6263
}

utils/utils.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
package utils
22

33
import (
4-
"os/exec"
5-
"io"
4+
"archive/zip"
5+
"bytes"
66
"encoding/json"
7-
"errors"
7+
"errors"
8+
"fmt"
9+
"io"
10+
"log"
11+
"os"
12+
"os/exec"
13+
"path/filepath"
14+
"strings"
15+
816
"gopkg.in/yaml.v3"
9-
"os"
10-
"path/filepath"
11-
"strings"
12-
"archive/zip"
13-
"bytes"
14-
"fmt"
15-
"io/ioutil"
16-
"log"
1717

18-
graphql "github.com/shurcooL/githubv4"
1918
"github.com/cli/go-gh"
19+
graphql "github.com/shurcooL/githubv4"
2020
)
2121

2222
const (
2323
ROOT = "codeql-dbs"
2424
VCS = "github.com"
2525
)
2626

27-
func GetPath(nwo string) string {
27+
func GetBasePath() string {
2828
home := os.Getenv("HOME")
29-
return filepath.Join(home, ROOT, VCS, nwo)
29+
return filepath.Join(home, ROOT, VCS)
30+
}
31+
32+
func GetPath(nwo string) string {
33+
return filepath.Join(GetBasePath(), nwo)
3034
}
3135

3236
func ValidateDB(dbPath string) error {
33-
fmt.Printf("Validating %s DB\n", dbPath)
3437
cmd := exec.Command("codeql", "resolve", "database", dbPath)
3538
cmd.Env = os.Environ()
3639
jsonBytes, err := cmd.CombinedOutput()
@@ -58,7 +61,7 @@ func ExtractDBInfo(body []byte) (string, string, error) {
5861
log.Fatal(err)
5962
}
6063
defer f.Close()
61-
yamlBytes, err := ioutil.ReadAll(f)
64+
yamlBytes, err := io.ReadAll(f)
6265
if err != nil {
6366
log.Fatal(err)
6467
}
@@ -217,7 +220,7 @@ func GetCommitInfo(nwo string, commitSha string) (string, string, error) {
217220

218221
graphqlClient, err := gh.GQLClient(nil)
219222
if err != nil {
220-
return "", "", err
223+
return "", "", err
221224
}
222225
var query struct {
223226
Repository struct {
@@ -237,7 +240,7 @@ func GetCommitInfo(nwo string, commitSha string) (string, string, error) {
237240
}
238241
err = graphqlClient.Query("CommitInfo", &query, variables)
239242
if err != nil {
240-
return "", "", err
243+
return "", "", err
241244
}
242-
return string(query.Repository.Object.Commit.AbbreviatedOid) , query.Repository.Object.Commit.CommittedDate.Format("2006-01-02T15:04:05"), nil
245+
return string(query.Repository.Object.Commit.AbbreviatedOid), query.Repository.Object.Commit.CommittedDate.Format("2006-01-02T15:04:05"), nil
243246
}

0 commit comments

Comments
 (0)