Skip to content

Commit b6d7635

Browse files
committed
Refactored VQL
1 parent f34a40c commit b6d7635

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

bin/reformat.go

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import (
1414
)
1515

1616
var (
17-
reformat = artifact_command.Command("reformat", "Reformat a set of artifacts")
18-
reformat_args = reformat.Arg("paths", "Paths to artifact yaml files").Required().Strings()
17+
reformat = artifact_command.Command("reformat", "Reformat a set of artifacts")
18+
reformat_dry_run = reformat.Flag("dry", "Do not overwrite files, just report errors").Bool()
19+
reformat_args = reformat.Arg("paths", "Paths to artifact yaml files").Required().Strings()
1920
)
2021

2122
func doReformat() error {
@@ -58,41 +59,25 @@ func doReformat() error {
5859
ACLManager: acl_managers.NewRoleACLManager(sm.Config, "administrator"),
5960
Logger: log.New(artifact_logger, "", 0),
6061
Env: ordereddict.NewDict().
62+
Set("DryRun", *reformat_dry_run).
6163
Set("Artifacts", artifact_paths),
6264
}
6365

6466
query := `
65-
LET Results <= SELECT FilePath,
66-
Result
67-
FROM foreach(row=Artifacts,
68-
query={
69-
SELECT _value AS FilePath,
70-
reformat(artifact=read_file(filename=_value)) AS Result
71-
FROM scope()
72-
})
73-
74-
LET Errors <= SELECT *
75-
FROM foreach(row=Results,
76-
query={
77-
SELECT log(level="ERROR", message="%s: %s", args=[FilePath, Result.Error])
78-
FROM scope()
79-
WHERE Result.Error
80-
})
81-
82-
SELECT *
83-
FROM foreach(row=Results,
84-
query={
85-
SELECT FilePath,
86-
Result.Artifact AS Artifact,
87-
Result.Error AS Error,
88-
if(condition=NOT Result.Error,
89-
then=copy(filename=Result.Artifact,
90-
accessor="data",
91-
dest=FilePath)) AS _Copy
92-
FROM scope()
93-
})
94-
`
95-
67+
LET Reformatted = SELECT reformat(artifact=read_file(filename=_value)) AS Result
68+
FROM foreach(row=Artifacts)
69+
WHERE if(condition=Result.Error,
70+
then=log(level="ERROR", message="%v: <red>%v</>",
71+
args=[_value, Result.Error], dedup=-1),
72+
else=log(message="Reformatted %v: <green>OK</>",
73+
args=_value, dedup=-1)
74+
AND NOT DryRun
75+
AND copy(accessor="data", dest=_value, filename=Result.Artifact))
76+
AND FALSE
77+
78+
SELECT * FROM Reformatted
79+
80+
`
9681
err = runQueryWithEnv(query, builder, "json")
9782
if err != nil {
9883
logger.Error("reformat: error running query: %v", query)

vql/functions/reformat.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func (self *ReformatFunction) Call(ctx context.Context, scope vfilter.Scope, arg
3636
arg := &ReformatFunctionArgs{}
3737
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, arg)
3838
if err != nil {
39-
scope.Log("reformat: %v", err)
4039
result.Artifact = arg.Artifact
4140
result.Error = err.Error()
4241
return result.ToDict()
@@ -50,15 +49,13 @@ func (self *ReformatFunction) Call(ctx context.Context, scope vfilter.Scope, arg
5049

5150
manager, err := services.GetRepositoryManager(config_obj)
5251
if err != nil {
53-
scope.Log("reformat: %v", err)
5452
result.Artifact = arg.Artifact
5553
result.Error = err.Error()
5654
return result.ToDict()
5755
}
5856

5957
reformatted, err := manager.ReformatVQL(ctx, arg.Artifact)
6058
if err != nil {
61-
scope.Log("reformat: %v", err)
6259
result.Artifact = arg.Artifact
6360
result.Error = err.Error()
6461
return result.ToDict()

0 commit comments

Comments
 (0)