Skip to content

Commit ce3e20c

Browse files
committed
enhance - add sys.file.stat built in tool
created a built in tool that stats a file and returns the size, mode and mod time. Signed-off-by: Bill Maxwell <[email protected]>
1 parent d41990a commit ce3e20c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pkg/builtin/builtin.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ var tools = map[string]types.Tool{
118118
},
119119
BuiltinFunc: SysRemove,
120120
},
121+
"sys.file.stat": {
122+
Parameters: types.Parameters{
123+
Description: "Gets size, modfied time, and mode of the specified file",
124+
Arguments: types.ObjectSchema(
125+
"filepath", "The complete path and filename of the file",
126+
),
127+
},
128+
BuiltinFunc: SysFileStat,
129+
},
121130
}
122131

123132
func SysProgram() *types.Program {
@@ -377,6 +386,22 @@ func SysRemove(ctx context.Context, env []string, input string) (string, error)
377386
return fmt.Sprintf("Removed file: %s", params.Location), os.Remove(params.Location)
378387
}
379388

389+
func SysFileStat(ctx context.Context, env []string, input string) (string, error) {
390+
var params struct {
391+
Filepath string `json:"filepath,omitempty"`
392+
}
393+
if err := json.Unmarshal([]byte(input), &params); err != nil {
394+
return "", err
395+
}
396+
397+
stat, err := os.Stat(params.Filepath)
398+
if err != nil {
399+
return "", err
400+
}
401+
402+
return fmt.Sprintf("File %s mode: %s, size: %d bytes, modtime: %s", params.Filepath, stat.Mode().String(), stat.Size(), stat.ModTime().String()), nil
403+
}
404+
380405
func SysDownload(ctx context.Context, env []string, input string) (_ string, err error) {
381406
var params struct {
382407
URL string `json:"url,omitempty"`

0 commit comments

Comments
 (0)