|
| 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 | +} |
0 commit comments