@@ -16,7 +16,6 @@ import (
16
16
"github.com/BurntSushi/locker"
17
17
"github.com/gptscript-ai/gptscript/pkg/config"
18
18
"github.com/gptscript-ai/gptscript/pkg/credentials"
19
- runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
20
19
"github.com/gptscript-ai/gptscript/pkg/hash"
21
20
"github.com/gptscript-ai/gptscript/pkg/repos/git"
22
21
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
@@ -55,10 +54,10 @@ func (n noopRuntime) Setup(_ context.Context, _ types.Tool, _, _ string, _ []str
55
54
}
56
55
57
56
type Manager struct {
57
+ cacheDir string
58
58
storageDir string
59
59
gitDir string
60
60
runtimeDir string
61
- credHelperDirs credentials.CredentialHelperDirs
62
61
runtimes []Runtime
63
62
credHelperConfig * credHelperConfig
64
63
}
@@ -72,11 +71,11 @@ type credHelperConfig struct {
72
71
func New (cacheDir string , runtimes ... Runtime ) * Manager {
73
72
root := filepath .Join (cacheDir , "repos" )
74
73
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 ,
80
79
}
81
80
}
82
81
@@ -110,50 +109,59 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
110
109
distInfo , suffix string
111
110
)
112
111
// The file helper is built-in and does not need to be downloaded.
113
- if helperName == "file" {
112
+ if helperName == config . FileCredHelper {
114
113
return nil
115
114
}
116
115
switch helperName {
117
- case "wincred" :
116
+ case config . WincredCredHelper :
118
117
suffix = ".exe"
119
118
default :
120
119
distInfo = fmt .Sprintf ("-%s-%s" , runtime .GOOS , runtime .GOARCH )
121
120
}
122
121
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 )
125
128
126
129
// Load the last-checked file to make sure we haven't checked the repo in the last 24 hours.
127
130
now := time .Now ()
128
- lastChecked , err := os .ReadFile (m . credHelperDirs .LastCheckedFile )
131
+ lastChecked , err := os .ReadFile (credHelperDirs .LastCheckedFile )
129
132
if err == nil {
130
133
if t , err := time .Parse (time .RFC3339 , strings .TrimSpace (string (lastChecked ))); err == nil && now .Sub (t ) < 24 * time .Hour {
131
134
// 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 {
133
136
log .Debugf ("Credential helper %s up-to-date as of %v, checking for updates after %v" , helperName , t , t .Add (24 * time .Hour ))
134
137
return nil
135
138
}
136
139
}
137
140
}
138
141
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 {
140
143
return err
141
144
}
142
145
143
146
// 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 {
145
153
return err
146
154
}
147
155
148
156
tool := types.Tool {
149
157
ToolDef : types.ToolDef {
150
158
Parameters : types.Parameters {
151
- Name : "gptscript-credential-helpers" ,
159
+ Name : repoName ,
152
160
},
153
161
},
154
162
Source : types.ToolSource {
155
163
Repo : & types.Repo {
156
- Root : runtimeEnv . VarOrDefault ( "GPTSCRIPT_CRED_HELPERS_ROOT" , "https://github.com/gptscript-ai/gptscript-credential-helpers.git" ) ,
164
+ Root : gitURL ,
157
165
},
158
166
},
159
167
}
@@ -164,12 +172,12 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
164
172
165
173
var needsDownloaded bool
166
174
// 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 )
168
176
if (err == nil && strings .TrimSpace (string (lastRevision )) != tool .Source .Repo .Root + tag ) || errors .Is (err , fs .ErrNotExist ) {
169
177
// Need to pull the latest version.
170
178
needsDownloaded = true
171
179
// 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 {
173
181
return err
174
182
}
175
183
} else if err != nil {
@@ -179,15 +187,15 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
179
187
if ! needsDownloaded {
180
188
// Check for the existence of the credential helper binary.
181
189
// 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 {
183
191
return nil
184
192
}
185
193
}
186
194
187
195
// Find the Go runtime and use it to build the credential helper.
188
196
for _ , rt := range m .runtimes {
189
197
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 )
191
199
}
192
200
}
193
201
0 commit comments