77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.nakama.town/fmartingr/dharma/pkg/reporter"
|
|
"git.nakama.town/fmartingr/dharma/pkg/scraper"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
format string
|
|
concurrency int
|
|
depth int
|
|
timeout int
|
|
verbose bool
|
|
internalOnly bool
|
|
)
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
var rootCmd = &cobra.Command{
|
|
Use: "dharma [URL]",
|
|
Short: "Scrape websites and check for broken links and references",
|
|
Long: `Dharma is a website link checker tool that crawls a website to find broken links,
|
|
images, CSS references, and more. It generates a report of all issues found.`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
url := args[0]
|
|
|
|
// Only print status message when using pretty format
|
|
if format == "pretty" {
|
|
fmt.Printf("Scanning website: %s\n", url)
|
|
} else {
|
|
// Force verbose off for non-pretty formats
|
|
verbose = false
|
|
}
|
|
|
|
// Create a new scraper
|
|
s := scraper.New(
|
|
scraper.WithConcurrency(concurrency),
|
|
scraper.WithDepth(depth),
|
|
scraper.WithTimeout(timeout),
|
|
scraper.WithVerbose(verbose),
|
|
scraper.WithInternalOnly(internalOnly),
|
|
)
|
|
|
|
// Run the scraper
|
|
results, err := s.Scan(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Generate report
|
|
r, err := reporter.New(format)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return r.Generate(results, os.Stdout)
|
|
},
|
|
}
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
func Execute() error {
|
|
return rootCmd.Execute()
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.Flags().StringVarP(&format, "format", "f", "pretty", "Output format (pretty, json, csv)")
|
|
rootCmd.Flags().IntVarP(&concurrency, "concurrency", "c", 10, "Number of concurrent requests")
|
|
rootCmd.Flags().IntVarP(&depth, "depth", "d", 3, "Maximum depth to crawl")
|
|
rootCmd.Flags().IntVarP(&timeout, "timeout", "t", 10, "Timeout in seconds for each request")
|
|
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
|
|
rootCmd.Flags().BoolVarP(&internalOnly, "internal-only", "i", false, "Only check internal links (same hostname)")
|
|
}
|