Add progress bar

main
Mari 4 years ago
parent e3c99e23b9
commit 164d33c27f
  1. 29
      drive-demuxer.go

@ -8,6 +8,8 @@ import (
"os" "os"
"path" "path"
"sync" "sync"
"github.com/cheggaaa/pb/v3"
) )
type TopLevel struct { type TopLevel struct {
@ -18,6 +20,7 @@ type TopLevel struct {
RightOutput string RightOutput string
OpenHandles chan int OpenHandles chan int
DryRun bool DryRun bool
Bar *pb.ProgressBar
} }
type Step struct { type Step struct {
@ -116,10 +119,6 @@ func (s *Step) SeparateRight(child string) {
rightInPath := s.InputPath(child, RIGHT) rightInPath := s.InputPath(child, RIGHT)
rightOutPath := s.OutputPath(child, RIGHT) rightOutPath := s.OutputPath(child, RIGHT)
if s.DryRun {
return
}
rightBase := path.Dir(rightOutPath) rightBase := path.Dir(rightOutPath)
err := os.MkdirAll(rightBase, 0755) err := os.MkdirAll(rightBase, 0755)
if err != nil { if err != nil {
@ -136,10 +135,6 @@ func (s *Step) Combine(child string) {
rightInPath := s.InputPath(child, RIGHT) rightInPath := s.InputPath(child, RIGHT)
combinedOutPath := s.OutputPath(child, COMBINED) combinedOutPath := s.OutputPath(child, COMBINED)
if s.DryRun {
return
}
combinedBase := path.Dir(combinedOutPath) combinedBase := path.Dir(combinedOutPath)
err := os.MkdirAll(combinedBase, 0755) err := os.MkdirAll(combinedBase, 0755)
if err != nil { if err != nil {
@ -279,17 +274,22 @@ func (s *Step) AreFilesIdentical(child string) bool {
func (s *Step) Walk() { func (s *Step) Walk() {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
for _, child := range s.ListChildren() { children := s.ListChildren()
s.Bar.AddTotal(int64(len(children)))
for _, child := range children {
rightPath := s.InputPath(child, RIGHT) rightPath := s.InputPath(child, RIGHT)
rightState, err := s.CheckState(rightPath) rightState, err := s.CheckState(rightPath)
if err != nil { if err != nil {
log.Printf("Error statting path %s: %s\n", rightPath, err) log.Printf("Error statting path %s: %s\n", rightPath, err)
s.Bar.Increment()
continue continue
} else if rightState == UNKNOWN { } else if rightState == UNKNOWN {
log.Printf("Unknown stat value for path %s\n", rightPath) log.Printf("Unknown stat value for path %s\n", rightPath)
s.Bar.Increment()
continue continue
} else if rightState == MISSING { } else if rightState == MISSING {
s.SeparateLeft(child) s.SeparateLeft(child)
s.Bar.Increment()
continue continue
} }
@ -297,12 +297,15 @@ func (s *Step) Walk() {
leftState, err := s.CheckState(leftPath) leftState, err := s.CheckState(leftPath)
if err != nil { if err != nil {
log.Printf("Error statting path %s: %s\n", leftPath, err) log.Printf("Error statting path %s: %s\n", leftPath, err)
s.Bar.Increment()
continue continue
} else if leftState == UNKNOWN { } else if leftState == UNKNOWN {
log.Printf("Unknown stat value for path %s\n", leftPath) log.Printf("Unknown stat value for path %s\n", leftPath)
s.Bar.Increment()
continue continue
} else if leftState == MISSING { } else if leftState == MISSING {
s.SeparateRight(child) s.SeparateRight(child)
s.Bar.Increment()
continue continue
} }
@ -310,8 +313,10 @@ func (s *Step) Walk() {
case FILE: case FILE:
if leftState == FILE && s.AreFilesIdentical(child) { if leftState == FILE && s.AreFilesIdentical(child) {
s.Combine(child) s.Combine(child)
s.Bar.Increment()
} else { } else {
s.Separate(child) s.Separate(child)
s.Bar.Increment()
} }
case DIRECTORY: case DIRECTORY:
if leftState == DIRECTORY { if leftState == DIRECTORY {
@ -329,9 +334,11 @@ func (s *Step) Walk() {
s.OpenHandles <- 1 s.OpenHandles <- 1
substep.Walk() substep.Walk()
s.RemoveInputDirs(child) s.RemoveInputDirs(child)
s.Bar.Increment()
}() }()
} else { } else {
s.Separate(child) s.Separate(child)
s.Bar.Increment()
} }
default: default:
panic("Unexpected state") panic("Unexpected state")
@ -343,7 +350,8 @@ func (s *Step) Walk() {
func main() { func main() {
settings := TopLevel{ settings := TopLevel{
OpenHandles: make(chan int, 300), OpenHandles: make(chan int, 450),
Bar: pb.StartNew(1),
} }
flag.StringVar(&settings.LeftInput, flag.StringVar(&settings.LeftInput,
"left-input", "./input/left", "The name of the left side of the input.") "left-input", "./input/left", "The name of the left side of the input.")
@ -362,4 +370,5 @@ func main() {
TopLevel: &settings, TopLevel: &settings,
Subpath: "", Subpath: "",
}).Walk() }).Walk()
settings.Bar.Increment()
} }

Loading…
Cancel
Save