Skip to content

Commit

Permalink
feat(check): show until field with remaining time / size before check…
Browse files Browse the repository at this point in the history
… passes
  • Loading branch information
l3uddz committed Jul 12, 2020
1 parent 022671d commit c4c8278
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
6 changes: 3 additions & 3 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ var uploadCmd = &cobra.Command{
// check if upload criteria met
if !flagNoCheck {
// no check was not enabled
if shouldUpload, err := upload.Check(); err != nil {
if res, err := upload.Check(); err != nil {
upload.Log.WithError(err).Error("Failed checking if uploader check conditions met, skipping...")
continue
} else if !shouldUpload {
upload.Log.Info("Upload conditions not met, skipping...")
} else if !res.Passed {
upload.Log.WithField("until", res.Info).Info("Upload conditions not met, skipping...")
continue
}
}
Expand Down
2 changes: 1 addition & 1 deletion uploader/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
}
)

func (u *Uploader) Check() (bool, error) {
func (u *Uploader) Check() (*checker.Result, error) {
// Perform the check
return u.Checker.Check(&u.Config.Check, u.Log, u.LocalFiles, u.LocalFilesSize)
}
Expand Down
16 changes: 14 additions & 2 deletions uploader/checker/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,29 @@ import (

type Age struct{}

func (Age) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (bool, error) {
func (Age) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (*Result, error) {
var checkPassed bool
var filesPassed int
var filesSize int64

oldestFile := time.Now()

// Check File Ages
maxFileAge := time.Now().Add(time.Duration(-cfg.Limit) * time.Minute)

for _, path := range paths {
path := path

// skip directories
if path.IsDir {
continue
}

// set oldestFile
if oldestFile.IsZero() || path.ModifiedTime.Before(oldestFile) {
oldestFile = path.ModifiedTime
}

// was this file modified after our max file age?
if path.ModifiedTime.Before(maxFileAge) {
filesPassed++
Expand All @@ -49,7 +58,10 @@ func (Age) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils
}).Info("Local files matching check criteria")
}

return checkPassed, nil
return &Result{
Passed: checkPassed,
Info: humanize.RelTime(oldestFile, maxFileAge, "", ""),
}, nil
}

func (Age) CheckFile(cfg *config.UploaderCheck, log *logrus.Entry, path pathutils.Path, size uint64) (bool, error) {
Expand Down
7 changes: 6 additions & 1 deletion uploader/checker/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import (
)

type Interface interface {
Check(*config.UploaderCheck, *logrus.Entry, []pathutils.Path, uint64) (bool, error)
Check(*config.UploaderCheck, *logrus.Entry, []pathutils.Path, uint64) (*Result, error)
CheckFile(*config.UploaderCheck, *logrus.Entry, pathutils.Path, uint64) (bool, error)
RcloneParams(check *config.UploaderCheck, entry *logrus.Entry) []string
}

type Result struct {
Passed bool
Info interface{}
}
18 changes: 13 additions & 5 deletions uploader/checker/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ import (

type Size struct{}

func (Size) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (bool, error) {
func (Size) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (*Result, error) {
// Check Total Size
s := humanize.Bytes(size)

if size > cfg.Limit {
log.WithFields(logrus.Fields{
"max_size": humanize.Bytes(cfg.Limit),
"current_size": humanize.Bytes(size),
"current_size": s,
"over_size": humanize.Bytes(size - cfg.Limit),
}).Info("Size is greater than specified limit")
return true, nil
return &Result{
Passed: true,
Info: s,
}, nil
} else {
return &Result{
Passed: false,
Info: humanize.Bytes(cfg.Limit - size),
}, nil
}

return false, nil
}

func (Size) CheckFile(cfg *config.UploaderCheck, log *logrus.Entry, path pathutils.Path, size uint64) (bool, error) {
Expand Down

0 comments on commit c4c8278

Please sign in to comment.