Skip to content

Commit bd9f9b9

Browse files
Merge pull request #72 from cloudnautique/main
enhance - add sys.stat built in tool
2 parents d4981fa + 5a91c64 commit bd9f9b9

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
@@ -128,6 +128,15 @@ var tools = map[string]types.Tool{
128128
},
129129
BuiltinFunc: SysRemove,
130130
},
131+
"sys.stat": {
132+
Parameters: types.Parameters{
133+
Description: "Gets size, modfied time, and mode of the specified file",
134+
Arguments: types.ObjectSchema(
135+
"filepath", "The complete path and filename of the file",
136+
),
137+
},
138+
BuiltinFunc: SysStat,
139+
},
131140
}
132141

133142
func SysProgram() *types.Program {
@@ -428,6 +437,22 @@ func SysRemove(ctx context.Context, env []string, input string) (string, error)
428437
return fmt.Sprintf("Removed file: %s", params.Location), os.Remove(params.Location)
429438
}
430439

440+
func SysStat(ctx context.Context, env []string, input string) (string, error) {
441+
var params struct {
442+
Filepath string `json:"filepath,omitempty"`
443+
}
444+
if err := json.Unmarshal([]byte(input), &params); err != nil {
445+
return "", err
446+
}
447+
448+
stat, err := os.Stat(params.Filepath)
449+
if err != nil {
450+
return "", err
451+
}
452+
453+
return fmt.Sprintf("File %s mode: %s, size: %d bytes, modtime: %s", params.Filepath, stat.Mode().String(), stat.Size(), stat.ModTime().String()), nil
454+
}
455+
431456
func SysDownload(ctx context.Context, env []string, input string) (_ string, err error) {
432457
var params struct {
433458
URL string `json:"url,omitempty"`

0 commit comments

Comments
 (0)