Skip to content

Commit 5f2ea0d

Browse files
authored
Feature/dynamic output dir (#31)
2 parents 4eb4d88 + 795eace commit 5f2ea0d

File tree

7 files changed

+79
-71
lines changed

7 files changed

+79
-71
lines changed

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,19 @@ Search for a book using the old website (default)
3232
clibgen search "Eloquent JavaScript"
3333
```
3434

35-
Search for a book using the newer website (this is useful if for some reason the old website is down or the mirrors are not working)
36-
`-s or -site` flag
35+
#### Search
3736

38-
```shell
39-
clibgen search -s "new" "Eloquent JavaScript"
40-
```
37+
Usage:
38+
` clibgen search [flags]`
4139

42-
```shell
43-
clibgen search -s "legacy" "Eloquent JavaScript"
44-
```
40+
Flags:
41+
- -f, --filter string search by [title, author, isbn] (default "title")
42+
- -h, --help help for search
43+
- -n, --number of results int number of result(s) to be displayed maximum: 25 (default 10)
44+
- -o, --output string Output directory (default "./")
45+
- -s, --site string which website to use [legacy, new] (default "legacy")
4546

46-
Limit search results (default: 10)
4747

48-
```shell
49-
clibgen search -n 5 "Eloquent JavaScript"
50-
```
5148

5249
### Found an issue?
5350

cmd/search.go

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@ package cmd
22

33
import (
44
"fmt"
5-
"strings"
6-
75
"github.com/fatih/color"
86
"github.com/laureanray/clibgen/internal/book"
97
"github.com/laureanray/clibgen/internal/libgen"
108
"github.com/laureanray/clibgen/internal/mirror"
9+
"github.com/laureanray/clibgen/internal/utils"
1110
"github.com/manifoldco/promptui"
1211
"github.com/spf13/cobra"
1312
)
1413

15-
func truncateText(s string, max int) string {
16-
if max > len(s) {
17-
return s
18-
}
19-
return s[:strings.LastIndex(s[:max], " ")] + " ..."
20-
}
21-
2214
func getExtension(s string) string {
2315
cyan := color.New(color.FgHiCyan).SprintFunc()
2416
magenta := color.New(color.FgHiMagenta).SprintFunc()
@@ -36,7 +28,8 @@ func getExtension(s string) string {
3628

3729
var (
3830
selectedSite string
39-
selectedFilter string
31+
selectedFilter string
32+
outputDirectory string
4033
numberOfResults = 10
4134

4235
searchCmd = &cobra.Command{
@@ -50,76 +43,69 @@ var (
5043
return
5144
}
5245

53-
var m mirror.Mirror
46+
var m mirror.Mirror
5447

5548
if selectedSite == "legacy" {
56-
m = mirror.NewLegacyMirror(libgen.IS)
49+
m = mirror.NewLegacyMirror(libgen.IS)
5750
} else if selectedSite == "new" {
58-
m = mirror.NewCurrentMirror(libgen.LC)
59-
} else{
60-
// TODO: Improve this.
61-
fmt.Print("Not an option");
62-
return
63-
}
64-
65-
var books []book.Book
66-
67-
switch (selectedFilter) {
68-
case libgen.AUTHOR:
69-
books, _ = m.SearchByAuthor(args[0])
70-
default:
71-
books, _ = m.SearchByTitle(args[0])
72-
}
73-
74-
if len(books) == 0 {
75-
return
76-
}
51+
m = mirror.NewCurrentMirror(libgen.LC)
52+
} else {
53+
// TODO: Improve this.
54+
fmt.Print("Not an option")
55+
return
56+
}
57+
58+
var books []book.Book
59+
60+
switch selectedFilter {
61+
case libgen.AUTHOR:
62+
books, _ = m.SearchByAuthor(args[0])
63+
default:
64+
books, _ = m.SearchByTitle(args[0])
65+
}
66+
67+
if len(books) == 0 {
68+
return
69+
}
7770

7871
var titles []string
7972

8073
for _, book := range books {
81-
parsedTitle := truncateText(book.Title, 42)
82-
parsedAuthor := truncateText(book.Author, 24)
74+
parsedTitle := utils.TruncateText(book.Title, 42)
75+
parsedAuthor := utils.TruncateText(book.Author, 24)
8376
parsedExt := getExtension(fmt.Sprintf("%-4s", book.Extension))
8477
titles = append(titles, fmt.Sprintf("%s %-6s | %-45s %s", parsedExt, book.FileSize, parsedTitle, parsedAuthor))
8578
}
86-
79+
8780
prompt := promptui.Select{
8881
Label: "Select Title",
8982
Items: titles,
9083
}
91-
84+
9285
resultInt, _, err := prompt.Run()
93-
86+
9487
if err != nil {
9588
fmt.Printf("Prompt failed %v\n", err)
9689
return
9790
}
9891

99-
println(resultInt)
100-
101-
m.DownloadSelection(books[resultInt])
92+
m.DownloadSelection(books[resultInt], outputDirectory)
10293
},
10394
}
10495
)
10596

10697
func init() {
10798
searchCmd.
10899
PersistentFlags().
109-
StringVarP(&selectedSite, "site", "s", "legacy", `select which site to use
110-
options:
111-
"legacy"
112-
"new"
113-
`)
114-
115-
searchCmd.
116-
PersistentFlags().
117-
StringVarP(&selectedFilter, "filter", "f", "title", `select which filter to use
118-
options:
119-
"title"
120-
"author"
121-
"isbn"
122-
`)
100+
StringVarP(&selectedSite, "site", "s", "legacy", `which website to use [legacy, new]`)
101+
102+
searchCmd.
103+
PersistentFlags().
104+
StringVarP(&selectedFilter, "filter", "f", "title", `search by [title, author, isbn]`)
105+
106+
searchCmd.
107+
PersistentFlags().
108+
StringVarP(&outputDirectory, "output", "o", "./", `Output directory`)
123109

124110
searchCmd.
125111
PersistentFlags().

internal/downloader/downloader.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net/http"
77
"os"
8+
"path/filepath"
89
"strings"
910

1011
"github.com/kennygrant/sanitize"
@@ -16,12 +17,14 @@ import (
1617
type Downloader struct {
1718
selectedBook book.Book
1819
directLink string
20+
outputFileDir string
1921
}
2022

21-
func NewDownloader(selectedBook book.Book, directLink string) *Downloader {
23+
func NewDownloader(selectedBook book.Book, directLink string, outputFileDir string) *Downloader {
2224
return &Downloader{
2325
selectedBook: selectedBook,
2426
directLink: directLink,
27+
outputFileDir: outputFileDir,
2528
}
2629
}
2730

@@ -38,6 +41,9 @@ func (d *Downloader) Download() error {
3841

3942
defer resp.Body.Close()
4043
filename := sanitize.Path(strings.Trim(d.selectedBook.Title, " ") + "." + d.selectedBook.Extension)
44+
filename = filepath.Clean(d.outputFileDir + "/" + filename)
45+
46+
fmt.Println("Downloading to: ", filename)
4147

4248
f, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
4349
defer f.Close()

internal/mirror/current_mirror.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ func (m *CurrentMirror) searchSite(query string) (*goquery.Document, error) {
123123
return document, e
124124
}
125125

126-
func (m *CurrentMirror) DownloadSelection(selectedBook book.Book) {
126+
func (m *CurrentMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) {
127127
fmt.Println(console.Info("Downloading book..."))
128+
128129
directLink := documentparser.GetDirectDownloadLinkFromCurrent(selectedBook.Mirrors[0])
129-
downloader.NewDownloader(selectedBook, directLink).Download()
130+
if outputDirectory == "" {
131+
outputDirectory = "./"
132+
}
133+
downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download()
130134
}

internal/mirror/legacy_mirror.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ func (m *LegacyMirror) searchSite(query string) (*goquery.Document, error) {
121121
return document, e
122122
}
123123

124-
func (m *LegacyMirror) DownloadSelection(selectedBook book.Book) {
124+
func (m *LegacyMirror) DownloadSelection(selectedBook book.Book, outputDirectory string) {
125125
fmt.Println(console.Info("Downloading book..."))
126126
directLink := documentparser.GetDirectDownloadLinkFromLegacy(selectedBook.Mirrors[0])
127-
downloader.NewDownloader(selectedBook, directLink).Download()
127+
128+
if outputDirectory == "" {
129+
outputDirectory = "./"
130+
}
131+
132+
downloader.NewDownloader(selectedBook, directLink, outputDirectory).Download()
128133
}

internal/mirror/mirror.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Mirror interface {
1010
SearchByAuthor(author string) ([]book.Book, error)
1111
// SearchByISBN(isbn string) []book.Book
1212
// 1GetDownloadLink(book book.Book) string
13-
DownloadSelection(book book.Book)
13+
DownloadSelection(book book.Book, outputDirectory string)
1414
}
1515

1616
// TODO: Make this persistent

internal/utils/string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
import "strings"
4+
5+
func TruncateText(s string, max int) string {
6+
if max > len(s) {
7+
return s
8+
}
9+
return s[:strings.LastIndex(s[:max], " ")] + " ..."
10+
}

0 commit comments

Comments
 (0)