mfer/src/manifest.go

70 lines
1.3 KiB
Go

package mfer
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/afero"
)
type MFGenerationJob struct {
sourcePath string
outputFile string
innerpb *MFFileInner
outerpb *MFFile
fileCount int64
totalSize int64
afs afero.Fs
}
func NewMFGenerationJobFromFilesystem(sourcePath string) (*MFGenerationJob, error) {
afs := afero.NewOsFs()
exists, err := afero.DirExists(afs, sourcePath)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("source directory does not exist")
}
mgj := MFGenerationJob{}
mgj.afs = afs
mgj.sourcePath = sourcePath
return &mgj, nil
}
func (m *MFGenerationJob) scanForFiles() error {
m.innerpb = &MFFileInner{}
m.innerpb.Version = MFFileInner_ONE
walkErr := filepath.Walk(m.sourcePath, func(itemPath string, info os.FileInfo, err error) error {
// we do not include the manifest file in the manifest
if itemPath == "index.mf" {
return nil
}
fpi := MFFilePath{}
fpi.Path = itemPath
fpi.Size = info.Size()
m.innerpb.Files = append(m.innerpb.Files, &fpi)
m.fileCount++
m.totalSize += fpi.Size
return nil
})
if walkErr != nil {
log.Fatal(walkErr)
return walkErr
}
fmt.Printf("%#v\n", m.innerpb)
fmt.Printf("filecount = %#v\n", m.fileCount)
fmt.Printf("totalsize = %#v\n", m.totalSize)
return nil
}