blob: 815c2eb04c148d0948bd74d9cedb1877cce5ee57 [file] [log] [blame]
Sketch🕴️305f8172026-02-27 13:58:43 +04001package tools
2
3import (
4 "os"
5)
6
7type FileReadArgs struct {
8 Path string `json:"path"`
9}
10
11func 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
19type FileWriteArgs struct {
20 Path string `json:"path"`
21 Contents string `json:"contents"`
22}
23
24type FileWriteResult struct {
25}
26
27func 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
35type DirListArgs struct {
36 Name string `json:"name"`
37}
38
39type DirEntry struct {
40 Name string `json:"name"`
41 IsDir bool `json:"is_dir"`
42}
43
44type DirListResult struct {
45 Entries []DirEntry `json:"entries"`
46}
47
48func 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}