mfer/mfer/manifest.go

77 lines
1.5 KiB
Go
Raw Normal View History

2022-12-02 00:31:49 +00:00
package mfer
import (
2022-12-02 00:54:01 +00:00
"fmt"
2022-12-02 00:31:49 +00:00
"io"
"io/fs"
2022-12-02 01:05:09 +00:00
"strings"
2022-12-02 00:31:49 +00:00
"github.com/spf13/afero"
)
type ManifestFile struct {
Path string
FileInfo fs.FileInfo
}
type Manifest struct {
2022-12-02 00:54:01 +00:00
SourceFS afero.Fs
Files []*ManifestFile
2022-12-02 01:05:09 +00:00
ScanOptions *ManifestScanOptions
2022-12-02 00:54:01 +00:00
TotalFileSize int64
2022-12-02 00:31:49 +00:00
}
2022-12-02 00:54:01 +00:00
type ManifestScanOptions struct {
IgnoreDotfiles bool
FollowSymLinks bool
}
func NewFromPath(inputPath string, options *ManifestScanOptions) (*Manifest, error) {
2022-12-02 00:31:49 +00:00
afs := afero.NewBasePathFs(afero.NewOsFs(), inputPath)
2022-12-02 00:54:01 +00:00
return NewFromFilesystem(afs, options)
2022-12-02 00:31:49 +00:00
}
2022-12-02 00:54:01 +00:00
func NewFromFilesystem(fs afero.Fs, options *ManifestScanOptions) (*Manifest, error) {
2022-12-02 00:31:49 +00:00
m := &Manifest{
2022-12-02 01:05:09 +00:00
SourceFS: fs,
ScanOptions: options,
2022-12-02 00:31:49 +00:00
}
m.Scan()
return m, nil
}
func (m *Manifest) Scan() {
afero.Walk(m.SourceFS, "", func(path string, info fs.FileInfo, err error) error {
2022-12-02 00:54:01 +00:00
2022-12-02 01:05:09 +00:00
if m.ScanOptions.IgnoreDotfiles && strings.HasPrefix(path, ".") {
// FIXME make this check all path components BUG
return nil
}
2022-12-02 00:54:01 +00:00
if info != nil && info.IsDir() {
// manifests contain only files, directories are implied.
return nil
}
fmt.Printf("path = %s\n", path)
fileinfo, staterr := m.SourceFS.Stat(path)
if staterr != nil {
panic(staterr)
}
2022-12-02 00:31:49 +00:00
nf := &ManifestFile{
Path: path,
2022-12-02 00:54:01 +00:00
FileInfo: fileinfo,
2022-12-02 00:31:49 +00:00
}
m.Files = append(m.Files, nf)
2022-12-02 00:54:01 +00:00
m.TotalFileSize = m.TotalFileSize + info.Size()
2022-12-02 01:05:09 +00:00
fmt.Printf("total file count now %i\n", len(m.Files))
fmt.Printf("total file size now %i\n", m.TotalFileSize)
2022-12-02 00:31:49 +00:00
return nil
})
}
func (m *Manifest) Write(output io.Writer) error {
return nil
}