Skip to content

Commit

Permalink
Added a progress bar and removed debug prints
Browse files Browse the repository at this point in the history
  • Loading branch information
Astrak00 committed Oct 1, 2024
1 parent 28471fd commit e662dfe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ go 1.22.4

require (
github.com/fatih/color v1.17.0
github.com/schollz/progressbar/v3 v3.16.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.16.0 h1:+MbBim/cE9DqDb8UXRfLJ6RZdyDkXG1BDy/sWc5s0Mc=
github.com/schollz/progressbar/v3 v3.16.0/go.mod h1:lLiKjKJ9/yzc9Q8jk+sVLfxWxgXKsktvUf6TO+4Y2nw=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -19,5 +25,7 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
52 changes: 48 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"

"github.com/fatih/color"
"github.com/schollz/progressbar/v3"
)

const (
Expand Down Expand Up @@ -305,7 +307,6 @@ func parseFlags() (int, string, string, int) {
dir := flag.String("d", "courses", "Introduce the directory where you want to save the files")
cores := flag.Int("c", runtime.NumCPU()-1, "Introduce the cores to use in the download")
flag.Parse()
fmt.Println("Cores:", *cores)

if *language == 0 {
fmt.Println("Introduce tu idioma: 1: Español, 2:English")
Expand Down Expand Up @@ -344,19 +345,62 @@ func promptForToken(language int) string {
func downloadFiles(filesStoreChan <-chan FileStore, maxGoroutines int) {
var wg sync.WaitGroup
semaphore := make(chan struct{}, maxGoroutines)
totalFiles := len(filesStoreChan)

// Create an atomic counter for completed files
var completedFiles int32

bar := progressbar.NewOptions(totalFiles,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowBytes(false),
progressbar.OptionSetWidth(20),
progressbar.OptionSetPredictTime(false),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))

for fileStore := range filesStoreChan {
wg.Add(1)
go func(fileStore FileStore) {
defer wg.Done()
semaphore <- struct{}{}
defer func() { <-semaphore }()

if err := downloadFile(fileStore); err != nil {
if err := downloadFileWithProgress(fileStore, bar, &completedFiles); err != nil {
fmt.Printf("Error downloading file %s: %v\n", fileStore.FileName, err)
}
}(fileStore)
}

wg.Wait()
}

func downloadFileWithProgress(fileStore FileStore, bar *progressbar.ProgressBar, completedFiles *int32) error {
resp, err := http.Get(fileStore.FileURL)
if err != nil {
return fmt.Errorf("error downloading the file: %v", err)
}
defer resp.Body.Close()

dir := filepath.Dir(fileStore.Dir)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return fmt.Errorf("error creating the directory: %v", err)
}

out, err := os.Create(fileStore.Dir)
if err != nil {
return fmt.Errorf("error creating the file: %v", err)
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("error copying the file: %v", err)
}

atomic.AddInt32(completedFiles, 1)
bar.Add(1)
return nil
}

0 comments on commit e662dfe

Please sign in to comment.