From 349a9fa5dc10fc773c22f09c9661436df67483e3 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Tue, 10 Sep 2024 17:18:43 -0400 Subject: [PATCH] chore: bubble up errors when downloading credential helper fails Signed-off-by: Donnie Adams --- pkg/repos/get.go | 5 ++++ pkg/repos/runtimes/golang/golang.go | 37 ++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pkg/repos/get.go b/pkg/repos/get.go index 8981d1fa..8346b8cf 100644 --- a/pkg/repos/get.go +++ b/pkg/repos/get.go @@ -146,6 +146,11 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co } tool := types.Tool{ + ToolDef: types.ToolDef{ + Parameters: types.Parameters{ + Name: "gptscript-credential-helpers", + }, + }, Source: types.ToolSource{ Repo: &types.Repo{ Root: runtimeEnv.VarOrDefault("GPTSCRIPT_CRED_HELPERS_ROOT", "https://github.com/gptscript-ai/gptscript-credential-helpers.git"), diff --git a/pkg/repos/runtimes/golang/golang.go b/pkg/repos/runtimes/golang/golang.go index 2601f521..47e8461f 100644 --- a/pkg/repos/runtimes/golang/golang.go +++ b/pkg/repos/runtimes/golang/golang.go @@ -97,21 +97,26 @@ type tag struct { } func GetLatestTag(tool types.Tool) (string, error) { - r, ok := getLatestRelease(tool) + r, ok, err := getLatestRelease(tool) + if err != nil { + return "", err + } + if !ok { return "", fmt.Errorf("failed to get latest release for %s", tool.Name) } + return r.label, nil } -func getLatestRelease(tool types.Tool) (*release, bool) { +func getLatestRelease(tool types.Tool) (*release, bool, error) { if tool.Source.Repo == nil || !strings.HasPrefix(tool.Source.Repo.Root, "https://github.com/") { - return nil, false + return nil, false, nil } parts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(tool.Source.Repo.Root, ".git"), "https://"), "/") if len(parts) != 3 { - return nil, false + return nil, false, fmt.Errorf("invalid GitHub URL: %s", tool.Source.Repo.Root) } client := http.Client{ @@ -124,17 +129,16 @@ func getLatestRelease(tool types.Tool) (*release, bool) { resp, err := client.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", account, repo)) if err != nil { - // ignore error - return nil, false + return nil, false, fmt.Errorf("failed to get tags: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return nil, false + return nil, false, fmt.Errorf("unexpected status when getting tags: %s", resp.Status) } var tags []tag if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil { - return nil, false + return nil, false, fmt.Errorf("failed to decode GitHub tags: %w", err) } for _, tag := range tags { if tag.Commit.Sha == tool.Source.Repo.Revision { @@ -142,23 +146,22 @@ func getLatestRelease(tool types.Tool) (*release, bool) { account: account, repo: repo, label: tag.Name, - }, true + }, true, nil } } resp, err = client.Get(fmt.Sprintf("https://github.com/%s/%s/releases/latest", account, repo)) if err != nil { - // ignore error - return nil, false + return nil, false, fmt.Errorf("failed to get latest release: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusFound { - return nil, false + return nil, false, fmt.Errorf("unexpected status when getting latest release: %s", resp.Status) } target := resp.Header.Get("Location") if target == "" { - return nil, false + return nil, false, nil } parts = strings.Split(target, "/") @@ -168,7 +171,7 @@ func getLatestRelease(tool types.Tool) (*release, bool) { account: account, repo: repo, label: label, - }, true + }, true, nil } func get(ctx context.Context, url string) (*http.Response, error) { @@ -249,7 +252,8 @@ func (r *Runtime) Binary(ctx context.Context, tool types.Tool, _, toolSource str return false, nil, nil } - rel, ok := getLatestRelease(tool) + // ignore the error + rel, ok, _ := getLatestRelease(tool) if !ok { return false, nil, nil } @@ -286,7 +290,8 @@ func (r *Runtime) DownloadCredentialHelper(ctx context.Context, tool types.Tool, return nil } - rel, ok := getLatestRelease(tool) + // ignore the error + rel, ok, _ := getLatestRelease(tool) if !ok { return fmt.Errorf("failed to find %s release", r.ID()) }