Skip to content

Commit c4c8278

Browse files
committed
feat(check): show until field with remaining time / size before check passes
1 parent 022671d commit c4c8278

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

cmd/upload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ var uploadCmd = &cobra.Command{
9898
// check if upload criteria met
9999
if !flagNoCheck {
100100
// no check was not enabled
101-
if shouldUpload, err := upload.Check(); err != nil {
101+
if res, err := upload.Check(); err != nil {
102102
upload.Log.WithError(err).Error("Failed checking if uploader check conditions met, skipping...")
103103
continue
104-
} else if !shouldUpload {
105-
upload.Log.Info("Upload conditions not met, skipping...")
104+
} else if !res.Passed {
105+
upload.Log.WithField("until", res.Info).Info("Upload conditions not met, skipping...")
106106
continue
107107
}
108108
}

uploader/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var (
1111
}
1212
)
1313

14-
func (u *Uploader) Check() (bool, error) {
14+
func (u *Uploader) Check() (*checker.Result, error) {
1515
// Perform the check
1616
return u.Checker.Check(&u.Config.Check, u.Log, u.LocalFiles, u.LocalFilesSize)
1717
}

uploader/checker/age.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,29 @@ import (
1212

1313
type Age struct{}
1414

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

20+
oldestFile := time.Now()
21+
2022
// Check File Ages
2123
maxFileAge := time.Now().Add(time.Duration(-cfg.Limit) * time.Minute)
2224

2325
for _, path := range paths {
26+
path := path
27+
2428
// skip directories
2529
if path.IsDir {
2630
continue
2731
}
2832

33+
// set oldestFile
34+
if oldestFile.IsZero() || path.ModifiedTime.Before(oldestFile) {
35+
oldestFile = path.ModifiedTime
36+
}
37+
2938
// was this file modified after our max file age?
3039
if path.ModifiedTime.Before(maxFileAge) {
3140
filesPassed++
@@ -49,7 +58,10 @@ func (Age) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils
4958
}).Info("Local files matching check criteria")
5059
}
5160

52-
return checkPassed, nil
61+
return &Result{
62+
Passed: checkPassed,
63+
Info: humanize.RelTime(oldestFile, maxFileAge, "", ""),
64+
}, nil
5365
}
5466

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

uploader/checker/interface.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import (
77
)
88

99
type Interface interface {
10-
Check(*config.UploaderCheck, *logrus.Entry, []pathutils.Path, uint64) (bool, error)
10+
Check(*config.UploaderCheck, *logrus.Entry, []pathutils.Path, uint64) (*Result, error)
1111
CheckFile(*config.UploaderCheck, *logrus.Entry, pathutils.Path, uint64) (bool, error)
1212
RcloneParams(check *config.UploaderCheck, entry *logrus.Entry) []string
1313
}
14+
15+
type Result struct {
16+
Passed bool
17+
Info interface{}
18+
}

uploader/checker/size.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,26 @@ import (
1010

1111
type Size struct{}
1212

13-
func (Size) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (bool, error) {
13+
func (Size) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (*Result, error) {
1414
// Check Total Size
15+
s := humanize.Bytes(size)
16+
1517
if size > cfg.Limit {
1618
log.WithFields(logrus.Fields{
1719
"max_size": humanize.Bytes(cfg.Limit),
18-
"current_size": humanize.Bytes(size),
20+
"current_size": s,
1921
"over_size": humanize.Bytes(size - cfg.Limit),
2022
}).Info("Size is greater than specified limit")
21-
return true, nil
23+
return &Result{
24+
Passed: true,
25+
Info: s,
26+
}, nil
27+
} else {
28+
return &Result{
29+
Passed: false,
30+
Info: humanize.Bytes(cfg.Limit - size),
31+
}, nil
2232
}
23-
24-
return false, nil
2533
}
2634

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

0 commit comments

Comments
 (0)