|
|
|
@@ -1,69 +1,53 @@
|
|
|
|
|
// packer - a simple CLI tool to find and zip file corresponding to a given regex
|
|
|
|
|
// the archive keeps the folder structure
|
|
|
|
|
// find + zip linux CLI works too
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"archive/zip"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/akamensky/argparse"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
fp "path/filepath"
|
|
|
|
|
re "regexp"
|
|
|
|
|
|
|
|
|
|
"github.com/akamensky/argparse"
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// WritterError
|
|
|
|
|
// Useless, just to play with `errors.As()` to check specific error
|
|
|
|
|
type WritterError struct {
|
|
|
|
|
message error
|
|
|
|
|
var ErrEmptyRegex = errors.New("empty regex after build")
|
|
|
|
|
|
|
|
|
|
func initLogger() {
|
|
|
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
|
|
|
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ErrorCode interface {
|
|
|
|
|
Code() int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (we WritterError) Error() string {
|
|
|
|
|
return fmt.Sprintf("error occured while creating archive : %s", we.message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (we WritterError) Code() int {
|
|
|
|
|
return 500
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseArguments - get needed arguments to launch the `packer`
|
|
|
|
|
// returns 3 strings :
|
|
|
|
|
// * regex
|
|
|
|
|
// * outputPath - full path of the zip archive
|
|
|
|
|
// * directory - which directory to check
|
|
|
|
|
// * remove - delete all file after creating zip archive
|
|
|
|
|
// parseArguments parses CLI arguments to launch `packer`.
|
|
|
|
|
func parseArguments() (string, string, string, bool, error) {
|
|
|
|
|
parser := argparse.NewParser("packer", "packer - zip file where filenames match with a regex")
|
|
|
|
|
var err error
|
|
|
|
|
parser := argparse.NewParser("packer", "zip files where filenames match a regex")
|
|
|
|
|
|
|
|
|
|
regex := parser.String("r", "regex", &argparse.Options{Required: true, Help: "filename Regex"})
|
|
|
|
|
outputPath := parser.String("o", "output-path", &argparse.Options{Required: true, Help: "zip output path (including zip name)"})
|
|
|
|
|
directory := parser.String("d", "directory", &argparse.Options{Required: false, Help: "root directory to check files", Default: "."})
|
|
|
|
|
remove := parser.Flag("x", "remove", &argparse.Options{Required: false, Help: "remove files after archive creation", Default: false})
|
|
|
|
|
|
|
|
|
|
parseError := parser.Parse(os.Args)
|
|
|
|
|
if parseError != nil {
|
|
|
|
|
err = fmt.Errorf("parsing error: %s", parseError)
|
|
|
|
|
if err := parser.Parse(os.Args); err != nil {
|
|
|
|
|
return "", "", "", false, fmt.Errorf("parsing error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *regex, *directory, *outputPath, *remove, err
|
|
|
|
|
return *regex, *directory, *outputPath, *remove, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buildRegex - build POSIX regex from a string
|
|
|
|
|
// WARN: lookahead and lookbehind are not supported
|
|
|
|
|
func buildRegex(regex string) (*re.Regexp, error) {
|
|
|
|
|
rc, err := re.CompilePOSIX(regex)
|
|
|
|
|
return rc, err
|
|
|
|
|
// buildRegex builds POSIX regex from a string.
|
|
|
|
|
// WARN: lookahead and lookbehind are not supported.
|
|
|
|
|
func buildRegex(regex string) (re.Regexp, error) {
|
|
|
|
|
rc, err := re.Compile(regex)
|
|
|
|
|
if rc == nil {
|
|
|
|
|
return re.Regexp{}, ErrEmptyRegex
|
|
|
|
|
}
|
|
|
|
|
return *rc, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// walkFilteredDir - walk into a directory and its subdirectories and find files that match a regex
|
|
|
|
|
// walkFilteredDir walks into a directory and its subdirectories and find files that match a regex.
|
|
|
|
|
func walkDirFiltered(directory string, regex re.Regexp) ([]string, error) {
|
|
|
|
|
var files []string
|
|
|
|
|
err := fp.Walk(directory,
|
|
|
|
@@ -72,7 +56,7 @@ func walkDirFiltered(directory string, regex re.Regexp) ([]string, error) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !info.IsDir() && regex.MatchString(path) {
|
|
|
|
|
log.Println("file found :", path)
|
|
|
|
|
log.Info().Str("path", path).Msg("file found")
|
|
|
|
|
files = append(files, path)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
@@ -80,21 +64,15 @@ func walkDirFiltered(directory string, regex re.Regexp) ([]string, error) {
|
|
|
|
|
return files, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getFilesFromDirectoryPath - wrapper calling `walkDirFiltered()`
|
|
|
|
|
func getFilesFromDirectoryPath(directory string, regex re.Regexp) ([]string, error) {
|
|
|
|
|
files, err := walkDirFiltered(directory, regex)
|
|
|
|
|
return files, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writeFileContent - write file content into zipped file
|
|
|
|
|
// chunking is used to avoid memory errors with large files
|
|
|
|
|
// writeFileContent writes file content into zipped file (chunking is used to avoid memory errors with large files).
|
|
|
|
|
func writeFileContent(f *os.File, zf io.Writer) (err error) {
|
|
|
|
|
const chunkSize = 4
|
|
|
|
|
|
|
|
|
|
b := make([]byte, chunkSize)
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
_, err = f.Read(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if _, err = f.Read(b); err != nil {
|
|
|
|
|
if err != io.EOF {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
@@ -106,9 +84,7 @@ func writeFileContent(f *os.File, zf io.Writer) (err error) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// generateZip - create a zip archive
|
|
|
|
|
// * outputPath: zip archive full path
|
|
|
|
|
// * filePaths: files path
|
|
|
|
|
// generateZip creates a zip archive from an output path and filepaths slice.
|
|
|
|
|
func generateZip(outputPath string, filepaths []string) error {
|
|
|
|
|
|
|
|
|
|
// create a zipfile from `outputPath`
|
|
|
|
@@ -125,14 +101,14 @@ func generateZip(outputPath string, filepaths []string) error {
|
|
|
|
|
// open matching regex file
|
|
|
|
|
f, openErr := os.Open(filepath)
|
|
|
|
|
if openErr != nil {
|
|
|
|
|
log.Println("[ERROR] :", openErr)
|
|
|
|
|
log.Err(openErr)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a zipped file
|
|
|
|
|
zf, zipErr := w.Create(filepath)
|
|
|
|
|
if zipErr != nil {
|
|
|
|
|
log.Println("[ERROR] :", zipErr)
|
|
|
|
|
log.Err(zipErr)
|
|
|
|
|
f.Close()
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
@@ -141,7 +117,7 @@ func generateZip(outputPath string, filepaths []string) error {
|
|
|
|
|
writeErr := writeFileContent(f, zf)
|
|
|
|
|
// catch sentinel error `io.EOF`
|
|
|
|
|
if writeErr != nil && writeErr != io.EOF {
|
|
|
|
|
log.Println("[ERROR] :", writeErr)
|
|
|
|
|
log.Err(zipErr)
|
|
|
|
|
f.Close()
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
@@ -151,69 +127,60 @@ func generateZip(outputPath string, filepaths []string) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make sure to check the error on Close.
|
|
|
|
|
closeErr := w.Close()
|
|
|
|
|
if closeErr != nil {
|
|
|
|
|
return WritterError{message: fmt.Errorf("error occured while closing the zip archive : %w", closeErr)}
|
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
|
return fmt.Errorf("error occured while closing the zip archive : %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// removeFiles - can't be more obvious...
|
|
|
|
|
func removeFiles(filepaths []string) {
|
|
|
|
|
for _, filepath := range filepaths {
|
|
|
|
|
err := os.Remove(filepath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("[ERROR] :", err)
|
|
|
|
|
if err := os.Remove(filepath); err != nil {
|
|
|
|
|
log.Err(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
initLogger()
|
|
|
|
|
|
|
|
|
|
// parse arguments
|
|
|
|
|
regex, directory, outputPath, remove, err := parseArguments()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("error while parsing args : ", err)
|
|
|
|
|
log.Fatal().Err(err).Msg("error while parsing args")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build regex from string
|
|
|
|
|
rc, err := buildRegex(regex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("error while compiling regex : '", regex, "'\nWARN: lookahead and lookbehind not implemented")
|
|
|
|
|
log.Fatal().Err(err).Str("regex", regex).Msg("error while compiling regex")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deferencing Regexp pointer
|
|
|
|
|
rd := *rc
|
|
|
|
|
|
|
|
|
|
// walk into directory
|
|
|
|
|
log.Printf("looking for file '%s' in '%s' ...", rd.String(), directory)
|
|
|
|
|
files, err := getFilesFromDirectoryPath(directory, rd)
|
|
|
|
|
log.Info().Str("regex", rc.String()).Str("directory", directory).Msg("looking for files...")
|
|
|
|
|
|
|
|
|
|
log.Printf("files found : %d", len(files))
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
|
message := fmt.Sprintf("no file found in the directory : %s with the regex : %s", directory, regex)
|
|
|
|
|
log.Fatal(message)
|
|
|
|
|
files, err := walkDirFiltered(directory, rc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
|
log.Warn().Str("regex", rc.String()).Str("directory", directory).Msg("no file found in the directory")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info().Int("len", len(files)).Msg("files found")
|
|
|
|
|
|
|
|
|
|
// generating zip archive
|
|
|
|
|
err = generateZip(outputPath, files)
|
|
|
|
|
|
|
|
|
|
// check if errors occured during zip
|
|
|
|
|
var errorCode interface {
|
|
|
|
|
Code() int
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.As(err, &errorCode) {
|
|
|
|
|
log.Fatal("zip archive writing error : ", err)
|
|
|
|
|
}
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
if err := generateZip(outputPath, files); err != nil {
|
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if remove {
|
|
|
|
|
log.Printf("clean up archive files...")
|
|
|
|
|
log.Info().Msg("cleaning up archive files...")
|
|
|
|
|
removeFiles(files)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("zip archive generated in %s", outputPath)
|
|
|
|
|
log.Info().Str("output path", outputPath).Msg("zip archive generated")
|
|
|
|
|
}
|
|
|
|
|