mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-25 21:39:10 +00:00
Merge branch 'fix-missing-close-on-csv' into 'master'
Add missing close when reading CSV file See merge request nvidia/container-toolkit/container-toolkit!128
This commit is contained in:
commit
1296a0ecf4
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -81,17 +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)
|
|
||||||
}
|
|
||||||
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)
|
||||||
@ -99,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)
|
||||||
|
Loading…
Reference in New Issue
Block a user