Skip to content

Commit

Permalink
feat: Scan files from a directory (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 authored Sep 14, 2022
1 parent 031203c commit 331af40
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
9 changes: 7 additions & 2 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
47 changes: 47 additions & 0 deletions utils/file_utils.go
Original file line number Diff line number Diff line change
@@ -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()
}
5 changes: 2 additions & 3 deletions utils/string_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 331af40

Please sign in to comment.