-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (79 loc) · 2.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"os"
"strings"
"github.com/benjaminkitt/shape-up-downloader/internal/converter"
"github.com/benjaminkitt/shape-up-downloader/internal/downloader"
"github.com/spf13/cobra"
)
func validateFlags(format string, output string) error {
// Validate format
format = strings.ToLower(format)
if format != "html" && format != "epub" {
return fmt.Errorf("invalid format: %s (must be 'html' or 'epub')", format)
}
// Validate output path
if format == "epub" {
if !strings.HasSuffix(output, ".epub") {
output = output + ".epub"
}
}
// Check if output directory/file exists
if _, err := os.Stat(output); err == nil {
return fmt.Errorf("output path already exists: %s", output)
}
return nil
}
func main() {
var outputFormat string
var outputDir string
rootCmd := &cobra.Command{
Use: "shape-up-downloader",
Short: "Download the Shape Up book from Basecamp",
Long: `A CLI tool to download the Shape Up book by Ryan Singer,
published by Basecamp, and save it as HTML or EPUB`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := validateFlags(outputFormat, outputDir); err != nil {
return err
}
// Initialize downloader
dl := downloader.New()
// Fetch table of contents
chapters, err := dl.FetchTOC()
if err != nil {
return fmt.Errorf("failed to fetch table of contents: %w", err)
}
// Download each chapter
for i, chapter := range chapters {
fmt.Printf("Downloading chapter %d/%d: %s\n", i+1, len(chapters), chapter.Title)
ch, err := dl.FetchChapter(chapter)
if err != nil {
return fmt.Errorf("failed to fetch chapter %s: %w", chapter.Title, err)
}
chapters[i] = *ch
}
// Convert to requested format
switch outputFormat {
case "html":
conv := converter.NewHTMLConverter(outputDir)
if err := conv.Convert(chapters, chapters[0].CSS); err != nil {
return fmt.Errorf("failed to convert to HTML: %w", err)
}
case "epub":
conv := converter.NewEPUBConverter(outputDir)
if err := conv.Convert(chapters, chapters[0].CSS); err != nil {
return fmt.Errorf("failed to convert to EPUB: %w", err)
}
}
fmt.Printf("Successfully downloaded Shape Up book to %s\n", outputDir)
return nil
},
}
rootCmd.Flags().StringVarP(&outputFormat, "format", "f", "html", "Output format (html or epub)")
rootCmd.Flags().StringVarP(&outputDir, "output", "o", "shape-up-book", "Output directory for HTML or filename for EPUB")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}