@@ -18,7 +18,6 @@ import (
18
18
"runtime"
19
19
"strings"
20
20
21
- "github.com/gptscript-ai/gptscript/pkg/credentials"
22
21
"github.com/gptscript-ai/gptscript/pkg/debugcmd"
23
22
runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
24
23
"github.com/gptscript-ai/gptscript/pkg/hash"
@@ -97,6 +96,14 @@ type tag struct {
97
96
} `json:"commit"`
98
97
}
99
98
99
+ func GetLatestTag (tool types.Tool ) (string , error ) {
100
+ r , ok := getLatestRelease (tool )
101
+ if ! ok {
102
+ return "" , fmt .Errorf ("failed to get latest release for %s" , tool .Name )
103
+ }
104
+ return r .label , nil
105
+ }
106
+
100
107
func getLatestRelease (tool types.Tool ) (* release , bool ) {
101
108
if tool .Source .Repo == nil || ! strings .HasPrefix (tool .Source .Repo .Root , "https://github.com/" ) {
102
109
return nil , false
@@ -116,11 +123,14 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
116
123
account , repo := parts [1 ], parts [2 ]
117
124
118
125
resp , err := client .Get (fmt .Sprintf ("https://api.github.com/repos/%s/%s/tags" , account , repo ))
119
- if err != nil || resp . StatusCode != http . StatusOK {
126
+ if err != nil {
120
127
// ignore error
121
128
return nil , false
122
129
}
123
130
defer resp .Body .Close ()
131
+ if resp .StatusCode != http .StatusOK {
132
+ return nil , false
133
+ }
124
134
125
135
var tags []tag
126
136
if err := json .NewDecoder (resp .Body ).Decode (& tags ); err != nil {
@@ -137,11 +147,14 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
137
147
}
138
148
139
149
resp , err = client .Get (fmt .Sprintf ("https://github.com/%s/%s/releases/latest" , account , repo ))
140
- if err != nil || resp . StatusCode != http . StatusFound {
150
+ if err != nil {
141
151
// ignore error
142
152
return nil , false
143
153
}
144
154
defer resp .Body .Close ()
155
+ if resp .StatusCode != http .StatusFound {
156
+ return nil , false
157
+ }
145
158
146
159
target := resp .Header .Get ("Location" )
147
160
if target == "" {
@@ -212,7 +225,7 @@ func downloadBin(ctx context.Context, checksum, src, url, bin string) error {
212
225
return nil
213
226
}
214
227
215
- func getChecksum (ctx context.Context , rel * release ) string {
228
+ func getChecksum (ctx context.Context , rel * release , artifactName string ) string {
216
229
resp , err := get (ctx , rel .checksumTxt ())
217
230
if err != nil {
218
231
// ignore error
@@ -223,7 +236,7 @@ func getChecksum(ctx context.Context, rel *release) string {
223
236
scan := bufio .NewScanner (resp .Body )
224
237
for scan .Scan () {
225
238
fields := strings .Fields (scan .Text ())
226
- if len (fields ) == 2 && (fields [1 ] == rel . srcBinName () || fields [1 ] == "*" + rel . srcBinName () ) {
239
+ if len (fields ) == 2 && (fields [1 ] == artifactName || fields [1 ] == "*" + artifactName ) {
227
240
return fields [0 ]
228
241
}
229
242
}
@@ -241,7 +254,7 @@ func (r *Runtime) Binary(ctx context.Context, tool types.Tool, _, toolSource str
241
254
return false , nil , nil
242
255
}
243
256
244
- checksum := getChecksum (ctx , rel )
257
+ checksum := getChecksum (ctx , rel , rel . srcBinName () )
245
258
if checksum == "" {
246
259
return false , nil , nil
247
260
}
@@ -268,30 +281,28 @@ func (r *Runtime) Setup(ctx context.Context, _ types.Tool, dataRoot, toolSource
268
281
return newEnv , nil
269
282
}
270
283
271
- func (r * Runtime ) BuildCredentialHelper (ctx context.Context , helperName string , credHelperDirs credentials. CredentialHelperDirs , dataRoot , revision string , env [] string ) error {
284
+ func (r * Runtime ) DownloadCredentialHelper (ctx context.Context , tool types. Tool , helperName , distInfo , suffix string , binDir string ) error {
272
285
if helperName == "file" {
273
286
return nil
274
287
}
275
288
276
- var suffix string
277
- if helperName == "wincred" {
278
- suffix = ".exe"
289
+ rel , ok := getLatestRelease (tool )
290
+ if ! ok {
291
+ return fmt .Errorf ("failed to find %s release" , r .ID ())
292
+ }
293
+ binaryName := "gptscript-credential-" + helperName
294
+ checksum := getChecksum (ctx , rel , binaryName + distInfo + suffix )
295
+ if checksum == "" {
296
+ return fmt .Errorf ("failed to find %s release checksum for os=%s arch=%s" , r .ID (), runtime .GOOS , runtime .GOARCH )
279
297
}
280
298
281
- binPath , err := r .getRuntime (ctx , dataRoot )
282
- if err != nil {
283
- return err
299
+ url , _ := strings .CutSuffix (rel .binURL (), rel .srcBinName ())
300
+ url += binaryName + distInfo + suffix
301
+ if err := downloadBin (ctx , checksum , strings .TrimSuffix (binDir , "bin" ), url , binaryName + suffix ); err != nil {
302
+ return fmt .Errorf ("failed to download %s release for os=%s arch=%s: %w" , r .ID (), runtime .GOOS , runtime .GOARCH , err )
284
303
}
285
- newEnv := runtimeEnv .AppendPath (env , binPath )
286
304
287
- log .InfofCtx (ctx , "Building credential helper %s" , helperName )
288
- cmd := debugcmd .New (ctx , filepath .Join (binPath , "go" ),
289
- "build" , "-buildvcs=false" , "-o" ,
290
- filepath .Join (credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix ),
291
- fmt .Sprintf ("./%s/cmd/" , helperName ))
292
- cmd .Env = stripGo (append (env , newEnv ... ))
293
- cmd .Dir = filepath .Join (credHelperDirs .RepoDir , revision )
294
- return cmd .Run ()
305
+ return nil
295
306
}
296
307
297
308
func (r * Runtime ) getReleaseAndDigest () (string , string , error ) {
0 commit comments