Refactor CSV file parsing

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-04-08 08:11:10 +02:00
parent d8109dc49b
commit d1a38f10a5
2 changed files with 33 additions and 13 deletions

View File

@ -68,7 +68,7 @@ func NewFromCSVFiles(logger *logrus.Logger, files []string, root string) (Discov
// MountSpecType. // MountSpecType.
func NewFromCSVFile(logger *logrus.Logger, locators map[csv.MountSpecType]lookup.Locator, filename string) (Discover, error) { func NewFromCSVFile(logger *logrus.Logger, locators map[csv.MountSpecType]lookup.Locator, filename string) (Discover, error) {
// Create a discoverer for each file-kind combination // Create a discoverer for each file-kind combination
targets, err := csv.ParseFile(logger, filename) targets, err := csv.NewCSVFileParser(logger, filename).Parse()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse CSV file: %v", err) return nil, fmt.Errorf("failed to parse CSV file: %v", err)
} }

View File

@ -81,19 +81,39 @@ func BaseFilesOnly(filenames []string) []string {
return selected return selected
} }
// ParseFile parses the specified file and returns a list of required jetson mounts // Parser specifies an interface for parsing MountSpecs
func ParseFile(logger *logrus.Logger, filename string) ([]*MountSpec, error) { type Parser interface {
csvFile, err := os.Open(filename) Parse() ([]*MountSpec, error)
if err != nil {
return nil, fmt.Errorf("failed to open %v for reading: %v", filename, err)
}
defer csvFile.Close()
return parseCSVFromReader(logger, csvFile), nil
} }
// parseCSVFromReader parses the specified file and returns a list of required jetson mounts type csv struct {
func parseCSVFromReader(logger *logrus.Logger, reader io.Reader) []*MountSpec { logger *logrus.Logger
filename string
}
// NewCSVFileParser creates a new parser for reading MountSpecs from the specified CSV file
func NewCSVFileParser(logger *logrus.Logger, filename string) Parser {
p := csv{
logger: logger,
filename: filename,
}
return &p
}
// Parse parses the csv file and returns a list of MountSpecs in the file
func (p csv) Parse() ([]*MountSpec, error) {
reader, err := os.Open(p.filename)
if err != nil {
return nil, fmt.Errorf("failed to open %v for reading: %v", p.filename, err)
}
defer reader.Close()
return p.parseFromReader(reader), nil
}
// parseFromReader parses the specified file and returns a list of required jetson mounts
func (p csv) parseFromReader(reader io.Reader) []*MountSpec {
var targets []*MountSpec var targets []*MountSpec
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
@ -101,7 +121,7 @@ func parseCSVFromReader(logger *logrus.Logger, reader io.Reader) []*MountSpec {
line := scanner.Text() line := scanner.Text()
target, err := NewMountSpecFromLine(line) target, err := NewMountSpecFromLine(line)
if err != nil { if err != nil {
logger.Debugf("Skipping invalid mount spec '%v': %v", line, err) p.logger.Debugf("Skipping invalid mount spec '%v': %v", line, err)
continue continue
} }
targets = append(targets, target) targets = append(targets, target)