Browse Source

Add progress bar and increase ignored files

pull/1/head
Tovi Jaeschke-Rogers 3 years ago
parent
commit
c6ac839418
6 changed files with 164 additions and 66 deletions
  1. +10
    -8
      Client/Filesystem/CommitFiles.go
  2. +50
    -36
      Client/Filesystem/FilesystemDiff.go
  3. +20
    -22
      Client/Filesystem/PickFiles.go
  4. +24
    -0
      Client/Filesystem/Print.go
  5. +41
    -0
      Client/ProgressBar/Bar.go
  6. +19
    -0
      Variables/Variables.go

+ 10
- 8
Client/Filesystem/CommitFiles.go View File

@ -2,9 +2,10 @@ package Filesystem
import ( import (
"PackageManager/Client/Database" "PackageManager/Client/Database"
"PackageManager/Client/ProgressBar"
"PackageManager/Variables" "PackageManager/Variables"
"fmt"
"github.com/vbauerster/mpb"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
@ -12,6 +13,7 @@ func CommitFiles() error {
var ( var (
fsStatus FilesystemStatus fsStatus FilesystemStatus
indexBucket *bolt.Bucket indexBucket *bolt.Bucket
bar *mpb.Bar
f string f string
e error e error
) )
@ -25,20 +27,20 @@ func CommitFiles() error {
indexBucket = tx.Bucket(Variables.FsHashIndexBucket) indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
if len(fsStatus.PickedFiles) > 0 { if len(fsStatus.PickedFiles) > 0 {
bar = ProgressBar.InitBar("Commiting...", len(fsStatus.PickedFiles))
for _, f = range fsStatus.PickedFiles { for _, f = range fsStatus.PickedFiles {
fmt.Println(f)
e = AddFileToBucket(indexBucket, f) e = AddFileToBucket(indexBucket, f)
bar.Increment()
if e != nil { if e != nil {
return e return e
} }
} }
/*
e = tx.DeleteBucket(Variables.FsHashPicksBucket)
if e != nil {
return e
}
*/
e = tx.DeleteBucket(Variables.FsHashPicksBucket)
if e != nil {
return e
}
ProgressBar.CloseBar(bar)
} }
return nil return nil


+ 50
- 36
Client/Filesystem/FilesystemDiff.go View File

@ -2,6 +2,7 @@ package Filesystem
import ( import (
"PackageManager/Client/Database" "PackageManager/Client/Database"
"PackageManager/Color"
"PackageManager/Variables" "PackageManager/Variables"
"fmt" "fmt"
"log" "log"
@ -30,47 +31,60 @@ func ShowFilesystemDiff(root string) error {
return e return e
} }
if len(fsStatus.NewFiles) > 0 {
fmt.Printf("New files: %d\n", len(fsStatus.NewFiles))
/*
fmt.Println("New files:")
for _, f = range fsStatus.NewFiles {
fmt.Printf("\t%s\n", Color.Green(f))
}
*/
}
fmt.Println("New files:")
PrintFilesOrLength(fsStatus.NewFiles, Color.Green)
if len(fsStatus.PickedFiles) > 0 {
fmt.Printf("Picked files: %d\n", len(fsStatus.PickedFiles))
/*
fmt.Println("Added files:")
for _, f = range fsStatus.PickedFiles {
fmt.Printf("\t%s\n", Color.Green(f))
}
*/
}
fmt.Println("Added files:")
PrintFilesOrLength(fsStatus.PickedFiles, Color.Green)
if len(fsStatus.ModifiedFiles) > 0 {
fmt.Printf("Modified files: %d\n", len(fsStatus.ModifiedFiles))
/*
fmt.Println("Modified files:")
for _, f = range fsStatus.ModifiedFiles {
fmt.Printf("\t%s\n", Color.Green(f))
}
*/
fmt.Println("Modified files:")
PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning)
fmt.Println("Deleted files:")
PrintFilesOrLength(fsStatus.MissingFiles, Color.Fatal)
return nil
}
func GetFilesystemLength(root string) (int, error) {
var (
rootStat os.FileInfo
fsCount int = 0
e error
)
rootStat, e = os.Stat(root)
if e != nil {
return fsCount, e
} }
if len(fsStatus.MissingFiles) > 0 {
fmt.Printf("Modified files: %d\n", len(fsStatus.MissingFiles))
/*
fmt.Println("Deleted files:")
for _, f = range fsStatus.MissingFiles {
fmt.Printf("\t%s\n", Color.Green(f))
}
*/
if rootStat.IsDir() && root[len(root)-1:] != "/" {
root = root + "/"
} }
return nil
filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {
// Ignore path in Variables.PruneRegexPaths
if i.IsDir() && matchAny(p, PruneRegex) {
log.Println("Prune", p)
return filepath.SkipDir
}
// Ignore path in Variables.IgnoreRegexPaths
if matchAny(p, IgnoreRegex) {
return nil
}
if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) {
return nil
}
fsCount++
return nil
})
return fsCount, e
} }
func GetFilesystemDiff(root string) (FilesystemStatus, error) { func GetFilesystemDiff(root string) (FilesystemStatus, error) {
@ -102,7 +116,7 @@ func GetFilesystemDiff(root string) (FilesystemStatus, error) {
e = Database.FsDB.View(func(tx *bolt.Tx) error { e = Database.FsDB.View(func(tx *bolt.Tx) error {
picksBucket = tx.Bucket(Variables.FsHashPicksBucket) picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
indexBucket = tx.Bucket(Variables.FsHashPicksBucket)
indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
filepath.Walk(root, func(p string, i os.FileInfo, _ error) error { filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {


+ 20
- 22
Client/Filesystem/PickFiles.go View File

@ -2,8 +2,10 @@ package Filesystem
import ( import (
"PackageManager/Client/Database" "PackageManager/Client/Database"
"PackageManager/Client/ProgressBar"
"PackageManager/Variables" "PackageManager/Variables"
"github.com/vbauerster/mpb"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
@ -11,6 +13,8 @@ func PickFiles(rootPath string) error {
var ( var (
fsStatus FilesystemStatus fsStatus FilesystemStatus
picksBucket *bolt.Bucket picksBucket *bolt.Bucket
totalLen int
bar *mpb.Bar
f string f string
e error e error
) )
@ -20,12 +24,21 @@ func PickFiles(rootPath string) error {
return e return e
} }
totalLen = len(fsStatus.NewFiles) + len(fsStatus.ModifiedFiles) + len(fsStatus.MissingFiles)
if totalLen == 0 {
return nil
}
bar = ProgressBar.InitBar("Adding...", totalLen)
e = Database.FsDB.Batch(func(tx *bolt.Tx) error { e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
picksBucket = tx.Bucket(Variables.FsHashPicksBucket) picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
if len(fsStatus.NewFiles) > 0 { if len(fsStatus.NewFiles) > 0 {
for _, f = range fsStatus.NewFiles { for _, f = range fsStatus.NewFiles {
e = AddFileToBucket(picksBucket, f) e = AddFileToBucket(picksBucket, f)
bar.Increment()
if e != nil { if e != nil {
return e return e
} }
@ -33,8 +46,9 @@ func PickFiles(rootPath string) error {
} }
if len(fsStatus.ModifiedFiles) > 0 { if len(fsStatus.ModifiedFiles) > 0 {
for _, f = range fsStatus.NewFiles {
for _, f = range fsStatus.ModifiedFiles {
e = AddFileToBucket(picksBucket, f) e = AddFileToBucket(picksBucket, f)
bar.Increment()
if e != nil { if e != nil {
return e return e
} }
@ -44,6 +58,7 @@ func PickFiles(rootPath string) error {
if len(fsStatus.MissingFiles) > 0 { if len(fsStatus.MissingFiles) > 0 {
for _, f = range fsStatus.NewFiles { for _, f = range fsStatus.NewFiles {
e = RemoveFileFromBucket(picksBucket, f) e = RemoveFileFromBucket(picksBucket, f)
bar.Increment()
if e != nil { if e != nil {
return e return e
} }
@ -53,35 +68,18 @@ func PickFiles(rootPath string) error {
return nil return nil
}) })
ProgressBar.CloseBar(bar)
return e return e
} }
func ResetAllPickedFiles() error { func ResetAllPickedFiles() error {
var ( var (
fsStatus FilesystemStatus
picksBucket *bolt.Bucket
f string
e error
e error
) )
fsStatus, e = GetFilesystemDiff(Variables.RootDir)
if e != nil {
return e
}
e = Database.FsDB.Batch(func(tx *bolt.Tx) error { e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
if len(fsStatus.PickedFiles) > 0 {
for _, f = range fsStatus.PickedFiles {
e = RemoveFileFromBucket(picksBucket, f)
if e != nil {
return e
}
}
}
return nil
return tx.DeleteBucket(Variables.FsHashPicksBucket)
}) })
return e return e


+ 24
- 0
Client/Filesystem/Print.go View File

@ -0,0 +1,24 @@
package Filesystem
import (
"fmt"
)
func PrintFiles(files []string, color func(...interface{}) string) {
var f string
for _, f = range files {
fmt.Printf("\t%s\n", color(f))
}
}
func PrintFilesLength(files []string) {
fmt.Printf("\t%d files found\n", len(files))
}
func PrintFilesOrLength(files []string, color func(...interface{}) string) {
if len(files) < 25 && len(files) > 0 {
PrintFiles(files, color)
return
}
PrintFilesLength(files)
}

+ 41
- 0
Client/ProgressBar/Bar.go View File

@ -0,0 +1,41 @@
package ProgressBar
import (
"sync"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
var (
BarWG sync.WaitGroup
P = mpb.New(mpb.WithWaitGroup(&BarWG))
)
func InitBar(name string, total int) *mpb.Bar {
var (
bar *mpb.Bar
)
BarWG.Add(1)
bar = P.AddBar(int64(total),
mpb.PrependDecorators(
decor.Name(name),
decor.Percentage(decor.WCSyncSpace),
),
mpb.AppendDecorators(
// replace ETA decorator with "done" message, OnComplete event
decor.OnComplete(
decor.AverageETA(decor.ET_STYLE_GO, decor.WC{W: 4}), "done",
),
),
)
return bar
}
func CloseBar(bar *mpb.Bar) {
bar.Abort(false)
BarWG.Done()
}

+ 19
- 0
Variables/Variables.go View File

@ -27,6 +27,21 @@ var (
"^/home$", "^/home$",
"^/build$", "^/build$",
"^/tools$", "^/tools$",
"^/run/user$",
"^/usr/share/zoneinfo-leaps$",
"^/usr/share/zoneinfo$",
"^/usr/share/zsh$",
"^/tmp$",
"^/var/db$",
"^/var/cache$",
"^/var/log$",
"^/var/spool$",
"^/var/lib/texmf$",
"^/var/lib/postgres$",
"^/var/lib/pacman$",
"^/var/lib/NetworkManager$",
"^/var/lib/systemd$",
"^/var/lib/xkb/README.compiled$",
"/lost\\+found$", "/lost\\+found$",
} }
@ -35,6 +50,10 @@ var (
"^/etc/passwd-$", "^/etc/passwd-$",
"^/etc/group$", "^/etc/group$",
"^/etc/group-$", "^/etc/group-$",
"^/var/.updated$",
"^/var/lib/mlocate/mlocate.db$",
"^/var/lib/krb5kdc/kdc.conf$",
"^/var/lib/alsa/asound.state$",
} }
) )


Loading…
Cancel
Save