feat: Add io.Reader-based API to archive cmd

This commit is contained in:
Felicitas Pojtinger
2021-12-15 23:14:22 +01:00
parent 3d4ee0d7ed
commit 129cfab84a
9 changed files with 99 additions and 189 deletions
+46 -1
View File
@@ -2,6 +2,10 @@ package cmd
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/pojntfx/stfs/internal/compression"
"github.com/pojntfx/stfs/internal/keys"
@@ -105,8 +109,49 @@ var operationArchiveCmd = &cobra.Command{
logging.NewLogger().PrintHeaderEvent,
)
files := make(chan config.FileConfig)
errs := make(chan error)
go func() {
if err := filepath.Walk(viper.GetString(fromFlag), func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
link := ""
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if link, err = os.Readlink(path); err != nil {
return err
}
}
files <- config.FileConfig{
GetFile: func() (io.ReadSeekCloser, error) {
return os.Open(path)
},
Info: info,
Path: path,
Link: link,
}
return nil
}); err != nil {
errs <- err
return
}
errs <- io.EOF
}()
if _, err := ops.Archive(
viper.GetString(fromFlag),
func() (config.FileConfig, error) {
select {
case file := <-files:
return file, err
case err := <-errs:
return config.FileConfig{}, err
}
},
viper.GetString(compressionLevelFlag),
viper.GetBool(overwriteFlag),
); err != nil {