mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-03 20:13:03 +00:00
137 lines
4.5 KiB
Go
137 lines
4.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var getCmd = &cobra.Command{
|
|
Use: "get <language> <keyword>",
|
|
Short: "Get documentation for a language/framework",
|
|
Long: `Quickly fetch documentation for popular languages and frameworks.
|
|
This command automatically maps language+keyword combinations to their official documentation sites.
|
|
|
|
Supported languages:
|
|
go, golang - Go documentation (pkg.go.dev)
|
|
rust - Rust documentation (docs.rs)
|
|
python, py - Python documentation (docs.python.org)
|
|
java - Java documentation (docs.oracle.com)
|
|
spring - Spring Boot documentation (docs.spring.io)
|
|
typescript, ts - TypeScript documentation (typescriptlang.org)
|
|
react - React documentation (react.dev)
|
|
vue - Vue.js documentation (vuejs.org)
|
|
nuxt - Nuxt documentation (nuxt.com)
|
|
docker - Docker documentation (docs.docker.com)
|
|
cloudflare, cf - Cloudflare documentation (developers.cloudflare.com)
|
|
astro - Astro documentation (docs.astro.build)
|
|
|
|
Examples:
|
|
devour get go http # Go HTTP package documentation
|
|
devour get python asyncio # Python asyncio module
|
|
devour get react hooks # React Hooks documentation
|
|
devour get docker compose # Docker Compose docs
|
|
devour get rust tokio # Rust Tokio crate`,
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: runGet,
|
|
}
|
|
|
|
func init() {
|
|
// Add flags that can override defaults
|
|
getCmd.Flags().StringVarP(&scrapeFormat, "format", "f", "json", "output format (json, markdown)")
|
|
getCmd.Flags().StringVarP(&scrapeOutput, "output", "o", "", "output directory (default: devour_data/docs)")
|
|
getCmd.Flags().IntVar(&scrapeConcurrency, "concurrency", 10, "parallel scraping workers")
|
|
}
|
|
|
|
func runGet(cmd *cobra.Command, args []string) error {
|
|
language := strings.ToLower(args[0])
|
|
keyword := strings.ToLower(args[1])
|
|
|
|
// Map language to base URL and construct full URL
|
|
url, err := constructDocURL(language, keyword)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Set the scrape type based on language
|
|
sourceType := mapLanguageToType(language)
|
|
|
|
// Reuse the existing scrape logic with pre-determined values
|
|
scrapeType = string(sourceType)
|
|
sourceURL := url
|
|
|
|
fmt.Printf("Getting docs for: %s %s\n", language, keyword)
|
|
fmt.Printf("URL: %s\n", sourceURL)
|
|
fmt.Printf("Type: %s\n", sourceType)
|
|
fmt.Println()
|
|
|
|
// Call the existing scrape logic
|
|
return runScrape(cmd, []string{sourceURL})
|
|
}
|
|
|
|
func constructDocURL(language, keyword string) (string, error) {
|
|
switch language {
|
|
case "go", "golang":
|
|
return fmt.Sprintf("https://pkg.go.dev/%s", keyword), nil
|
|
case "rust":
|
|
return fmt.Sprintf("https://docs.rs/%s/latest/%s/", keyword, keyword), nil
|
|
case "python", "py":
|
|
if keyword == "stdlib" || keyword == "standard" {
|
|
return "https://docs.python.org/3/library/", nil
|
|
}
|
|
return fmt.Sprintf("https://docs.python.org/3/library/%s.html", keyword), nil
|
|
case "java":
|
|
return fmt.Sprintf("https://docs.oracle.com/javase/8/docs/api/%s.html", keyword), nil
|
|
case "spring":
|
|
return fmt.Sprintf("https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#%s", keyword), nil
|
|
case "typescript", "ts":
|
|
return fmt.Sprintf("https://www.typescriptlang.org/docs/handbook/%s.html", keyword), nil
|
|
case "react":
|
|
return fmt.Sprintf("https://react.dev/reference/react/%s", keyword), nil
|
|
case "vue":
|
|
return fmt.Sprintf("https://vuejs.org/guide/%s.html", keyword), nil
|
|
case "nuxt":
|
|
return fmt.Sprintf("https://nuxt.com/docs/guide/%s", keyword), nil
|
|
case "docker":
|
|
return fmt.Sprintf("https://docs.docker.com/%s", keyword), nil
|
|
case "cloudflare", "cf":
|
|
return fmt.Sprintf("https://developers.cloudflare.com/%s", keyword), nil
|
|
case "astro":
|
|
return fmt.Sprintf("https://docs.astro.build/en/guides/%s", keyword), nil
|
|
default:
|
|
return "", fmt.Errorf("unsupported language: %s. Supported languages: go, rust, python, java, spring, typescript, react, vue, nuxt, docker, cloudflare, astro", language)
|
|
}
|
|
}
|
|
|
|
func mapLanguageToType(language string) string {
|
|
switch language {
|
|
case "go", "golang":
|
|
return "godocs"
|
|
case "rust":
|
|
return "rustdocs"
|
|
case "python", "py":
|
|
return "pythondocs"
|
|
case "java":
|
|
return "javadocs"
|
|
case "spring":
|
|
return "springdocs"
|
|
case "typescript", "ts":
|
|
return "tsdocs"
|
|
case "react":
|
|
return "reactdocs"
|
|
case "vue":
|
|
return "vuedocs"
|
|
case "nuxt":
|
|
return "nuxtdocs"
|
|
case "docker":
|
|
return "dockerdocs"
|
|
case "cloudflare", "cf":
|
|
return "cloudflaredocs"
|
|
case "astro":
|
|
return "astrodocs"
|
|
default:
|
|
return "web"
|
|
}
|
|
}
|