| Sketch🕴️ | 305f817 | 2026-02-27 13:58:43 +0400 | [diff] [blame^] | 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | ) |
| 6 | |
| 7 | type FileReadArgs struct { |
| 8 | Path string `json:"path"` |
| 9 | } |
| 10 | |
| 11 | func FileRead(args FileReadArgs) (string, error) { |
| 12 | if b, err := os.ReadFile(args.Path); err != nil { |
| 13 | return "", err |
| 14 | } else { |
| 15 | return string(b), nil |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | type FileWriteArgs struct { |
| 20 | Path string `json:"path"` |
| 21 | Contents string `json:"contents"` |
| 22 | } |
| 23 | |
| 24 | type FileWriteResult struct { |
| 25 | } |
| 26 | |
| 27 | func FileWrite(args FileWriteArgs) (string, error) { |
| 28 | if err := os.WriteFile(args.Path, []byte(args.Contents), 0666); err != nil { |
| 29 | return "error", err |
| 30 | } else { |
| 31 | return "done", nil |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | type DirListArgs struct { |
| 36 | Name string `json:"name"` |
| 37 | } |
| 38 | |
| 39 | type DirEntry struct { |
| 40 | Name string `json:"name"` |
| 41 | IsDir bool `json:"is_dir"` |
| 42 | } |
| 43 | |
| 44 | type DirListResult struct { |
| 45 | Entries []DirEntry `json:"entries"` |
| 46 | } |
| 47 | |
| 48 | func DirList(args DirListArgs) (DirListResult, error) { |
| 49 | dir := "." |
| 50 | if args.Name != "" { |
| 51 | dir = args.Name |
| 52 | } |
| 53 | entries, err := os.ReadDir(dir) |
| 54 | if err != nil { |
| 55 | return DirListResult{}, err |
| 56 | } |
| 57 | var ret DirListResult |
| 58 | for _, e := range entries { |
| 59 | ret.Entries = append(ret.Entries, DirEntry{ |
| 60 | Name: e.Name(), |
| 61 | IsDir: e.IsDir(), |
| 62 | }) |
| 63 | } |
| 64 | return ret, nil |
| 65 | } |