package qbitrun import ( "io" "io/fs" "os" "path/filepath" ) type WorkflowDir interface { Files() []io.ReadCloser } type workflowFiles []io.ReadCloser func (wf workflowFiles) Files() []io.ReadCloser { return wf } // OpenDir opens a directory and returns a WorkflowDir. // // Set filter to filter out unwanted files. func OpenDir(dir string, filters ...func(path string, d fs.FileInfo) bool) (WorkflowDir, error) { var files []io.ReadCloser err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } ok := true for _, filter := range filters { ok = filter(path, info) if !ok { break } } if ok { file, err := os.Open(path) if err != nil { return err } files = append(files, file) } return nil }) if err != nil { return nil, err } return workflowFiles(files), nil } // WorkflowFromReadClosers creates a WorkflowDir from a list of io.ReadCloser. func WorkflowFromReadClosers(rcs ...io.ReadCloser) WorkflowDir { return workflowFiles(rcs) } // WorkflowFromReaders creates a WorkflowDir from a list of io.Reader. // // If the Reader implements io.ReadCloser, it will be used as is. // If not, it will be wrapped with io.NopCloser. func WorkflowFromReaders(readers ...io.Reader) WorkflowDir { var rcs []io.ReadCloser for _, r := range readers { if rc, ok := r.(io.ReadCloser); ok { rcs = append(rcs, rc) } else { rcs = append(rcs, io.NopCloser(r)) } } return workflowFiles(rcs) }