Skip to content

Commit 79561ad

Browse files
committed
🧹 extract find cmd generator into its own package
1 parent 1f3bf90 commit 79561ad

File tree

3 files changed

+106
-50
lines changed

3 files changed

+106
-50
lines changed

providers/os/resources/files.go

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package resources
66

77
import (
88
"errors"
9-
"fmt"
109
"regexp"
1110
"strconv"
1211
"strings"
@@ -15,17 +14,9 @@ import (
1514
"go.mondoo.com/cnquery/v12/llx"
1615
"go.mondoo.com/cnquery/v12/providers-sdk/v1/plugin"
1716
"go.mondoo.com/cnquery/v12/providers/os/connection/shared"
17+
"go.mondoo.com/cnquery/v12/providers/os/resources/filesfind"
1818
)
1919

20-
var findTypes = map[string]string{
21-
"file": "f",
22-
"directory": "d",
23-
"character": "c",
24-
"block": "b",
25-
"socket": "s",
26-
"link": "l",
27-
}
28-
2920
func initFilesFind(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
3021
if args["permissions"] == nil {
3122
args["permissions"] = llx.IntData(int64(0o777))
@@ -34,10 +25,6 @@ func initFilesFind(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[s
3425
return args, nil, nil
3526
}
3627

37-
func octal2string(o int64) string {
38-
return fmt.Sprintf("%o", o)
39-
}
40-
4128
func (l *mqlFilesFind) id() (string, error) {
4229
var id strings.Builder
4330
id.WriteString(l.From.Data)
@@ -62,7 +49,7 @@ func (l *mqlFilesFind) id() (string, error) {
6249
}
6350

6451
if l.Permissions.Data != 0o777 {
65-
id.WriteString(" permissions=" + octal2string(l.Permissions.Data))
52+
id.WriteString(" permissions=" + filesfind.Octal2string(l.Permissions.Data))
6653
}
6754

6855
return id.String(), nil
@@ -84,6 +71,9 @@ func (l *mqlFilesFind) list() ([]any, error) {
8471
}
8572
} else if conn.Capabilities().Has(shared.Capability_RunCommand) && pf.IsFamily("unix") {
8673
foundFiles, err = l.unixFilesFindCmd()
74+
if err != nil {
75+
return nil, err
76+
}
8777
} else {
8878
return nil, errors.New("find is not supported for your platform")
8979
}
@@ -142,45 +132,14 @@ func (l *mqlFilesFind) fsFilesFind(conn shared.Connection) ([]string, error) {
142132
}
143133

144134
func (l *mqlFilesFind) unixFilesFindCmd() ([]string, error) {
145-
var call strings.Builder
146-
call.WriteString("find -L ")
147-
call.WriteString(strconv.Quote(l.From.Data))
148-
149-
if !l.Xdev.Data {
150-
call.WriteString(" -xdev")
151-
}
152-
153-
if l.Type.Data != "" {
154-
t, ok := findTypes[l.Type.Data]
155-
if ok {
156-
call.WriteString(" -type " + t)
157-
}
158-
}
159-
160-
if l.Regex.Data != "" {
161-
// TODO: we need to escape regex here
162-
call.WriteString(" -regex '")
163-
call.WriteString(l.Regex.Data)
164-
call.WriteString("'")
165-
}
166-
167-
if l.Permissions.Data != 0o777 {
168-
call.WriteString(" -perm -")
169-
call.WriteString(octal2string(l.Permissions.Data))
170-
}
171-
172-
if l.Name.Data != "" {
173-
call.WriteString(" -name ")
174-
call.WriteString(l.Name.Data)
175-
}
176-
135+
var depth *int64
177136
if l.Depth.IsSet() {
178-
call.WriteString(" -maxdepth ")
179-
call.WriteString(octal2string(l.Depth.Data))
137+
depth = &l.Depth.Data
180138
}
181139

140+
callCmd := filesfind.BuildFilesFindCmd(l.From.Data, l.Xdev.Data, l.Type.Data, l.Regex.Data, l.Permissions.Data, l.Name.Data, depth)
182141
rawCmd, err := CreateResource(l.MqlRuntime, "command", map[string]*llx.RawData{
183-
"command": llx.StringData(call.String()),
142+
"command": llx.StringData(callCmd),
184143
})
185144
if err != nil {
186145
return nil, err
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package filesfind
5+
6+
import (
7+
"fmt"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
var findTypes = map[string]string{
13+
"file": "f",
14+
"directory": "d",
15+
"character": "c",
16+
"block": "b",
17+
"socket": "s",
18+
"link": "l",
19+
}
20+
21+
func Octal2string(o int64) string {
22+
return fmt.Sprintf("%o", o)
23+
}
24+
25+
func BuildFilesFindCmd(from string, xdev bool, fileType string, regex string, permission int64, search string, depth *int64) string {
26+
var call strings.Builder
27+
call.WriteString("find -L ")
28+
call.WriteString(strconv.Quote(from))
29+
30+
if !xdev {
31+
call.WriteString(" -xdev")
32+
}
33+
34+
if fileType != "" {
35+
t, ok := findTypes[fileType]
36+
if ok {
37+
call.WriteString(" -type " + t)
38+
}
39+
}
40+
41+
if regex != "" {
42+
// TODO: we need to escape regex here
43+
call.WriteString(" -regex '")
44+
call.WriteString(regex)
45+
call.WriteString("'")
46+
}
47+
48+
if permission != 0o777 {
49+
call.WriteString(" -perm -")
50+
call.WriteString(Octal2string(permission))
51+
}
52+
53+
if search != "" {
54+
call.WriteString(" -name ")
55+
call.WriteString(search)
56+
}
57+
58+
if depth != nil {
59+
call.WriteString(" -maxdepth ")
60+
call.WriteString(Octal2string(*depth))
61+
}
62+
return call.String()
63+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Mondoo, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package filesfind
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestUnixFilesCmdGeneration(t *testing.T) {
13+
tests := []struct {
14+
From string
15+
Xdev bool
16+
FileType string
17+
Regex string
18+
Permission int64
19+
Search string
20+
Depth *int64
21+
ExpectedCmd string
22+
}{
23+
{
24+
From: "/Users/john/.aws",
25+
FileType: "file",
26+
ExpectedCmd: "find -L \"/Users/john/.aws\" -xdev -type f -perm -0",
27+
},
28+
}
29+
30+
for _, tt := range tests {
31+
cmd := BuildFilesFindCmd(tt.From, tt.Xdev, tt.FileType, tt.Regex, tt.Permission, tt.Search, tt.Depth)
32+
assert.Equal(t, tt.ExpectedCmd, cmd)
33+
}
34+
}

0 commit comments

Comments
 (0)