From c558ca5299cdea231a7b333711666ba144454627 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 9 Feb 2024 22:38:56 -0700 Subject: [PATCH] Add sys.remove --- examples/sqlite-download.gpt | 9 ++++++--- pkg/builtin/builtin.go | 26 +++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/examples/sqlite-download.gpt b/examples/sqlite-download.gpt index 6364bc35..53166545 100644 --- a/examples/sqlite-download.gpt +++ b/examples/sqlite-download.gpt @@ -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. diff --git a/pkg/builtin/builtin.go b/pkg/builtin/builtin.go index c20aace9..b10e101a 100644 --- a/pkg/builtin/builtin.go +++ b/pkg/builtin/builtin.go @@ -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 { @@ -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), ¶ms); 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), ¶ms); err != nil { return "", err @@ -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 } @@ -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)