Skip to content
Snippets Groups Projects
fs_job.go 1.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • package core
    
    import (
    	"bufio"
    	"log"
    	"os"
    	"path/filepath"
    	"strings"
    )
    
    // Return a new Job that will scan path recursively for ULO/RDF files
    // and pass them to the executing Importer.
    func NewFileSystemJob(path string) *Job {
    	return &Job{
    		collectfunc: makeFsCollectFunc(path),
    		id:          generateJobId(),
    	}
    }
    
    // Function that is called when collecting ULO/RDF from the local
    // file system.
    func makeFsCollectFunc(path string) func(*Job, *Collector) error {
    	return func(j *Job, c *Collector) error {
    		return filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
    			if err != nil {
    				j.Logf("error walking path=%v: %v", path, err)
    				return nil
    			}
    
    			log.Println(path)
    			return importLocalFile(c.im, path)
    		})
    	}
    }
    
    // Import file at path with im. Returns no error if the file at
    // path is a not supported type.
    func importLocalFile(im Importer, path string) error {
    	if strings.HasSuffix(path, ".rdf") {
    		return importLocalRdfFile(im, path)
    	}
    
    	return nil
    }
    
    // Import ULO/RDF file at path with im.
    func importLocalRdfFile(im Importer, path string) error {
    	fd, err := os.Open(path)
    	if err != nil {
    		return err
    	}
    
    	defer fd.Close()
    
    	r := bufio.NewReader(fd)
    	return im.Import(r)
    }