mfer/mfer/manifest.go

115 lines
2.2 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-04 00:33:24 +00:00
"os"
2022-12-03 23:18:47 +00:00
"path/filepath"
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
}
2022-12-03 23:18:47 +00:00
func (m *ManifestFile) String() string {
return fmt.Sprintf("<File \"%s\">", m.Path)
}
2022-12-02 00:31:49 +00:00
type Manifest struct {
2022-12-02 00:54:01 +00:00
SourceFS afero.Fs
2022-12-03 23:18:47 +00:00
SourceFSRoot string
2022-12-02 00:54:01 +00:00
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-04 00:33:24 +00:00
func (m *Manifest) String() string {
return fmt.Sprintf("<Manifest count=%d totalSize=%d>", len(m.Files), m.TotalFileSize)
}
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-03 23:18:47 +00:00
abs, err := filepath.Abs(inputPath)
if err != nil {
return nil, err
}
afs := afero.NewBasePathFs(afero.NewOsFs(), abs)
m, err := NewFromFS(afs, options)
if err != nil {
return nil, err
}
m.SourceFSRoot = abs
return m, nil
2022-12-02 00:31:49 +00:00
}
2022-12-03 23:18:47 +00:00
func NewFromFS(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
}
2022-12-04 00:33:24 +00:00
err := m.Scan()
if err != nil {
return nil, err
}
2022-12-02 00:31:49 +00:00
return m, nil
}
2022-12-04 00:33:24 +00:00
func (m *Manifest) Scan() error {
// FIXME scan and whatever function does the hashing should take ctx
oe := afero.Walk(m.SourceFS, "./", func(path string, info fs.FileInfo, err error) error {
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
}
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 00:31:49 +00:00
return nil
})
2022-12-04 00:33:24 +00:00
if oe != nil {
return oe
}
return nil
}
func (m *Manifest) WriteToFile(path string) error {
// FIXME refuse to overwrite without -f if file exists
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return m.Write(f)
2022-12-02 00:31:49 +00:00
}
func (m *Manifest) Write(output io.Writer) error {
2022-12-04 00:33:24 +00:00
// FIXME implement
panic("nope")
return nil // nolint:all
2022-12-02 00:31:49 +00:00
}