2023-02-10 01:18:27 +00:00
|
|
|
package fslocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FS represents a local file system
|
|
|
|
type FS struct {
|
|
|
|
// Pattern is used for matching one or multiple files.
|
|
|
|
// The pattern may describe hierarchical names such as
|
|
|
|
// /usr/*/bin/ed (assuming the Separator is '/').
|
|
|
|
Pattern string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init verifies that configured Pattern is correct
|
|
|
|
func (fs *FS) Init() error {
|
|
|
|
_, err := filepath.Glob(fs.Pattern)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Stringer interface
|
|
|
|
func (fs *FS) String() string {
|
|
|
|
return fmt.Sprintf("Local FS{MatchPattern: %q}", fs.Pattern)
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:46:19 +00:00
|
|
|
// List returns the list of file names which will be read via Read fn
|
|
|
|
func (fs *FS) List() ([]string, error) {
|
2023-02-10 01:18:27 +00:00
|
|
|
matches, err := filepath.Glob(fs.Pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while matching files via pattern %s: %w", fs.Pattern, err)
|
|
|
|
}
|
2023-03-09 13:46:19 +00:00
|
|
|
return matches, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read returns a map of read files where
|
|
|
|
// key is the file name and value is file's content.
|
|
|
|
func (fs *FS) Read(files []string) (map[string][]byte, error) {
|
2023-02-10 01:18:27 +00:00
|
|
|
result := make(map[string][]byte)
|
2023-03-09 13:46:19 +00:00
|
|
|
for _, path := range files {
|
2023-02-10 01:18:27 +00:00
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while reading file %q: %w", path, err)
|
|
|
|
}
|
|
|
|
result[path] = data
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|