diff --git a/cmd/scan.go b/cmd/scan.go index 1822a66..ba48e85 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -64,16 +64,19 @@ You can use the "vt analysis" command for retrieving information about the analyses. If the command receives a single hypen (-) the file paths are read from the standard -input, one per line.` +input, one per line. + +The command can also receive a directory to scan all files contained on it.` var scanFileCmdExample = ` vt scan file foo.exe vt scan file foo.exe bar.exe + vt scan file foo/ cat list_of_file_paths | vt scan file -` // NewScanFileCmd returns a new instance of the 'scan file' command. func NewScanFileCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "file [file]...", + Use: "file [[dir] | [file]...]", Short: "Scan one or more files", Long: scanFileCmdHelp, Example: scanFileCmdExample, @@ -84,6 +87,8 @@ func NewScanFileCmd() *cobra.Command { var argReader utils.StringReader if len(args) == 1 && args[0] == "-" { argReader = utils.NewStringIOReader(os.Stdin) + } else if len(args) == 1 && utils.IsDir(args[0]) { + argReader, _ = utils.NewFileDirReader(args[0]) } else { argReader = utils.NewStringArrayReader(args) } diff --git a/go.mod b/go.mod index 3fb02f7..6a91f2f 100644 --- a/go.mod +++ b/go.mod @@ -22,5 +22,5 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.7.0 - golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934 // indirect + golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 // indirect ) diff --git a/go.sum b/go.sum index d86ce72..7616c0a 100644 --- a/go.sum +++ b/go.sum @@ -299,6 +299,8 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934 h1:u/E0NqCIWRDAo9WCFo6Ko49njPFDLSd3z+X1HgWDMpE= golang.org/x/sys v0.0.0-20191028164358-195ce5e7f934/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho= +golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= diff --git a/utils/file_utils.go b/utils/file_utils.go new file mode 100644 index 0000000..a3fd82c --- /dev/null +++ b/utils/file_utils.go @@ -0,0 +1,47 @@ +// Copyright © 2019 The VirusTotal CLI authors. All Rights Reserved. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package utils + +import ( + "os" + "path" +) + +// FileDirReader returns all files inside a given directory +// as a StringArrayReader +func NewFileDirReader(fileDir string) (*StringArrayReader, error) { + files, err := os.ReadDir(fileDir) + if err != nil { + return nil, err + } + fileNames := []string{} + for _, f := range files { + // Skip subdirectories + if f.IsDir() { + continue + } + fileNames = append(fileNames, path.Join(fileDir, f.Name())) + } + return &StringArrayReader{strings: fileNames}, nil +} + +// IsDir function returns whether a file is a directory or not +func IsDir(f string) bool { + fileInfo, err := os.Stat(f) + if err != nil { + // error reading the file, assuming it is not a directory + return false + } + return fileInfo.IsDir() +} diff --git a/utils/string_reader.go b/utils/string_reader.go index 7bd33bf..0201605 100644 --- a/utils/string_reader.go +++ b/utils/string_reader.go @@ -99,18 +99,17 @@ func (f *FilteredStringReader) ReadString() (s string, err error) { return s, err } - // MappedStringReader reads strings from a StringReader and call a map function // that transforms the strings in some other string. type MappedStringReader struct { - r StringReader + r StringReader mapFn func(string) string } // NewMappedStringReader creates a new MappedStringReader that reads strings from // r and can call mapFn for transforming the string before returning it. func NewMappedStringReader(r StringReader, mapFn func(string) string) *MappedStringReader { - return &MappedStringReader{r:r, mapFn: mapFn} + return &MappedStringReader{r: r, mapFn: mapFn} } // ReadString reads strings from the underlying StringReader and can call the