Skip to content

Commit c558ca5

Browse files
Add sys.remove
1 parent 90a63c4 commit c558ca5

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

examples/sqlite-download.gpt

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
tools: sys.download, sys.exec
1+
tools: sys.download, sys.exec, sys.remove
22

3-
Download https://www.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip to the
4-
local directory. Expand the archive as there is a sqlite database in it.
3+
Download https://www.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip to a
4+
random file. Then expand the archive to a temporary location as there is a sqlite
5+
database in it.
56

67
First inspect the schema of the database to understand the table structure.
78

89
Form and run a SQL query to find the artist with the most number of albums and output
910
the result of that.
11+
12+
When done remove the database file and the downloaded content.

pkg/builtin/builtin.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ var tools = map[string]types.Tool{
8585
Description: "Downloads a URL, saving the contents to disk at a given location",
8686
Arguments: types.ObjectSchema(
8787
"url", "The URL to download, either http or https.",
88-
"location", "(optional) The on disk location to store the file. If no location is specified a temp location will be used. If the target file already exists it will not be overwritten and will fail."),
88+
"location", "(optional) The on disk location to store the file. If no location is specified a temp location will be used. If the target file already exists it will fail unless override is set to true.",
89+
"override", "If true and a file at the location exists, the file will be overwritten, otherwise fail. Default is false"),
8990
BuiltinFunc: SysDownload,
9091
},
92+
"sys.remove": {
93+
Description: "Removes the specified files",
94+
Arguments: types.ObjectSchema(
95+
"location", "The file to remove"),
96+
BuiltinFunc: SysRemove,
97+
},
9198
}
9299

93100
func SysProgram() *types.Program {
@@ -317,10 +324,22 @@ func SysAbort(ctx context.Context, env []string, input string) (string, error) {
317324
return "", fmt.Errorf("ABORT: %s", params.Message)
318325
}
319326

327+
func SysRemove(ctx context.Context, env []string, input string) (string, error) {
328+
var params struct {
329+
Location string `json:"location,omitempty"`
330+
}
331+
if err := json.Unmarshal([]byte(input), &params); err != nil {
332+
return "", err
333+
}
334+
335+
return fmt.Sprintf("Removed file: %s", params.Location), os.Remove(params.Location)
336+
}
337+
320338
func SysDownload(ctx context.Context, env []string, input string) (string, error) {
321339
var params struct {
322340
URL string `json:"url,omitempty"`
323341
Location string `json:"location,omitempty"`
342+
Override bool `json:"override,omitempty"`
324343
}
325344
if err := json.Unmarshal([]byte(input), &params); err != nil {
326345
return "", err
@@ -339,9 +358,9 @@ func SysDownload(ctx context.Context, env []string, input string) (string, error
339358
params.Location = f.Name()
340359
}
341360

342-
if checkExists {
361+
if checkExists && !params.Override {
343362
if _, err := os.Stat(params.Location); err == nil {
344-
return "", fmt.Errorf("file %s already exists and can not be overwritten: %w", params.Location, err)
363+
return "", fmt.Errorf("file %s already exists and can not be overwritten", params.Location)
345364
} else if err != nil && !errors.Is(err, fs.ErrNotExist) {
346365
return "", err
347366
}
@@ -361,6 +380,7 @@ func SysDownload(ctx context.Context, env []string, input string) (string, error
361380
return "", fmt.Errorf("invalid status code [%d] downloading [%s]: %s", resp.StatusCode, params.URL, resp.Status)
362381
}
363382

383+
_ = os.Remove(params.Location)
364384
f, err := os.Create(params.Location)
365385
if err != nil {
366386
return "", fmt.Errorf("failed to create [%s]: %w", params.Location, err)

0 commit comments

Comments
 (0)