81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package toolsrv
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"ollie/paths"
|
|
)
|
|
|
|
// ToolsPath returns the directory to search for named tool scripts.
|
|
// Resolved in order: first entry of OLLIE_TOOLS_PATH (colon-separated),
|
|
// then ~/.config/ollie/tools.
|
|
func ToolsPath() string {
|
|
if p := os.Getenv("OLLIE_TOOLS_PATH"); p != "" {
|
|
if i := strings.Index(p, ":"); i >= 0 {
|
|
p = p[:i]
|
|
}
|
|
return p
|
|
}
|
|
return paths.CfgDir() + "/tools"
|
|
}
|
|
|
|
// DiscoverTools scans the tools directory for .meta files and returns
|
|
// metadata for all tools.
|
|
func DiscoverTools() []ToolInfo {
|
|
dir := ToolsPath()
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var infos []ToolInfo
|
|
for _, e := range entries {
|
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".meta") {
|
|
continue
|
|
}
|
|
name := strings.TrimSuffix(e.Name(), ".meta")
|
|
data, err := os.ReadFile(filepath.Join(dir, e.Name()))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var m MetaFile
|
|
if err := json.Unmarshal(data, &m); err != nil {
|
|
continue
|
|
}
|
|
info := ToolInfoFromMeta(name, &m)
|
|
if info.Name == "" {
|
|
continue // no variant matched — tool unavailable on this host
|
|
}
|
|
infos = append(infos, info)
|
|
}
|
|
return infos
|
|
}
|
|
|
|
// ToolPrompt returns the full prompt content for a named tool.
|
|
// Returns "" if the tool has no .meta file or no prompt.
|
|
func ToolPrompt(name string) string {
|
|
if strings.Contains(name, "/") || strings.Contains(name, "..") {
|
|
return ""
|
|
}
|
|
m, err := LoadMetaFile(name)
|
|
if err != nil || m == nil {
|
|
return ""
|
|
}
|
|
return m.Prompt
|
|
}
|
|
|
|
// ReadTool reads a named tool script from the tools directory.
|
|
func ReadTool(name string) (string, error) {
|
|
if strings.Contains(name, "/") || strings.Contains(name, "..") {
|
|
return "", os.ErrNotExist
|
|
}
|
|
path := filepath.Join(ToolsPath(), name)
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(data), nil
|
|
}
|