Skip to content

Commit

Permalink
feat(tools): Add category flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed Jul 16, 2024
1 parent dd8123d commit edc9927
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ Usage: werkzeugkasten [flags]
Flags:
-accessible
Enable accessibility mode for interactive use
-categories
Print all categories and tool count
-category string
List tools by category
-debug
Enable debug output
-dir string
Where to download the tools (default ".")
-help
Print help message
-installDir string
Where to install the tools (default ".")
-list
Print all available tools
-theme string
Set theme for interactive mode (default "catppuccin")
-tool value
Specify multiple tools to install programmatically (e.g., -tool kustomize -tool task)
-tools
Print all available tools
-version
Print version
```
Expand Down
7 changes: 6 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type cliConfig struct {
accessible bool
category string
debug bool
downloadDir string
tools bool
Expand Down Expand Up @@ -36,7 +37,8 @@ func cli() cliConfig {
accessibleFlag := flag.Bool("accessible", false, "Enable accessibility mode for interactive use")
downloadDirFlag := flag.String("dir", ".", "Where to download the tools")
listToolsFlag := flag.Bool("tools", false, "Print all available tools")
listCategoriesFlag := flag.Bool("categories", false, "Print all available categories")
listCategoriesFlag := flag.Bool("categories", false, "Print all categories and tool count")
listByCategoriesFlag := flag.String("category", "", "List tools by category")
themeFlag := flag.String("theme", "catppuccin", "Set theme for interactive mode")
flag.Var(&toolList, "tool", "Specify multiple tools to install programmatically (e.g., -tool kustomize -tool task)")
flag.Parse()
Expand Down Expand Up @@ -65,6 +67,9 @@ func cli() cliConfig {
if *downloadDirFlag != "" {
cliFlags.downloadDir = *downloadDirFlag
}
if *listByCategoriesFlag != "" {
cliFlags.category = *listByCategoriesFlag
}
cliFlags.toolList = []string{}
if len(toolList) > 0 {
cliFlags.toolList = toolList
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ func main() {

if cfg.tools {
printTools(tools)
logger.Debug("found tools", "count", len(tools.Tools))
os.Exit(0)
}
if cfg.categories {
printCategories(getCategories(tools))
os.Exit(0)
}
if cfg.category != "" {
result := getToolsByCategory(cfg.category, tools)
if len(result.Tools) == 0 {
logger.Warn("no results found", "category", cfg.category)
os.Exit(0)
}
printTools(result)
logger.Debug("found tools", "category", cfg.category, "count", len(result.Tools))
os.Exit(0)
}
// interactive mode
if len(cfg.toolList) == 0 {
startUI(cfg, tools)
Expand Down
16 changes: 16 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ func printCategories(categories map[string]int) {
w.Flush()
}

func getToolsByCategory(category string, tools Tools) Tools {
var toolsFound Tools
toolsFound.Tools = make(map[string]Tool, 0)
lowerCategory := strings.ToLower(category)
for k, t := range tools.Tools {
lowerCategories := make([]string, len(t.Categories))
for i, v := range t.Categories {
lowerCategories[i] = strings.ToLower(v)
}
if slices.Contains(lowerCategories, lowerCategory) {
toolsFound.Tools[k] = t
}
}
return toolsFound
}

func printTools(tools Tools) {
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintln(w, "Key\tURL\tDescription")
Expand Down

0 comments on commit edc9927

Please sign in to comment.