Files
ARTEX/third_party/norma/tool/ls.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

72 lines
1.8 KiB
Go

package tool
import (
"context"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
)
// NewLS builds the LS tool: lists the entries of a directory.
func NewLS() CoreTool {
return Build(Spec{
Name: "LS",
Description: "Lists the files and subdirectories of a directory (non-recursive). Use Glob to match by pattern across depth, or Grep to search contents.",
Prompt: "path may be absolute or relative to the working directory; defaults to the working directory.",
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"path": map[string]any{"type": "string", "description": "Directory to list (default working directory)."},
},
},
ReadOnly: always,
Concurrent: always,
Permissions: allowReadOnly,
Run: runLS,
})
}
func runLS(_ context.Context, input json.RawMessage, tc *ToolContext) (Result, error) {
var in struct {
Path string `json:"path"`
}
if err := json.Unmarshal(input, &in); err != nil {
return Result{}, err
}
dir := tc.WorkingDir
if in.Path != "" {
dir = resolvePath(tc, in.Path)
}
if dir == "" {
dir = "."
}
entries, err := os.ReadDir(dir)
if err != nil {
return Errorf(fmt.Sprintf("Error listing %s: %v", in.Path, err)), nil
}
if len(entries) == 0 {
return Text("(empty directory)"), nil
}
sort.Slice(entries, func(i, j int) bool {
if entries[i].IsDir() != entries[j].IsDir() {
return entries[i].IsDir() // directories first
}
return entries[i].Name() < entries[j].Name()
})
var b strings.Builder
for _, e := range entries {
if e.IsDir() {
fmt.Fprintf(&b, "%s/\n", e.Name())
continue
}
var size int64
if info, err := e.Info(); err == nil {
size = info.Size()
}
fmt.Fprintf(&b, "%s\t%d bytes\n", e.Name(), size)
}
return Text(Capture(tc, b.String())), nil
}