diff --git a/internal/console/console.go b/internal/console/console.go index 516942b..d7fa95b 100644 --- a/internal/console/console.go +++ b/internal/console/console.go @@ -29,6 +29,6 @@ func Success(format string, a ...any) string { } func Normal(format string, a ...any) string { - white := color.New(color.FgWhite).SprintFunc() - return white(fmt.Sprintf(format, a...)) + white := color.New(color.FgWhite).SprintFunc() + return white(fmt.Sprintf(format, a...)) } diff --git a/internal/document_parser/current_document.go b/internal/document_parser/current_document.go index e8b5990..8d00396 100644 --- a/internal/document_parser/current_document.go +++ b/internal/document_parser/current_document.go @@ -24,17 +24,17 @@ func NewCurrentDocumentParserFromReader(r io.Reader) *CurrentDocumentParser { } func findTitle(selection *goquery.Selection) string { - var title string - selection.Find("a").EachWithBreak(func(v int, s *goquery.Selection) bool { - if s.Text() != "" && len(s.Text()) > 1 { - title = s.Text() - // Break out of the loop - return false - } - return true - }) - - return title + var title string + selection.Find("a").EachWithBreak(func(v int, s *goquery.Selection) bool { + if s.Text() != "" && len(s.Text()) > 1 { + title = s.Text() + // Break out of the loop + return false + } + return true + }) + + return title } func (cdp *CurrentDocumentParser) GetBookDataFromDocument() []book.Book { @@ -82,7 +82,7 @@ func (cdp *CurrentDocumentParser) GetBookDataFromDocument() []book.Book { } func (cdp *CurrentDocumentParser) getDownloadLinkFromDocument() (string, bool) { - return cdp.doc.Find("#main a").First().Attr("href") + return cdp.doc.Find("#main a").First().Attr("href") } func (cdp *CurrentDocumentParser) getBookTitleFromSelection(selection *goquery.Selection) string { @@ -103,8 +103,8 @@ func (cdp *CurrentDocumentParser) getBookTitleFromSelection(selection *goquery.S func (cdp *CurrentDocumentParser) GetDirectDownloadLink(selectedBook book.Book) string { fmt.Println("Obtaining direct download link") - // TODO Implement retry? - link := selectedBook.Mirrors[0] + // TODO Implement retry? + link := selectedBook.Mirrors[0] resp, err := http.Get(link) defer func(Body io.ReadCloser) { diff --git a/internal/document_parser/document_parser.go b/internal/document_parser/document_parser.go index 321f273..25d028c 100644 --- a/internal/document_parser/document_parser.go +++ b/internal/document_parser/document_parser.go @@ -9,7 +9,6 @@ import ( // } type DocumentParser interface { - GetBookDataFromDocument() []book.Book - GetDownloadLinkFromDocument() (string, bool) + GetBookDataFromDocument() []book.Book + GetDownloadLinkFromDocument() (string, bool) } - diff --git a/internal/document_parser/legacy_document.go b/internal/document_parser/legacy_document.go index 6a4e37d..c7b5d6e 100644 --- a/internal/document_parser/legacy_document.go +++ b/internal/document_parser/legacy_document.go @@ -11,7 +11,7 @@ import ( ) type LegacyDocumentParser struct { - doc *goquery.Document + doc *goquery.Document } func NewLegacyDocumentParser(document *goquery.Document) *LegacyDocumentParser { @@ -19,8 +19,8 @@ func NewLegacyDocumentParser(document *goquery.Document) *LegacyDocumentParser { } func NewLegacyDocumentParserFromReader(r io.Reader) *LegacyDocumentParser { - document, _ := goquery.NewDocumentFromReader(r) - return &LegacyDocumentParser{doc: document} + document, _ := goquery.NewDocumentFromReader(r) + return &LegacyDocumentParser{doc: document} } func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book { @@ -48,7 +48,7 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book { case 9, 10, 11: href, hrefExists := columnSelection.Find("a").Attr("href") if hrefExists { - mirrors = append(mirrors, href) + mirrors = append(mirrors, href) } } }) @@ -68,12 +68,10 @@ func (ldp *LegacyDocumentParser) GetBookDataFromDocument() []book.Book { return books } - -func (ldp *LegacyDocumentParser) getDownloadLinkFromDocument() (string, bool){ - return ldp.doc.Find("#download > ul > li > a").First().Attr("href") +func (ldp *LegacyDocumentParser) getDownloadLinkFromDocument() (string, bool) { + return ldp.doc.Find("#download > ul > li > a").First().Attr("href") } - func getBookTitleFromSelection(selection *goquery.Selection) string { var title string selection.Find("a").Each(func(v int, s *goquery.Selection) { @@ -89,7 +87,6 @@ func getBookTitleFromSelection(selection *goquery.Selection) string { return title } - func GetDirectDownloadLinkFromLegacy(link string) string { fmt.Println("Obtaining direct download link") resp, err := http.Get(link) @@ -105,17 +102,17 @@ func GetDirectDownloadLinkFromLegacy(link string) string { fmt.Println("Error getting response:", err) } - page := NewLegacyDocumentParserFromReader(resp.Body) - // TODO: I think this can be improved - directDownloadLink, exists := page.getDownloadLinkFromDocument() + page := NewLegacyDocumentParserFromReader(resp.Body) + // TODO: I think this can be improved + directDownloadLink, exists := page.getDownloadLinkFromDocument() + + fmt.Println("Direct download link:", directDownloadLink) - fmt.Println("Direct download link:", directDownloadLink) + if exists { + return directDownloadLink + } - if exists { - return directDownloadLink - } - - return "" + return "" } func GetDirectDownloadLinkFromCurrent(link string) string { @@ -133,17 +130,15 @@ func GetDirectDownloadLinkFromCurrent(link string) string { fmt.Println("Error getting response:", err) } - page := NewCurrentDocumentParserFromReader(resp.Body) - // TODO: I think this can be improved - directDownloadLink, exists := page.getDownloadLinkFromDocument() - - fmt.Println("Direct download link:", directDownloadLink) + page := NewCurrentDocumentParserFromReader(resp.Body) + // TODO: I think this can be improved + directDownloadLink, exists := page.getDownloadLinkFromDocument() - if exists { - return directDownloadLink - } - - return "" -} + fmt.Println("Direct download link:", directDownloadLink) + if exists { + return directDownloadLink + } + return "" +} diff --git a/internal/downloader/downloader.go b/internal/downloader/downloader.go index d88a8e3..ce028ae 100644 --- a/internal/downloader/downloader.go +++ b/internal/downloader/downloader.go @@ -15,23 +15,23 @@ import ( ) type Downloader struct { - selectedBook book.Book - directLink string - outputFileDir string + selectedBook book.Book + directLink string + outputFileDir string } func NewDownloader(selectedBook book.Book, directLink string, outputFileDir string) *Downloader { - return &Downloader{ - selectedBook: selectedBook, - directLink: directLink, - outputFileDir: outputFileDir, - } + return &Downloader{ + selectedBook: selectedBook, + directLink: directLink, + outputFileDir: outputFileDir, + } } func (d *Downloader) Download() error { fmt.Println(console.Info("Initializing download ")) - // TODO: implement retry + // TODO: implement retry req, _ := http.NewRequest("GET", d.directLink, nil) resp, error := http.DefaultClient.Do(req) @@ -41,9 +41,9 @@ func (d *Downloader) Download() error { defer resp.Body.Close() filename := sanitize.Path(strings.Trim(d.selectedBook.Title, " ") + "." + d.selectedBook.Extension) - filename = filepath.Clean(d.outputFileDir + "/" + filename) + filename = filepath.Clean(d.outputFileDir + "/" + filename) - fmt.Println("Downloading to: ", filename) + fmt.Println("Downloading to: ", filename) f, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666) defer f.Close() @@ -61,5 +61,5 @@ func (d *Downloader) Download() error { fmt.Println(console.Success("File successfully downloaded: %s", f.Name())) } - return err + return err } diff --git a/internal/libgen/libgen.go b/internal/libgen/libgen.go index 50fe451..f9f2fff 100644 --- a/internal/libgen/libgen.go +++ b/internal/libgen/libgen.go @@ -1,28 +1,29 @@ package libgen type Domain string + const ( - RS = "rs" - IS = "is" - ST = "st" + RS = "rs" + IS = "is" + ST = "st" - LI = "li" - LC = "lc" - GS = "gs" - TOP = "top" - CLICK = "click" + LI = "li" + LC = "lc" + GS = "gs" + TOP = "top" + CLICK = "click" ) type Filter string + const ( - TITLE = "title" - AUTHOR = "author" - SERIES = "series" - PUBLISHER = "publisher" - YEAR = "year" - ISBN = "isbn" - MD5 = "md5" - TAGS = "tags" - EXTENSION = "extension" + TITLE = "title" + AUTHOR = "author" + SERIES = "series" + PUBLISHER = "publisher" + YEAR = "year" + ISBN = "isbn" + MD5 = "md5" + TAGS = "tags" + EXTENSION = "extension" ) - diff --git a/internal/mirror/current_mirror.go b/internal/mirror/current_mirror.go index 7482aeb..8cab6d1 100644 --- a/internal/mirror/current_mirror.go +++ b/internal/mirror/current_mirror.go @@ -16,37 +16,37 @@ import ( ) type CurrentMirror struct { - domain libgen.Domain - filter libgen.Filter - config Configuration + domain libgen.Domain + filter libgen.Filter + config Configuration } func NewCurrentMirror(domain libgen.Domain) *CurrentMirror { - return &CurrentMirror{ - domain: domain, - // TODO: Make this configurable - filter: libgen.TITLE, - config: Configuration{ - numberOfResults: 5, - }, - } + return &CurrentMirror{ + domain: domain, + // TODO: Make this configurable + filter: libgen.TITLE, + config: Configuration{ + numberOfResults: 5, + }, + } } func (m *CurrentMirror) SearchByTitle(query string) ([]book.Book, error) { fmt.Println("Searching for: ", console.Higlight(query)) var document *goquery.Document - document, err := m.searchSite(query) + document, err := m.searchSite(query) - if err != nil || document == nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - return nil, errors.New("Error searching for book") - } + if err != nil || document == nil { + fmt.Println(console.Error("Error searching for book: %s", query)) + return nil, errors.New("Error searching for book") + } fmt.Println(console.Success("Search complete, parsing the document...")) - - page := documentparser.NewCurrentDocumentParser(document) - bookResults := page.GetBookDataFromDocument() + + page := documentparser.NewCurrentDocumentParser(document) + bookResults := page.GetBookDataFromDocument() if len(bookResults) >= m.config.numberOfResults { bookResults = bookResults[:m.config.numberOfResults] @@ -59,16 +59,16 @@ func (m *CurrentMirror) SearchByAuthor(query string) ([]book.Book, error) { fmt.Println("Searching by author: ", console.Higlight(query)) var document *goquery.Document - m.filter = libgen.AUTHOR - document, err := m.searchSite(query) + m.filter = libgen.AUTHOR + document, err := m.searchSite(query) + + if err != nil || document == nil { + fmt.Println(console.Error("Error searching for book: %s", query)) + return nil, errors.New("Error searching for book") + } - if err != nil || document == nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - return nil, errors.New("Error searching for book") - } - - page := documentparser.NewCurrentDocumentParser(document) - bookResults := page.GetBookDataFromDocument() + page := documentparser.NewCurrentDocumentParser(document) + bookResults := page.GetBookDataFromDocument() if len(bookResults) >= m.config.numberOfResults { bookResults = bookResults[:m.config.numberOfResults] @@ -77,30 +77,29 @@ func (m *CurrentMirror) SearchByAuthor(query string) ([]book.Book, error) { return bookResults, err } - -// Search the libgen site returns the document +// Search the libgen site returns the document // of the search results page func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) { - baseUrl := fmt.Sprintf("https://libgen.%s/index.php", m.domain) + baseUrl := fmt.Sprintf("https://libgen.%s/index.php", m.domain) queryString := fmt.Sprintf( - "%s?req=\"%s\"", - baseUrl, - url.QueryEscape(query), - ) + "%s?req=\"%s\"", + baseUrl, + url.QueryEscape(query), + ) - filter := string(string(m.filter)[0]) + filter := string(string(m.filter)[0]) - reqString:= queryString + "&columns%5B%5D=" + filter + "&objects%5B%5D=f&objects%5B%5D=e&objects%5B%5D=s&objects%5B%5D=a&objects%5B%5D=p&objects%5B%5D=w&topics%5B%5D=l&topics%5B%5D=c&topics%5B%5D=f&res=25&gmode=on&filesuns=all" + reqString := queryString + "&columns%5B%5D=" + filter + "&objects%5B%5D=f&objects%5B%5D=e&objects%5B%5D=s&objects%5B%5D=a&objects%5B%5D=p&objects%5B%5D=w&topics%5B%5D=l&topics%5B%5D=c&topics%5B%5D=f&res=25&gmode=on&filesuns=all" - fmt.Println(reqString) + fmt.Println(reqString) resp, e := http.Get(reqString) - if (resp.StatusCode > 400) { - fmt.Println("Library Genesis is down. ¯\\_(ツ)_/¯") - return nil, errors.New("Library Genesis is down") - } + if resp.StatusCode > 400 { + fmt.Println("Library Genesis is down. ¯\\_(ツ)_/¯") + return nil, errors.New("Library Genesis is down") + } if e != nil { return nil, e @@ -113,7 +112,7 @@ func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) { } }(resp.Body) - document, e := goquery.NewDocumentFromReader(resp.Body) + document, e := goquery.NewDocumentFromReader(resp.Body) if e != nil { fmt.Println(e) @@ -124,11 +123,11 @@ func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) { } func (m *CurrentMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) { - fmt.Println(console.Info("Downloading book...")) + fmt.Println(console.Info("Downloading book...")) - directLink := documentparser.GetDirectDownloadLinkFromCurrent(selectedBook.Mirrors[0]) - if outputDirectory == "" { - outputDirectory = "./" - } - downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download() + directLink := documentparser.GetDirectDownloadLinkFromCurrent(selectedBook.Mirrors[0]) + if outputDirectory == "" { + outputDirectory = "./" + } + downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download() } diff --git a/internal/mirror/legacy_mirror.go b/internal/mirror/legacy_mirror.go index eb78edd..ca8ede1 100644 --- a/internal/mirror/legacy_mirror.go +++ b/internal/mirror/legacy_mirror.go @@ -43,7 +43,7 @@ func (m *LegacyMirror) SearchByTitle(query string) ([]book.Book, error) { // TODO: Implement retrying // fmt.Println(infoColor("Retrying with other site")) // document, e = searchLibgen(query, siteToUse) // If this also fails then we have a problem - return nil, errors.New("Error searching for book") + return nil, errors.New("Error searching for book") } fmt.Println(console.Success("Search complete, parsing the document...")) @@ -58,24 +58,24 @@ func (m *LegacyMirror) SearchByTitle(query string) ([]book.Book, error) { } func (m *LegacyMirror) SearchByAuthor(query string) ([]book.Book, error) { - fmt.Println("Searching by author: ", console.Higlight(query)) + fmt.Println("Searching by author: ", console.Higlight(query)) - m.filter = libgen.AUTHOR - document, err := m.searchSite(query) + m.filter = libgen.AUTHOR + document, err := m.searchSite(query) - if err != nil || document == nil { - fmt.Println(console.Error("Error searching for book: %s", query)) - return nil, errors.New("Error searching for book") - } + if err != nil || document == nil { + fmt.Println(console.Error("Error searching for book: %s", query)) + return nil, errors.New("Error searching for book") + } - bookResults := - documentparser.NewLegacyDocumentParser(document).GetBookDataFromDocument() + bookResults := + documentparser.NewLegacyDocumentParser(document).GetBookDataFromDocument() - if len(bookResults) >= m.config.numberOfResults { - bookResults = bookResults[:m.config.numberOfResults] - } + if len(bookResults) >= m.config.numberOfResults { + bookResults = bookResults[:m.config.numberOfResults] + } - return bookResults, err + return bookResults, err } // Search the libgen site returns the document @@ -95,10 +95,10 @@ func (m *LegacyMirror) searchSite(query string) (*goquery.Document, error) { resp, e := http.Get(queryString) - if (resp.StatusCode > 400) { - fmt.Println("Library Genesis is down. ¯\\_(ツ)_/¯") - return nil, errors.New("Library Genesis is down") - } + if resp.StatusCode > 400 { + fmt.Println("Library Genesis is down. ¯\\_(ツ)_/¯") + return nil, errors.New("Library Genesis is down") + } if e != nil { return nil, e @@ -122,12 +122,12 @@ func (m *LegacyMirror) searchSite(query string) (*goquery.Document, error) { } func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) { - fmt.Println(console.Info("Downloading book...")) - directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0]) - - if outputDirectory == "" { - outputDirectory = "./" - } - - downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download() + fmt.Println(console.Info("Downloading book...")) + directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0]) + + if outputDirectory == "" { + outputDirectory = "./" + } + + downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download() } diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index 9328570..0f8af05 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -6,18 +6,18 @@ import ( ) type Mirror interface { - SearchByTitle(query string) ([]book.Book, error) - SearchByAuthor(author string) ([]book.Book, error) - // SearchByISBN(isbn string) []book.Book - // 1GetDownloadLink(book book.Book) string - DownloadSelection(book book.Book, outputDirectory string) + SearchByTitle(query string) ([]book.Book, error) + SearchByAuthor(author string) ([]book.Book, error) + // SearchByISBN(isbn string) []book.Book + // 1GetDownloadLink(book book.Book) string + DownloadSelection(book book.Book, outputDirectory string) } // TODO: Make this persistent type Configuration struct { - numberOfResults int + numberOfResults int } type NewMirror struct { - domain libgen.Domain + domain libgen.Domain } diff --git a/test/integration_test.go b/test/integration_test.go index ebd1e6f..735c540 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -14,90 +14,89 @@ import ( ) var ( - update = flag.Bool("update", false, "update .golden files") + update = flag.Bool("update", false, "update .golden files") ) var binaryName = "clibgen" var binaryPath = "" func TestMain(m *testing.M) { - flag.Parse() + flag.Parse() - err := os.Chdir("..") - if err != nil { - fmt.Printf("could not change dir: %v", err) - os.Exit(1) - } + err := os.Chdir("..") + if err != nil { + fmt.Printf("could not change dir: %v", err) + os.Exit(1) + } - dir, err := os.Getwd() + dir, err := os.Getwd() - if err != nil { - fmt.Printf("could not get current dir: %v", err) - } + if err != nil { + fmt.Printf("could not get current dir: %v", err) + } - binaryPath = filepath.Join(dir, binaryName) + binaryPath = filepath.Join(dir, binaryName) - fmt.Println("binary path", binaryPath) + fmt.Println("binary path", binaryPath) - os.Exit(m.Run()) + os.Exit(m.Run()) } - func runBinary(args []string) ([]byte, error) { - cmd := exec.Command(binaryPath, args...) - cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") - return cmd.CombinedOutput() + cmd := exec.Command(binaryPath, args...) + cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") + return cmd.CombinedOutput() } func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) { - fmt.Println("Running binary with file input", bytesToWrite) - fmt.Printf("Executing command: %s", binaryPath) + fmt.Println("Running binary with file input", bytesToWrite) + fmt.Printf("Executing command: %s", binaryPath) - cmd := exec.Command(binaryPath, args...) - cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") + cmd := exec.Command(binaryPath, args...) + cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") - stdout, err := cmd.StdoutPipe() - stdin, err := cmd.StdinPipe() + stdout, err := cmd.StdoutPipe() + stdin, err := cmd.StdinPipe() - cmd.Stderr = cmd.Stdout + cmd.Stderr = cmd.Stdout - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } - fmt.Println("Starting command...") - if err = cmd.Start(); err != nil { - return nil, err - } + fmt.Println("Starting command...") + if err = cmd.Start(); err != nil { + return nil, err + } - time.Sleep(1 * time.Second) + time.Sleep(1 * time.Second) - _, err = stdin.Write(bytesToWrite) + _, err = stdin.Write(bytesToWrite) - fmt.Println("Wrote to stdin", bytesToWrite) + fmt.Println("Wrote to stdin", bytesToWrite) - if err != nil { - fmt.Println("Error writing to stdin", err) - } + if err != nil { + fmt.Println("Error writing to stdin", err) + } - time.Sleep(14 * time.Second) + time.Sleep(14 * time.Second) - if err = cmd.Process.Signal(os.Interrupt); err != nil { - fmt.Println("signal") - return nil, err - } + if err = cmd.Process.Signal(os.Interrupt); err != nil { + fmt.Println("signal") + return nil, err + } - out, _ := ioutil.ReadAll(stdout) + out, _ := ioutil.ReadAll(stdout) - if err = cmd.Wait(); err != nil { - if strings.Contains(err.Error(), "interrupt") { - return out, nil - } else { - return nil, err - } - } + if err = cmd.Wait(); err != nil { + if strings.Contains(err.Error(), "interrupt") { + return out, nil + } else { + return nil, err + } + } - return out, err + return out, err } func TestStaticCliArgs(t *testing.T) { @@ -106,7 +105,7 @@ func TestStaticCliArgs(t *testing.T) { args []string fixture string }{ - {"help args", []string{"--help"}, "help.golden"}, + {"help args", []string{"--help"}, "help.golden"}, } for _, tt := range tests { @@ -134,26 +133,26 @@ func TestStaticCliArgs(t *testing.T) { func TestSearch(t *testing.T) { tests := []struct { - name string - args []string - bytesToWrite []byte + name string + args []string + bytesToWrite []byte }{ - {"search test", []string{"search", "Eloquent JavaScript"}, []byte{14, 14, 10}}, + {"search test", []string{"search", "Eloquent JavaScript"}, []byte{14, 14, 10}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { output, err := runBinaryWithFileInput(tt.args, tt.bytesToWrite) - fmt.Println(string(output)) - if _, err := os.Stat("eloquent-javascript-a-modern-introduction-to-programming.epub"); err == nil { - fmt.Printf("File exists\n"); - } else { + fmt.Println(string(output)) + if _, err := os.Stat("eloquent-javascript-a-modern-introduction-to-programming.epub"); err == nil { + fmt.Printf("File exists\n") + } else { t.Fatal("File wasn't found") - } + } if err != nil { - fmt.Println("error", err.Error()) + fmt.Println("error", err.Error()) t.Fatal(err) } }) @@ -164,7 +163,7 @@ func removeLastLine(str string, num int) string { lines := strings.Split(str, "\n") if len(lines) > 0 { - lines = lines[:len(lines) - num] + lines = lines[:len(lines)-num] } return strings.Join(lines, "\n") @@ -177,22 +176,22 @@ func writeFixture(t *testing.T, goldenFile string, actual []byte) { f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) defer f.Close() - _, err = f.WriteString(string(actual)) + _, err = f.WriteString(string(actual)) - if err != nil { - t.Fatalf("Error writing to file %s: %s", goldenPath, err) - } + if err != nil { + t.Fatalf("Error writing to file %s: %s", goldenPath, err) + } } func loadFixture(t *testing.T, goldenFile string) string { goldenPath := "testdata/" + goldenFile - f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) + f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) - content, err := ioutil.ReadAll(f) - if err != nil { - t.Fatalf("Error opening file %s: %s", goldenPath, err) - } + content, err := ioutil.ReadAll(f) + if err != nil { + t.Fatalf("Error opening file %s: %s", goldenPath, err) + } - return string(content) + return string(content) }