@@ -8,22 +8,21 @@ import (
88 "io/fs"
99 "os"
1010 "path/filepath"
11+ "runtime"
1112 "strings"
1213 "sync"
1314 "time"
1415
1516 "github.com/BurntSushi/locker"
1617 "github.com/gptscript-ai/gptscript/pkg/config"
1718 "github.com/gptscript-ai/gptscript/pkg/credentials"
19+ runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
1820 "github.com/gptscript-ai/gptscript/pkg/hash"
19- "github.com/gptscript-ai/gptscript/pkg/loader/github"
2021 "github.com/gptscript-ai/gptscript/pkg/repos/git"
2122 "github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
2223 "github.com/gptscript-ai/gptscript/pkg/types"
2324)
2425
25- const credentialHelpersRepo = "github.com/gptscript-ai/gptscript-credential-helpers"
26-
2726type Runtime interface {
2827 ID () string
2928 Supports (tool types.Tool , cmd []string ) bool
@@ -68,7 +67,6 @@ type credHelperConfig struct {
6867 lock sync.Mutex
6968 initialized bool
7069 cliCfg * config.CLIConfig
71- env []string
7270}
7371
7472func New (cacheDir string , runtimes ... Runtime ) * Manager {
@@ -90,7 +88,7 @@ func (m *Manager) EnsureCredentialHelpers(ctx context.Context) error {
9088 defer m .credHelperConfig .lock .Unlock ()
9189
9290 if ! m .credHelperConfig .initialized {
93- if err := m .deferredSetUpCredentialHelpers (ctx , m .credHelperConfig .cliCfg , m . credHelperConfig . env ); err != nil {
91+ if err := m .deferredSetUpCredentialHelpers (ctx , m .credHelperConfig .cliCfg ); err != nil {
9492 return err
9593 }
9694 m .credHelperConfig .initialized = true
@@ -99,27 +97,28 @@ func (m *Manager) EnsureCredentialHelpers(ctx context.Context) error {
9997 return nil
10098}
10199
102- func (m * Manager ) SetUpCredentialHelpers (_ context.Context , cliCfg * config.CLIConfig , env [] string ) error {
100+ func (m * Manager ) SetUpCredentialHelpers (_ context.Context , cliCfg * config.CLIConfig ) error {
103101 m .credHelperConfig = & credHelperConfig {
104102 cliCfg : cliCfg ,
105- env : env ,
106103 }
107104 return nil
108105}
109106
110- func (m * Manager ) deferredSetUpCredentialHelpers (ctx context.Context , cliCfg * config.CLIConfig , env [] string ) error {
107+ func (m * Manager ) deferredSetUpCredentialHelpers (ctx context.Context , cliCfg * config.CLIConfig ) error {
111108 var (
112- helperName = cliCfg .CredentialsStore
113- suffix string
109+ helperName = cliCfg .CredentialsStore
110+ distInfo , suffix string
114111 )
115- if helperName == "wincred" {
116- suffix = ".exe"
117- }
118-
119- // The file helper is built-in and does not need to be compiled.
112+ // The file helper is built-in and does not need to be downloaded.
120113 if helperName == "file" {
121114 return nil
122115 }
116+ switch helperName {
117+ case "wincred" :
118+ suffix = ".exe"
119+ default :
120+ distInfo = fmt .Sprintf ("-%s-%s" , runtime .GOOS , runtime .GOARCH )
121+ }
123122
124123 locker .Lock ("gptscript-credential-helpers" )
125124 defer locker .Unlock ("gptscript-credential-helpers" )
@@ -137,13 +136,7 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
137136 }
138137 }
139138
140- // Load the credential helpers repo information.
141- _ , _ , repo , _ , err := github .Load (ctx , nil , credentialHelpersRepo )
142- if err != nil {
143- return err
144- }
145-
146- if err := os .MkdirAll (m .credHelperDirs .HelperDir , 0755 ); err != nil {
139+ if err := os .MkdirAll (filepath .Dir (m .credHelperDirs .LastCheckedFile ), 0755 ); err != nil {
147140 return err
148141 }
149142
@@ -152,37 +145,44 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
152145 return err
153146 }
154147
155- var needsBuild bool
148+ tool := types.Tool {
149+ Source : types.ToolSource {
150+ Repo : & types.Repo {
151+ Root : runtimeEnv .VarOrDefault ("GPTSCRIPT_CRED_HELPERS_ROOT" , "https://github.com/gptscript-ai/gptscript-credential-helpers.git" ),
152+ },
153+ },
154+ }
155+ tag , err := golang .GetLatestTag (tool )
156+ if err != nil {
157+ return err
158+ }
156159
160+ var needsDownloaded bool
157161 // Check the last revision shasum and see if it is different from the current one.
158162 lastRevision , err := os .ReadFile (m .credHelperDirs .RevisionFile )
159- if (err == nil && strings .TrimSpace (string (lastRevision )) != repo . Revision ) || errors .Is (err , fs .ErrNotExist ) {
163+ if (err == nil && strings .TrimSpace (string (lastRevision )) != tool . Source . Repo . Root + tag ) || errors .Is (err , fs .ErrNotExist ) {
160164 // Need to pull the latest version.
161- needsBuild = true
162- if err := git .Checkout (ctx , m .gitDir , repo .Root , repo .Revision , filepath .Join (m .credHelperDirs .RepoDir , repo .Revision )); err != nil {
163- return err
164- }
165+ needsDownloaded = true
165166 // Update the revision file to the new revision.
166- if err : = os .WriteFile (m .credHelperDirs .RevisionFile , []byte (repo . Revision ), 0644 ); err != nil {
167+ if err = os .WriteFile (m .credHelperDirs .RevisionFile , []byte (tool . Source . Repo . Root + tag ), 0644 ); err != nil {
167168 return err
168169 }
169170 } else if err != nil {
170171 return err
171172 }
172173
173- if ! needsBuild {
174- // Check for the existence of the gptscript- credential-osxkeychain binary.
175- // If it's there, we have no need to build it and can just return.
176- if _ , err : = os .Stat (filepath .Join (m .credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix )); err == nil {
174+ if ! needsDownloaded {
175+ // Check for the existence of the credential helper binary.
176+ // If it's there, we have no need to download it and can just return.
177+ if _ , err = os .Stat (filepath .Join (m .credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix )); err == nil {
177178 return nil
178179 }
179180 }
180181
181182 // Find the Go runtime and use it to build the credential helper.
182- for _ , runtime := range m .runtimes {
183- if strings .HasPrefix (runtime .ID (), "go" ) {
184- goRuntime := runtime .(* golang.Runtime )
185- return goRuntime .BuildCredentialHelper (ctx , helperName , m .credHelperDirs , m .runtimeDir , repo .Revision , env )
183+ for _ , rt := range m .runtimes {
184+ if strings .HasPrefix (rt .ID (), "go" ) {
185+ return rt .(* golang.Runtime ).DownloadCredentialHelper (ctx , tool , helperName , distInfo , suffix , m .credHelperDirs .BinDir )
186186 }
187187 }
188188
0 commit comments