Skip to content

Commit 5a91c64

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 118ea59 commit 5a91c64

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

docs/docs/tools/system.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,106 @@
11
# System Tools
2+
23
GPTScript comes with a set of system tools that provide various core functionalities.
34

45
## sys.abort
6+
57
Aborts the operation and provides an error message.
8+
69
#### Arguments
10+
711
- `message`: The description of the error or unexpected result that caused abort to be called.
812

913
## sys.download
14+
1015
Downloads a file from a specified URL to an optional location on disk with an option to override existing files.
16+
1117
#### Arguments
18+
1219
- `location` (optional): The on-disk location to store the downloaded file.
1320
- `override`: If true, allows overwriting of an existing file. Default is false.
1421
- `url`: The HTTP or HTTPS URL of the file to be downloaded.
1522

1623
## sys.exec
24+
1725
Executes a command with the ability to specify command arguments and the working directory.
26+
1827
#### Arguments
28+
1929
- `command`: The full command to run, including all arguments.
2030
- `directory`: The working directory for the command. Defaults to the current directory ".".
2131

2232
## sys.find
33+
2334
Searches for files within a directory that match a given pattern using Unix glob format.
35+
2436
#### Arguments
37+
2538
- `directory`: The directory to perform the search in. Defaults to the current directory ".".
2639
- `pattern`: The pattern to match against filenames.
2740

2841
## sys.getenv
42+
2943
Retrieves the value of an environment variable.
44+
3045
#### Arguments
46+
3147
- `name`: The name of the environment variable to retrieve.
3248

3349
## sys.http.get
50+
3451
Performs an HTTP GET request to the specified URL.
52+
3553
#### Arguments
54+
3655
- `url`: The URL to perform the GET request.
3756

3857
## sys.http.html2text
58+
3959
Converts the HTML content from a given URL to plain text.
60+
4061
#### Arguments
62+
4163
- `url`: The URL of the HTML content to be converted.
4264

4365
## sys.http.post
66+
4467
Sends an HTTP POST request with given content to a specified URL.
68+
4569
#### Arguments
70+
4671
- `content`: The content to be posted.
4772
- `contentType`: The MIME type of the content being posted.
4873
- `url`: The URL to which the POST request should be sent.
4974

5075
## sys.read
76+
5177
Reads the content from a specified file.
78+
5279
#### Arguments
80+
5381
- `filename`: The name of the file from which to read content.
5482

5583
## sys.remove
84+
5685
Removes a file from the specified location.
86+
5787
#### Arguments
88+
5889
- `location`: The path to the file that needs to be removed.
5990

91+
## sys.stat
92+
93+
Retrieves the status of a file, such as size, permissions, and last modified time.
94+
95+
#### Arguments
96+
97+
- `filepath`: The path to the file for which to retrieve status.
98+
6099
## sys.write
100+
61101
Writes content to a specified file.
102+
62103
#### Arguments
104+
63105
- `content`: The content to be written to the file.
64106
- `filename`: The filename where the content should be written.

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.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: SysStat,
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 SysStat(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)