From a839b41ee4fcc91633293622dd171ca9d552b0d6 Mon Sep 17 00:00:00 2001 From: Janez Troha Date: Thu, 16 Apr 2020 14:40:17 +0200 Subject: [PATCH] Add support fo skipping files by date --- main.go | 21 +++++++++++++++++++-- tar.go | 12 ++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index ee5fa14..6703e71 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,11 @@ package main import ( "os" + "time" "github.com/woocart/targz/logger" "github.com/woocart/targz/version" + "go.uber.org/zap" kingpin "gopkg.in/alecthomas/kingpin.v2" ) @@ -14,14 +16,29 @@ var app = kingpin.New("targz", "Compress data to Tar using gzip compression.") var from = app.Arg("from", "Directory or file to compress").Required().ExistingFileOrDir() var to = app.Arg("to", "Where to write to [path or stdout]").Required().String() var exclude = app.Flag("exclude", "Exclude files matching PATTERN, a glob(3)-style wildcard pattern.").Strings() +var afterString = app.Flag("afterDate", "Exclude files created before date (RFC3339).").String() func main() { app.Author("dz0ny") app.Version(version.String()) kingpin.MustParse(app.Parse(os.Args[1:])) + after := time.Unix(0, 0) // from start of counting + if *afterString != "" { + if *afterString == "now" { + after = time.Now() + } else { + afterParsed, err := time.Parse(time.RFC3339, *afterString) + if err != nil { + log.Fatal("Could not parse afterDate", err) + } + after = afterParsed + } + + log.Infow("Only including files create or modified", zap.String("afterDate", after.Format(time.RFC3339))) + } if *to == "stdout" { - err := Tar(*from, os.Stdout, *exclude) + err := Tar(*from, os.Stdout, *exclude, after) if err != nil { log.Fatal(err) } @@ -30,7 +47,7 @@ func main() { if err != nil { log.Fatal(err) } - err = Tar(*from, f, *exclude) + err = Tar(*from, f, *exclude, after) if err != nil { log.Fatal(err) } diff --git a/tar.go b/tar.go index 3f6d933..4b13835 100644 --- a/tar.go +++ b/tar.go @@ -9,6 +9,7 @@ import ( "path/filepath" "regexp" "strings" + "time" "go.uber.org/zap" ) @@ -16,7 +17,7 @@ import ( // Tar takes a source and variable writers and walks 'source' writing each file // found to the tar writer; the purpose for accepting multiple writers is to allow // for multiple outputs (for example a file, or md5 hash) -func Tar(src string, writer io.Writer, excludes []string) error { +func Tar(src string, writer io.Writer, excludes []string, after time.Time) error { source, err := filepath.Abs(src) if err != nil { @@ -52,7 +53,7 @@ func Tar(src string, writer io.Writer, excludes []string) error { for _, exclude := range excludes { if matched, _ := regexp.MatchString(exclude, filePath); matched { - log.Infow("Skipped", zap.String("file", filePath)) + log.Debugw("Skipped", zap.String("file", filePath)) return nil } } @@ -66,6 +67,13 @@ func Tar(src string, writer io.Writer, excludes []string) error { return err } + // To use AccessTime or ChangeTime, specify the Format as PAX or GNU. + // To use sub-second resolution, specify the Format as PAX. + if header.ModTime.Before(after) { + log.Debugw("Skipped due modified-date", zap.String("file", filePath)) + return nil + } + // update the name to correctly reflect the desired destination when untaring header.Name = filePath header.Gid = 0