Christian Schmied
76c2a7be5e
All checks were successful
go/archiver/pipeline/head This commit looks good
45 lines
981 B
Go
45 lines
981 B
Go
package archiver
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func NewErrorOnlyArchiver(conf *Config) func(string, error) {
|
|
return func(filePath string, archiveErr error) {
|
|
if archiveErr != nil {
|
|
err := writeErrorFile(conf, filePath, archiveErr)
|
|
if err != nil {
|
|
logger.Error(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func writeErrorFile(conf *Config, filePath string, archiveErr error) error {
|
|
if archiveErr != nil {
|
|
baseName := filepath.Base(filePath)
|
|
|
|
destFolder := getDestFolder(conf, ErrorFolder)
|
|
globDir := extractGlobbedDir(conf.Folder, filePath)
|
|
if globDir != "" {
|
|
destFolder = filepath.Join(destFolder, globDir)
|
|
}
|
|
|
|
err := os.MkdirAll(destFolder, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f, err := os.OpenFile(filepath.Join(destFolder, baseName+".err"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
_, err = f.Write([]byte(archiveErr.Error() + "\n"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|