Skip to content

Commit

Permalink
Add sys.remove
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 10, 2024
1 parent 90a63c4 commit c558ca5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 6 additions & 3 deletions examples/sqlite-download.gpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
tools: sys.download, sys.exec
tools: sys.download, sys.exec, sys.remove

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

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

Form and run a SQL query to find the artist with the most number of albums and output
the result of that.

When done remove the database file and the downloaded content.
26 changes: 23 additions & 3 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ var tools = map[string]types.Tool{
Description: "Downloads a URL, saving the contents to disk at a given location",
Arguments: types.ObjectSchema(
"url", "The URL to download, either http or https.",
"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."),
"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.",
"override", "If true and a file at the location exists, the file will be overwritten, otherwise fail. Default is false"),
BuiltinFunc: SysDownload,
},
"sys.remove": {
Description: "Removes the specified files",
Arguments: types.ObjectSchema(
"location", "The file to remove"),
BuiltinFunc: SysRemove,
},
}

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

func SysRemove(ctx context.Context, env []string, input string) (string, error) {
var params struct {
Location string `json:"location,omitempty"`
}
if err := json.Unmarshal([]byte(input), &params); err != nil {
return "", err
}

return fmt.Sprintf("Removed file: %s", params.Location), os.Remove(params.Location)
}

func SysDownload(ctx context.Context, env []string, input string) (string, error) {
var params struct {
URL string `json:"url,omitempty"`
Location string `json:"location,omitempty"`
Override bool `json:"override,omitempty"`
}
if err := json.Unmarshal([]byte(input), &params); err != nil {
return "", err
Expand All @@ -339,9 +358,9 @@ func SysDownload(ctx context.Context, env []string, input string) (string, error
params.Location = f.Name()
}

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

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

0 comments on commit c558ca5

Please sign in to comment.