Skip to content

Commit

Permalink
fix: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Aug 19, 2024
1 parent 2435f22 commit fc52b11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
45 changes: 22 additions & 23 deletions cmd/ayo/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"strings"
"text/tabwriter"

Expand All @@ -10,6 +11,8 @@ import (
"github.com/banst/ayo/pkg/tool"
)

type Tools []string

type Tool struct {
Tools []string `arg:"" optional:"" help:"Filename of the tool to get information about. If empty, all tools from the toolbox are listed." type:"existingfile"` //nolint:lll // Struct tags are long.
}
Expand All @@ -27,32 +30,15 @@ func (t *Tool) Run(context *kong.Context, cli *CLI) error {

for i, tf := range t.Tools {
if i > 0 {
if _, err := fmt.Fprintln(tw); err != nil {
return err
}
}

if _, err := fmt.Fprintf(tw, "File:\t%s\n", tf); err != nil {
return err
fmt.Fprintln(tw)
}
fmt.Fprintf(tw, "File:\t%s\n", tf)

if t, err := tool.Load(tf); err != nil {
if _, err := fmt.Fprintf(tw, "Error:\t%s\n", err); err != nil {
return err
}
t, err := tool.Load(tf)
if err != nil {
fmt.Fprintf(tw, "Error:\t%s\n", err)
} else {
params := []string{}
for name, p := range t.Function.Parameters.Properties {
params = append(params, fmt.Sprintf("%s %s", name, p.Type))
}
function := fmt.Sprintf("%s(%s)", t.Function.Name, strings.Join(params, ", "))

cmd := strings.Join(append([]string{t.Cmd}, t.Args...), " ")

format := "Function:\t%s\nDescription:\t%s\nCommand:\t%s\n"
if _, err := fmt.Fprintf(tw, format, function, t.Function.Description, cmd); err != nil {
return err
}
printToolInfo(tw, t)
}

if err := tw.Flush(); err != nil {
Expand All @@ -62,3 +48,16 @@ func (t *Tool) Run(context *kong.Context, cli *CLI) error {

return nil
}

func printToolInfo(w io.Writer, t *tool.Tool) {
params := []string{}
for name, p := range t.Function.Parameters.Properties {
params = append(params, fmt.Sprintf("%s %s", name, p.Type))
}
function := fmt.Sprintf("%s(%s)", t.Function.Name, strings.Join(params, ", "))

cmd := strings.Join(append([]string{t.Cmd}, t.Args...), " ")

format := "Function:\t%s\nDescription:\t%s\nCommand:\t%s\n"
fmt.Fprintf(w, format, function, t.Function.Description, cmd)
}
13 changes: 7 additions & 6 deletions pkg/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,22 @@ func Load(file string) (*Tool, error) {

// LoadAll loads a list of tools from a directory.
func LoadAll(dir string) (map[string]*Tool, error) {
tools := make(map[string]*Tool)

// Get the list of json files in the directory
files, err := ListFiles(dir)
if err != nil {
return tools, err
}

// Load the tools concurrently, pool of 10 goroutines max
tools := make(map[string]*Tool, len(files))
lock := sync.Mutex{}
eg := new(errgroup.Group)
eg.SetLimit(10)
for _, f := range files {
file := f // capture the loop variable
eg.Go(func() error {
tool, err := Load(file) //nolint:govet // Shadowing err is fine here.
tool, err := Load(file)
if err != nil {
return err
}
Expand All @@ -70,10 +74,7 @@ func LoadAll(dir string) (map[string]*Tool, error) {
})
}

if err = eg.Wait(); err != nil {
return nil, err
}
return tools, nil
return tools, eg.Wait()
}

// ListToolFiles lists the tool files in a directory.
Expand Down

0 comments on commit fc52b11

Please sign in to comment.