Skip to content

Commit

Permalink
list improvement.
Browse files Browse the repository at this point in the history
- Use machine's local time zone
- Provide (encrypted) archive size.
  • Loading branch information
jixunmoe committed Sep 20, 2020
1 parent 821840f commit f7892cf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
6 changes: 4 additions & 2 deletions cmd/backup-cli/command-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ func listProjects() {
}
}

var dateFormat = "2006-01-02 15:04:05 (MST)"
var dateFormat = "2006-01-02 15:04:05 @MST"

func listArchive(projectName string) {
archives := backup.GetBackupArchives(projectName)
fmt.Printf("%d version(s) available for %s\n", len(archives), projectName)

for i, a := range archives {
fmt.Printf(" %2d. %s (%s)\n", i+1, a.FileName, time.Unix(a.Time, 0).UTC().Format(dateFormat))
size := a.GetFormattedSize()
date := time.Unix(a.Time, 0).Format(dateFormat)
fmt.Printf(" %2d. %s (%6s, %s)\n", i+1, a.FileName, size, date)
}
}
43 changes: 43 additions & 0 deletions utils/backup/archive-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package backup

import (
"fmt"
"github.com/jixunmoe-go/backups/utils/humanize"
"os"
"path"
"regexp"
"strconv"
)

var reFileName = regexp.MustCompile(`^(\d+)\.bin$`)

type ArchiveInfo struct {
FileName string
Time int64
Project string
}

func createArchiveFromName(project, name string) *ArchiveInfo {
m := reFileName.FindStringSubmatch(name)
ts, err := strconv.ParseInt(m[1], 10, 64)
if err != nil {
return nil
}
return &ArchiveInfo{
FileName: name,
Time: ts,
Project: project,
}
}

func (a *ArchiveInfo) GetPath() string {
return path.Join(GetBackupLocation(a.Project), a.FileName)
}

func (a *ArchiveInfo) GetFormattedSize() string {
fi, err := os.Stat(a.GetPath())
if err != nil {
return fmt.Sprintf("(unknown size: %s)", err)
}
return humanize.ByteCountBinary(fi.Size())
}
28 changes: 0 additions & 28 deletions utils/backup/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,9 @@ package backup

import (
"io/ioutil"
"path"
"regexp"
"sort"
"strconv"
)

var reFileName = regexp.MustCompile(`^(\d+)\.bin$`)

type ArchiveInfo struct {
FileName string
Time int64
Project string
}

func createArchiveFromName(project, name string) *ArchiveInfo {
m := reFileName.FindStringSubmatch(name)
ts, err := strconv.ParseInt(m[1], 10, 64)
if err != nil {
return nil
}
return &ArchiveInfo{
FileName: name,
Time: ts,
Project: project,
}
}

func (a *ArchiveInfo) GetPath() string {
return path.Join(GetBackupLocation(a.Project), a.FileName)
}

func GetBackupProjects() []string {
root := GetBackupLocation(".")
s, err := ioutil.ReadDir(root)
Expand Down
17 changes: 17 additions & 0 deletions utils/humanize/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package humanize

import "fmt"

// https://programming.guide/go/formatting-byte-size-to-human-readable-format.html
func ByteCountBinary(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}

0 comments on commit f7892cf

Please sign in to comment.