Skip to content

Latest commit

 

History

History
159 lines (125 loc) · 3.07 KB

File metadata and controls

159 lines (125 loc) · 3.07 KB

I need to first explore the repository structure to understand the codebase and then create a complete solution for the toxicity filter CLI.

<search_files> . .*.(js|ts|py|go|rs|java)$ </search_files>

<read_file> . </read_file>

<search_files> . package.json|requirements.txt|go.mod|Cargo.toml|pom.xml </search_files>

<list_dir> . </list_dir>

<search_files> . .CLI. </search_files>

<search_files> . .toxicity.|.species.|.catalog. </search_files>

<list_dir> src </list_dir>

<list_dir> cmd </list_dir>

<read_file> README.md </read_file>

<read_file> go.mod </read_file>

<search_content> species </search_content>

<search_content> toxicity </search_content>

<read_file> internal/species/species.go </read_file>

<read_file> cmd/plantguide/main.go </read_file>

<list_dir> internal </list_dir>

<read_file> internal/catalog/catalog.go </read_file>

<list_dir> cmd/plantguide </list_dir>

<read_file> cmd/plantguide/filter.go </read_file>

<search_content> FilterCommand </search_content>

<read_file> internal/catalog/catalog_test.go </read_file>

<read_file> cmd/plantguide/filter_test.go </read_file>

<search_content> toxic </search_content>

package main

import ( "encoding/json" "flag" "fmt" "os" "strings"

"github.com/mergeos-bounties/PlantGuide/internal/catalog"

)

func filterToxicityCommand() { fs := flag.NewFlagSet("filter-toxicity", flag.ExitOnError) toxicity := fs.String("toxicity", "", "Filter by toxicity level (safe, toxic, highly-toxic)") format := fs.String("format", "json", "Output format (json, text)")

fs.Parse(os.Args[2:])

if *toxicity == "" {
	fmt.Fprintln(os.Stderr, "Error: --toxicity flag is required")
	fs.Usage()
	os.Exit(1)
}

toxicityLevel := strings.ToLower(*toxicity)
validLevels := map[string]bool{
	"safe":         true,
	"toxic":        true,
	"highly-toxic": true,
}

if !validLevels[toxicityLevel] {
	fmt.Fprintf(os.Stderr, "Error: invalid toxicity level '%s'. Must be one of: safe, toxic, highly-toxic\n", *toxicity)
	os.Exit(1)
}

cat, err := catalog.Load()
if err != nil {
	fmt.Fprintf(os.Stderr, "Error loading catalog: %v\n", err)
	os.Exit(1)
}

filtered := catalog.FilterByToxicity(cat, toxicityLevel)

if *format == "json" {
	encoder := json.NewEncoder(os.Stdout)
	encoder.SetIndent("", "  ")
	if err := encoder.Encode(filtered); err != nil {
		fmt.Fprintf(os.Stderr, "Error encoding JSON: %v\n", err)
		os.Exit(1)
	}
} else if *format == "text" {
	for _, species := range filtered {
		fmt.Printf("%s (%s) - Toxicity: %s\n", species.CommonName, species.ScientificName, species.Toxicity)
	}
} else {
	fmt.Fprintf(os.Stderr, "Error: invalid format '%s'. Must be 'json' or 'text'\n", *format)
	os.Exit(1)
}

}

func init() { commands["filter-toxicity"] = filterToxicityCommand }