Skip to content

Commit 331af40

Browse files
authored
feat: Scan files from a directory (#55)
1 parent 031203c commit 331af40

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

cmd/scan.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ You can use the "vt analysis" command for retrieving information about the
6464
analyses.
6565
6666
If the command receives a single hypen (-) the file paths are read from the standard
67-
input, one per line.`
67+
input, one per line.
68+
69+
The command can also receive a directory to scan all files contained on it.`
6870

6971
var scanFileCmdExample = ` vt scan file foo.exe
7072
vt scan file foo.exe bar.exe
73+
vt scan file foo/
7174
cat list_of_file_paths | vt scan file -`
7275

7376
// NewScanFileCmd returns a new instance of the 'scan file' command.
7477
func NewScanFileCmd() *cobra.Command {
7578
cmd := &cobra.Command{
76-
Use: "file [file]...",
79+
Use: "file [[dir] | [file]...]",
7780
Short: "Scan one or more files",
7881
Long: scanFileCmdHelp,
7982
Example: scanFileCmdExample,
@@ -84,6 +87,8 @@ func NewScanFileCmd() *cobra.Command {
8487
var argReader utils.StringReader
8588
if len(args) == 1 && args[0] == "-" {
8689
argReader = utils.NewStringIOReader(os.Stdin)
90+
} else if len(args) == 1 && utils.IsDir(args[0]) {
91+
argReader, _ = utils.NewFileDirReader(args[0])
8792
} else {
8893
argReader = utils.NewStringArrayReader(args)
8994
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ require (
2222
github.com/spf13/pflag v1.0.5
2323
github.com/spf13/viper v1.7.1
2424
github.com/stretchr/testify v1.7.0
25-
golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934 // indirect
25+
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 // indirect
2626
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w
299299
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
300300
golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934 h1:u/E0NqCIWRDAo9WCFo6Ko49njPFDLSd3z+X1HgWDMpE=
301301
golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
302+
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho=
303+
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
302304
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
303305
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
304306
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=

utils/file_utils.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright © 2019 The VirusTotal CLI authors. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package utils
15+
16+
import (
17+
"os"
18+
"path"
19+
)
20+
21+
// FileDirReader returns all files inside a given directory
22+
// as a StringArrayReader
23+
func NewFileDirReader(fileDir string) (*StringArrayReader, error) {
24+
files, err := os.ReadDir(fileDir)
25+
if err != nil {
26+
return nil, err
27+
}
28+
fileNames := []string{}
29+
for _, f := range files {
30+
// Skip subdirectories
31+
if f.IsDir() {
32+
continue
33+
}
34+
fileNames = append(fileNames, path.Join(fileDir, f.Name()))
35+
}
36+
return &StringArrayReader{strings: fileNames}, nil
37+
}
38+
39+
// IsDir function returns whether a file is a directory or not
40+
func IsDir(f string) bool {
41+
fileInfo, err := os.Stat(f)
42+
if err != nil {
43+
// error reading the file, assuming it is not a directory
44+
return false
45+
}
46+
return fileInfo.IsDir()
47+
}

utils/string_reader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,17 @@ func (f *FilteredStringReader) ReadString() (s string, err error) {
9999
return s, err
100100
}
101101

102-
103102
// MappedStringReader reads strings from a StringReader and call a map function
104103
// that transforms the strings in some other string.
105104
type MappedStringReader struct {
106-
r StringReader
105+
r StringReader
107106
mapFn func(string) string
108107
}
109108

110109
// NewMappedStringReader creates a new MappedStringReader that reads strings from
111110
// r and can call mapFn for transforming the string before returning it.
112111
func NewMappedStringReader(r StringReader, mapFn func(string) string) *MappedStringReader {
113-
return &MappedStringReader{r:r, mapFn: mapFn}
112+
return &MappedStringReader{r: r, mapFn: mapFn}
114113
}
115114

116115
// ReadString reads strings from the underlying StringReader and can call the

0 commit comments

Comments
 (0)