|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "flag" |
5 | | - "os" |
6 | | - |
7 | | - "github.com/fatih/color" |
8 | | - "github.com/heinrichb/scrapey-cli/pkg/config" |
9 | | - "github.com/heinrichb/scrapey-cli/pkg/utils" |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | + "github.com/heinrichb/scrapey-cli/pkg/config" |
| 11 | + "github.com/heinrichb/scrapey-cli/pkg/crawler" |
| 12 | + "github.com/heinrichb/scrapey-cli/pkg/utils" |
10 | 13 | ) |
11 | 14 |
|
12 | 15 | /* |
@@ -58,27 +61,27 @@ It parses command-line flags, prints a welcome message, loads the configuration, |
58 | 61 | applies CLI overrides using a ConfigOverride object, and prints confirmation messages. |
59 | 62 | */ |
60 | 63 | func main() { |
61 | | - // Parse CLI flags. |
62 | | - flag.Parse() |
| 64 | + // Parse CLI flags. |
| 65 | + flag.Parse() |
63 | 66 |
|
64 | 67 | // Store the verbose flag in global state. |
65 | 68 | config.Verbose = verbose |
66 | 69 |
|
67 | 70 | // Print a welcome message in cyan using our PrintColored utility. |
68 | 71 | utils.PrintColored("Welcome to Scrapey CLI!", "", color.FgCyan) |
69 | 72 |
|
70 | | - // Default to "configs/default.json" if no config path is provided. |
71 | | - if configPath == "" { |
72 | | - configPath = "configs/default.json" |
73 | | - } |
| 73 | + // Default to "configs/default.json" if no config path is provided. |
| 74 | + if configPath == "" { |
| 75 | + configPath = "configs/default.json" |
| 76 | + } |
74 | 77 |
|
75 | | - // Attempt to load the configuration from the specified file. |
76 | | - cfg, err := config.Load(configPath) |
77 | | - if err != nil { |
78 | | - // If loading fails, print an error message in red and exit. |
79 | | - utils.PrintColored("Failed to load config: ", err.Error(), color.FgRed) |
80 | | - os.Exit(1) |
81 | | - } |
| 78 | + // Attempt to load the configuration from the specified file. |
| 79 | + cfg, err := config.Load(configPath) |
| 80 | + if err != nil { |
| 81 | + // If loading fails, print an error message in red and exit. |
| 82 | + utils.PrintColored("Failed to load config: ", err.Error(), color.FgRed) |
| 83 | + os.Exit(1) |
| 84 | + } |
82 | 85 |
|
83 | 86 | // Construct a partial ConfigOverride struct for CLI overrides. |
84 | 87 | cliOverrides := config.ConfigOverride{} |
@@ -128,10 +131,29 @@ func main() { |
128 | 131 |
|
129 | 132 | // Print which routes will be scraped. |
130 | 133 | utils.PrintColored("Base URL: ", cfg.URL.Base, color.FgYellow) |
131 | | - if cfg.URL.IncludeBase { |
| 134 | + // Create a new Crawler instance |
| 135 | + c := crawler.New() |
| 136 | + |
| 137 | + if cfg.URL.IncludeBase { |
132 | 138 | utils.PrintColored("Including base URL in scraping.", "", color.FgGreen) |
| 139 | + content, err := c.FetchURL(cfg.URL.Base) |
| 140 | + if err != nil { |
| 141 | + utils.PrintColored("Failed to fetch URL: ", err.Error(), color.FgRed) |
| 142 | + os.Exit(1) // To-Do Handle error without exiting program |
| 143 | + } |
| 144 | + // Print the fetched content |
| 145 | + fmt.Println("Fetched Content:") |
| 146 | + fmt.Println(content) |
133 | 147 | } |
134 | 148 | for _, route := range cfg.URL.Routes { |
135 | 149 | utils.PrintColored("Scraping route: ", route, color.FgHiBlue) |
| 150 | + content, err := c.FetchURL(path.Join(cfg.URL.Base,route)) |
| 151 | + if err != nil { |
| 152 | + utils.PrintColored("Failed to fetch URL: ", err.Error(), color.FgRed) |
| 153 | + os.Exit(1) // To-Do Handle error without exiting program |
| 154 | + } |
| 155 | + // Print the fetched content |
| 156 | + fmt.Println("Fetched Content:") |
| 157 | + fmt.Println(content) |
136 | 158 | } |
137 | 159 | } |
0 commit comments