@@ -8,22 +8,21 @@ import (
8
8
"io/fs"
9
9
"os"
10
10
"path/filepath"
11
+ "runtime"
11
12
"strings"
12
13
"sync"
13
14
"time"
14
15
15
16
"github.com/BurntSushi/locker"
16
17
"github.com/gptscript-ai/gptscript/pkg/config"
17
18
"github.com/gptscript-ai/gptscript/pkg/credentials"
19
+ runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
18
20
"github.com/gptscript-ai/gptscript/pkg/hash"
19
- "github.com/gptscript-ai/gptscript/pkg/loader/github"
20
21
"github.com/gptscript-ai/gptscript/pkg/repos/git"
21
22
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
22
23
"github.com/gptscript-ai/gptscript/pkg/types"
23
24
)
24
25
25
- const credentialHelpersRepo = "github.com/gptscript-ai/gptscript-credential-helpers"
26
-
27
26
type Runtime interface {
28
27
ID () string
29
28
Supports (tool types.Tool , cmd []string ) bool
@@ -68,7 +67,6 @@ type credHelperConfig struct {
68
67
lock sync.Mutex
69
68
initialized bool
70
69
cliCfg * config.CLIConfig
71
- env []string
72
70
}
73
71
74
72
func New (cacheDir string , runtimes ... Runtime ) * Manager {
@@ -90,7 +88,7 @@ func (m *Manager) EnsureCredentialHelpers(ctx context.Context) error {
90
88
defer m .credHelperConfig .lock .Unlock ()
91
89
92
90
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 {
94
92
return err
95
93
}
96
94
m .credHelperConfig .initialized = true
@@ -99,27 +97,28 @@ func (m *Manager) EnsureCredentialHelpers(ctx context.Context) error {
99
97
return nil
100
98
}
101
99
102
- func (m * Manager ) SetUpCredentialHelpers (_ context.Context , cliCfg * config.CLIConfig , env [] string ) error {
100
+ func (m * Manager ) SetUpCredentialHelpers (_ context.Context , cliCfg * config.CLIConfig ) error {
103
101
m .credHelperConfig = & credHelperConfig {
104
102
cliCfg : cliCfg ,
105
- env : env ,
106
103
}
107
104
return nil
108
105
}
109
106
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 {
111
108
var (
112
- helperName = cliCfg .CredentialsStore
113
- suffix string
109
+ helperName = cliCfg .CredentialsStore
110
+ distInfo , suffix string
114
111
)
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.
120
113
if helperName == "file" {
121
114
return nil
122
115
}
116
+ switch helperName {
117
+ case "wincred" :
118
+ suffix = ".exe"
119
+ default :
120
+ distInfo = fmt .Sprintf ("-%s-%s" , runtime .GOOS , runtime .GOARCH )
121
+ }
123
122
124
123
locker .Lock ("gptscript-credential-helpers" )
125
124
defer locker .Unlock ("gptscript-credential-helpers" )
@@ -137,13 +136,7 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
137
136
}
138
137
}
139
138
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 {
147
140
return err
148
141
}
149
142
@@ -152,37 +145,44 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
152
145
return err
153
146
}
154
147
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
+ }
156
159
160
+ var needsDownloaded bool
157
161
// Check the last revision shasum and see if it is different from the current one.
158
162
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 ) {
160
164
// 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
165
166
// 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 {
167
168
return err
168
169
}
169
170
} else if err != nil {
170
171
return err
171
172
}
172
173
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 {
177
178
return nil
178
179
}
179
180
}
180
181
181
182
// 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 )
186
186
}
187
187
}
188
188
0 commit comments