Skip to content

Commit

Permalink
Add support fo skipping files by date
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Apr 16, 2020
1 parent 74431cd commit a839b41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
12 changes: 10 additions & 2 deletions tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"go.uber.org/zap"
)

// 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 {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down

0 comments on commit a839b41

Please sign in to comment.