Skip to content

Commit 257ca6a

Browse files
committed
refactor: extract UnmarshalArtifact
1 parent 545b805 commit 257ca6a

3 files changed

Lines changed: 29 additions & 42 deletions

File tree

cmd/batch_clone_build/main.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"log"
76
"os"
87

98
"github.com/Lslightly/qlstat/config"
10-
"github.com/goccy/go-yaml"
119
)
1210

1311
type Options struct {
@@ -34,15 +32,7 @@ func main() {
3432
flag.Usage()
3533
os.Exit(1)
3634
}
37-
yamlPath := flag.Arg(0)
38-
yamlData, err := os.ReadFile(yamlPath)
39-
if err != nil {
40-
log.Fatalf("Failed to read config file: %v", err)
41-
}
42-
cfg := new(config.Artifact)
43-
if err := yaml.Unmarshal(yamlData, cfg); err != nil {
44-
log.Fatalf("Failed to parse YAML: %v", err)
45-
}
35+
cfg := config.UnmarshalArtifact(flag.Arg(0))
4636
dirSetup(cfg)
4737
if !opt.disableClone {
4838
batchClone(cfg)

cmd/codeql_qdriver/main.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
"github.com/Lslightly/qlstat/config"
2121
"github.com/Lslightly/qlstat/utils"
22-
yaml "github.com/goccy/go-yaml"
2322
"github.com/schollz/progressbar/v3"
2423
)
2524

@@ -30,10 +29,7 @@ var validFmts = []string{
3029
"bqrs",
3130
}
3231

33-
var cfg config.Artifact
34-
3532
var (
36-
configPath string
3733
targetDecodeFmt string
3834
onlyDecode bool
3935
doCollect bool
@@ -55,39 +51,27 @@ func main() {
5551
flag.Usage()
5652
os.Exit(1)
5753
}
58-
configPath = flag.Arg(0)
59-
parseConfig()
54+
cfg := config.UnmarshalArtifact(flag.Arg(0))
6055
if cfg.ParallelCore != 0 {
6156
runtime.GOMAXPROCS(cfg.ParallelCore)
6257
}
6358
if !onlyDecode {
64-
if _, err := config.ArchiveCurrentIfExist(&cfg, "query"); err != nil {
59+
if _, err := config.ArchiveCurrentIfExist(cfg, "query"); err != nil {
6560
log.Fatalf("Failed to archive current log dir: %v", err)
6661
}
6762
for grpi, grp := range cfg.QueryGrps {
6863
fmt.Printf("Grp %d: Executing queries\n", grpi)
69-
queriesExec(grp)
64+
queriesExec(cfg, grp)
7065
}
7166
}
7267
fmt.Println("Decoding results")
73-
if _, err := config.ArchiveCurrentIfExist(&cfg, "decode"); err != nil {
68+
if _, err := config.ArchiveCurrentIfExist(cfg, "decode"); err != nil {
7469
log.Fatalf("Failed to archive current log dir: %v", err)
7570
}
76-
decodeResults(targetDecodeFmt)
71+
decodeResults(cfg, targetDecodeFmt)
7772
if doCollect && targetDecodeFmt == "csv" {
7873
fmt.Println("Collecting CSVs")
79-
collectCSVs()
80-
}
81-
}
82-
83-
func parseConfig() {
84-
bs, err := os.ReadFile(configPath)
85-
if err != nil {
86-
log.Fatalln("error occurs when reading config", configPath, err)
87-
}
88-
err = yaml.Unmarshal(bs, &cfg)
89-
if err != nil {
90-
log.Fatalln("error occurs when parsing json", err)
74+
collectCSVs(cfg)
9175
}
9276
}
9377

@@ -106,7 +90,7 @@ first remove all content in ${config.OutResultRoot}/${qScriptNameNoExt}
10690
dump error log for ${db} in ${config.OutResultRoot}/${qScriptNameNoExt}/error.log
10791
dump stdout/stderr for ${db} in ${config.OutResultRoot}/${qScriptNameNoExt}/log/${db}.log
10892
*/
109-
func queriesExec(grp config.QueryGroup) {
93+
func queriesExec(cfg *config.Artifact, grp config.QueryGroup) {
11094
dbs := cfg.ConvStrSliceToDBSlice(grp.QueryDBs)
11195
bar := progressbar.Default(int64(len(dbs) * len(grp.Queries)))
11296
for _, qScript := range grp.Queries {
@@ -121,7 +105,7 @@ func queriesExec(grp config.QueryGroup) {
121105
wg.Add(1)
122106
go func(db config.DB, query config.Query) {
123107
defer wg.Done()
124-
queryForOneDB(db, query)
108+
queryForOneDB(cfg, db, query)
125109
bar.Describe(fmt.Sprintf("query: %-15s db: %-15s exts: %-15s", query.Name(), db.Name, query.ExternalsSingleString()))
126110
bar.Add(1)
127111
}(db, query)
@@ -139,7 +123,7 @@ func queriesExec(grp config.QueryGroup) {
139123
/*
140124
codeql query run -d=${config.InDBRoot}/${repo} ${config.QueryRoot}/${qScript} --output=${qResultDir}/${repo} --search-path=./qlsrc/lib --external=$pred/${config.dbRoot}/${repo}/ext/$pred.csv
141125
*/
142-
func queryForOneDB(db config.DB, query config.Query) {
126+
func queryForOneDB(cfg *config.Artifact, db config.DB, query config.Query) {
143127
qResultDir := query.DirPath(cfg.ResultRoot)
144128
repoOut, repoErr := utils.CreateOutAndErr(filepath.Join(query.DirPath(cfg.PassLogDir("query")), db.Name))
145129
defer repoOut.Close()
@@ -180,7 +164,7 @@ func checkDecodeTargetFmt(tgtFmt string) bool {
180164
/*
181165
codeql bqrs decode --format=${tgtFmt} ${path}/${fileBase}.bqrs --output=${path}/${fileBase}.${tgtFmt}
182166
*/
183-
func decodeResults(tgtFmt string) {
167+
func decodeResults(cfg *config.Artifact, tgtFmt string) {
184168
checkDecodeTargetFmt(tgtFmt)
185169

186170
// decode bqrs only in query result dir
@@ -192,12 +176,12 @@ func decodeResults(tgtFmt string) {
192176
bar.Add(1)
193177
query := config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles)
194178
bar.Describe(fmt.Sprintf("Grp %d: Decoding %s dir", grpi, query.Name()))
195-
decodeFilesInDir(tgtFmt, query)
179+
decodeFilesInDir(cfg, tgtFmt, query)
196180
}
197181
}
198182
}
199183

200-
func decodeFilesInDir(tgtFmt string, query config.Query) {
184+
func decodeFilesInDir(cfg *config.Artifact, tgtFmt string, query config.Query) {
201185
decodeLogDir := filepath.Join(cfg.PassLogDir("decode"), query.PathNoExt())
202186
utils.MkdirAll(decodeLogDir)
203187

@@ -228,19 +212,19 @@ func decodeFilesInDir(tgtFmt string, query config.Query) {
228212
wg.Wait()
229213
}
230214

231-
func collectCSVs() {
215+
func collectCSVs(cfg *config.Artifact) {
232216
for grpi, grp := range cfg.QueryGrps {
233217
bar := progressbar.Default(int64(len(grp.Queries)))
234218
for _, qScript := range grp.Queries {
235219
query := config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles)
236220
bar.Describe(fmt.Sprintf("Grp %d: Collecting for "+query.PathNoExt(), grpi))
237-
collectCSVsForQuery(query)
221+
collectCSVsForQuery(cfg, query)
238222
bar.Add(1)
239223
}
240224
}
241225
}
242226

243-
func collectCSVsForQuery(query config.Query) {
227+
func collectCSVsForQuery(cfg *config.Artifact, query config.Query) {
244228
qResultDir := query.DirPath(cfg.ResultRoot)
245229
qResultCSV := qResultDir + ".csv"
246230

config/artifact.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"encoding/json"
5+
"log"
56
"os"
67
"path/filepath"
78
"time"
@@ -42,6 +43,18 @@ type QueryGroup struct {
4243
ExternalFiles []string `yaml:"externalFiles"`
4344
}
4445

46+
func UnmarshalArtifact(filename string) *Artifact {
47+
data, err := os.ReadFile(filename)
48+
if err != nil {
49+
log.Fatalf("Failed to read config file: %v", err)
50+
}
51+
cfg := new(Artifact)
52+
if err := yaml.Unmarshal(data, cfg); err != nil {
53+
log.Fatalf("Failed to parse YAML: %v", err)
54+
}
55+
return cfg
56+
}
57+
4558
// ReadExtsFromFile reads filename and returns a slice of non-empty external predicates' names defined in the file
4659
func ReadExtsFromFile(filename string) (externals []string, err error) {
4760
var exts []string

0 commit comments

Comments
 (0)