@@ -16,7 +16,6 @@ import (
1616 "github.com/BurntSushi/locker"
1717 "github.com/gptscript-ai/gptscript/pkg/config"
1818 "github.com/gptscript-ai/gptscript/pkg/credentials"
19- runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
2019 "github.com/gptscript-ai/gptscript/pkg/hash"
2120 "github.com/gptscript-ai/gptscript/pkg/repos/git"
2221 "github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
@@ -55,10 +54,10 @@ func (n noopRuntime) Setup(_ context.Context, _ types.Tool, _, _ string, _ []str
5554}
5655
5756type Manager struct {
57+ cacheDir string
5858 storageDir string
5959 gitDir string
6060 runtimeDir string
61- credHelperDirs credentials.CredentialHelperDirs
6261 runtimes []Runtime
6362 credHelperConfig * credHelperConfig
6463}
@@ -72,11 +71,11 @@ type credHelperConfig struct {
7271func New (cacheDir string , runtimes ... Runtime ) * Manager {
7372 root := filepath .Join (cacheDir , "repos" )
7473 return & Manager {
75- storageDir : root ,
76- gitDir : filepath . Join ( root , "git" ) ,
77- runtimeDir : filepath .Join (root , "runtimes " ),
78- credHelperDirs : credentials . GetCredentialHelperDirs ( cacheDir ),
79- runtimes : runtimes ,
74+ cacheDir : cacheDir ,
75+ storageDir : root ,
76+ gitDir : filepath .Join (root , "git " ),
77+ runtimeDir : filepath . Join ( root , "runtimes" ),
78+ runtimes : runtimes ,
8079 }
8180}
8281
@@ -110,50 +109,59 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
110109 distInfo , suffix string
111110 )
112111 // The file helper is built-in and does not need to be downloaded.
113- if helperName == "file" {
112+ if helperName == config . FileCredHelper {
114113 return nil
115114 }
116115 switch helperName {
117- case "wincred" :
116+ case config . WincredCredHelper :
118117 suffix = ".exe"
119118 default :
120119 distInfo = fmt .Sprintf ("-%s-%s" , runtime .GOOS , runtime .GOARCH )
121120 }
122121
123- locker .Lock ("gptscript-credential-helpers" )
124- defer locker .Unlock ("gptscript-credential-helpers" )
122+ repoName := credentials .RepoNameForCredentialStore (helperName )
123+
124+ locker .Lock (repoName )
125+ defer locker .Unlock (repoName )
126+
127+ credHelperDirs := credentials .GetCredentialHelperDirs (m .cacheDir , helperName )
125128
126129 // Load the last-checked file to make sure we haven't checked the repo in the last 24 hours.
127130 now := time .Now ()
128- lastChecked , err := os .ReadFile (m . credHelperDirs .LastCheckedFile )
131+ lastChecked , err := os .ReadFile (credHelperDirs .LastCheckedFile )
129132 if err == nil {
130133 if t , err := time .Parse (time .RFC3339 , strings .TrimSpace (string (lastChecked ))); err == nil && now .Sub (t ) < 24 * time .Hour {
131134 // Make sure the binary still exists, and if it does, return.
132- if _ , err := os .Stat (filepath .Join (m . credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix )); err == nil {
135+ if _ , err := os .Stat (filepath .Join (credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix )); err == nil {
133136 log .Debugf ("Credential helper %s up-to-date as of %v, checking for updates after %v" , helperName , t , t .Add (24 * time .Hour ))
134137 return nil
135138 }
136139 }
137140 }
138141
139- if err := os .MkdirAll (filepath .Dir (m . credHelperDirs .LastCheckedFile ), 0755 ); err != nil {
142+ if err := os .MkdirAll (filepath .Dir (credHelperDirs .LastCheckedFile ), 0755 ); err != nil {
140143 return err
141144 }
142145
143146 // Update the last-checked file.
144- if err := os .WriteFile (m .credHelperDirs .LastCheckedFile , []byte (now .Format (time .RFC3339 )), 0644 ); err != nil {
147+ if err := os .WriteFile (credHelperDirs .LastCheckedFile , []byte (now .Format (time .RFC3339 )), 0644 ); err != nil {
148+ return err
149+ }
150+
151+ gitURL , err := credentials .GitURLForRepoName (repoName )
152+ if err != nil {
145153 return err
146154 }
147155
148156 tool := types.Tool {
149157 ToolDef : types.ToolDef {
150158 Parameters : types.Parameters {
151- Name : "gptscript-credential-helpers" ,
159+ Name : repoName ,
152160 },
153161 },
154162 Source : types.ToolSource {
155163 Repo : & types.Repo {
156- Root : runtimeEnv . VarOrDefault ( "GPTSCRIPT_CRED_HELPERS_ROOT" , "https://github.com/gptscript-ai/gptscript-credential-helpers.git" ) ,
164+ Root : gitURL ,
157165 },
158166 },
159167 }
@@ -164,12 +172,12 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
164172
165173 var needsDownloaded bool
166174 // Check the last revision shasum and see if it is different from the current one.
167- lastRevision , err := os .ReadFile (m . credHelperDirs .RevisionFile )
175+ lastRevision , err := os .ReadFile (credHelperDirs .RevisionFile )
168176 if (err == nil && strings .TrimSpace (string (lastRevision )) != tool .Source .Repo .Root + tag ) || errors .Is (err , fs .ErrNotExist ) {
169177 // Need to pull the latest version.
170178 needsDownloaded = true
171179 // Update the revision file to the new revision.
172- if err = os .WriteFile (m . credHelperDirs .RevisionFile , []byte (tool .Source .Repo .Root + tag ), 0644 ); err != nil {
180+ if err = os .WriteFile (credHelperDirs .RevisionFile , []byte (tool .Source .Repo .Root + tag ), 0644 ); err != nil {
173181 return err
174182 }
175183 } else if err != nil {
@@ -179,15 +187,15 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
179187 if ! needsDownloaded {
180188 // Check for the existence of the credential helper binary.
181189 // If it's there, we have no need to download it and can just return.
182- if _ , err = os .Stat (filepath .Join (m . credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix )); err == nil {
190+ if _ , err = os .Stat (filepath .Join (credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix )); err == nil {
183191 return nil
184192 }
185193 }
186194
187195 // Find the Go runtime and use it to build the credential helper.
188196 for _ , rt := range m .runtimes {
189197 if strings .HasPrefix (rt .ID (), "go" ) {
190- return rt .(* golang.Runtime ).DownloadCredentialHelper (ctx , tool , helperName , distInfo , suffix , m . credHelperDirs .BinDir )
198+ return rt .(* golang.Runtime ).DownloadCredentialHelper (ctx , tool , helperName , distInfo , suffix , credHelperDirs .BinDir )
191199 }
192200 }
193201
0 commit comments