Skip to content

Commit

Permalink
feat: write output as plain text
Browse files Browse the repository at this point in the history
  • Loading branch information
michalczmiel committed Jan 22, 2024
1 parent a7e0315 commit 8eba8eb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
8 changes: 4 additions & 4 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ func runFileCmd(cmd *cobra.Command, args []string) error {
return err
}

err = internal.DownloadImages(links, parameters)
if err != nil {
return err
}
results := internal.DownloadImages(links, parameters)

printer := internal.NewStdoutPrinter()
printer.PrintResultsAsPlainText(results)

return nil
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ func runHtmlCmd(cmd *cobra.Command, args []string) error {
return err
}

err = internal.DownloadImages(links, parameters)
if err != nil {
return err
}
results := internal.DownloadImages(links, parameters)
printer := internal.NewStdoutPrinter()
printer.PrintResultsAsPlainText(results)

return nil

}

func init() {
Expand Down
11 changes: 4 additions & 7 deletions internal/images.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package internal

import (
"fmt"
"path"
"sync"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ type DownloadResult struct {
Err error
}

func DownloadImages(links []string, parameters Parameters) error {
func DownloadImages(links []string, parameters Parameters) []DownloadResult {
linksToProcess := make(chan string, len(links))
results := make(chan DownloadResult, len(links))

Expand All @@ -63,11 +62,9 @@ func DownloadImages(links []string, parameters Parameters) error {
close(results)
}()

resultsOutput := []DownloadResult{}
for result := range results {
if result.Err != nil {
fmt.Printf("error downloading %s : %v\n", result.Url, result.Err)
}
resultsOutput = append(resultsOutput, result)
}

return nil
return resultsOutput
}
31 changes: 31 additions & 0 deletions internal/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package internal

import (
"fmt"
"io"
"os"
)

type Printer struct {
writer io.Writer
}

func NewStdoutPrinter() *Printer {
return &Printer{writer: os.Stdout}
}

func (p *Printer) PrintResultsAsPlainText(results []DownloadResult) {
successfulDownloads := 0
failedDownloads := 0

for _, result := range results {
if result.Err != nil {
failedDownloads++
} else {
successfulDownloads++
}
}

fmt.Fprintf(p.writer, "Successful downloads: %d\n", successfulDownloads)
fmt.Fprintf(p.writer, "Failed downloads: %d\n", failedDownloads)
}
30 changes: 30 additions & 0 deletions internal/printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package internal

import (
"bytes"
"fmt"
"testing"
)

func TestPrintResultsAsPlainText(t *testing.T) {
var buf bytes.Buffer

printer := Printer{writer: &buf}

results := []DownloadResult{
{Url: "https://example.com/image1.jpg", Err: nil},
{Url: "https://example.com/image2.jpg", Err: fmt.Errorf("invalid content type")},
{Url: "https://example.com/image3.jpg", Err: fmt.Errorf("error downloading image")},
{Url: "https://example.com/image4.jpg", Err: nil},
}

printer.PrintResultsAsPlainText(results)

got := buf.String()

expected := "Successful downloads: 2\nFailed downloads: 2\n"

if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}

0 comments on commit 8eba8eb

Please sign in to comment.