@@ -3,11 +3,9 @@ package config
3
3
import (
4
4
"encoding/base64"
5
5
"encoding/json"
6
- "errors"
7
6
"fmt"
8
7
"os"
9
8
"runtime"
10
- "slices"
11
9
"strings"
12
10
"sync"
13
11
@@ -23,25 +21,19 @@ const (
23
21
FileCredHelper = "file"
24
22
SqliteCredHelper = "sqlite"
25
23
PostgresCredHelper = "postgres"
26
-
27
- GPTScriptHelperPrefix = "gptscript-credential-"
28
24
)
29
25
30
26
var (
31
27
darwinHelpers = []string {OsxkeychainCredHelper , FileCredHelper , SqliteCredHelper , PostgresCredHelper }
32
28
windowsHelpers = []string {WincredCredHelper , FileCredHelper }
33
29
linuxHelpers = []string {SecretserviceCredHelper , PassCredHelper , FileCredHelper , SqliteCredHelper , PostgresCredHelper }
34
- )
35
30
36
- func listAsString (helpers []string ) string {
37
- if len (helpers ) == 0 {
38
- return ""
39
- } else if len (helpers ) == 1 {
40
- return helpers [0 ]
41
- }
31
+ // Helpers is a list of all supported credential helpers from github.com/gptscript-ai/gptscript-credential-helpers
32
+ Helpers = []string {WincredCredHelper , OsxkeychainCredHelper , SecretserviceCredHelper , PassCredHelper }
42
33
43
- return strings .Join (helpers [:len (helpers )- 1 ], ", " ) + " or " + helpers [len (helpers )- 1 ]
44
- }
34
+ // DBHelpers is a list of all supported credential helpers from github.com/gptscript-ai/gptscript-credential-database
35
+ DBHelpers = []string {SqliteCredHelper , PostgresCredHelper }
36
+ )
45
37
46
38
type AuthConfig types.AuthConfig
47
39
@@ -74,8 +66,8 @@ func (a *AuthConfig) UnmarshalJSON(data []byte) error {
74
66
type CLIConfig struct {
75
67
Auths map [string ]AuthConfig `json:"auths,omitempty"`
76
68
CredentialsStore string `json:"credsStore,omitempty"`
77
- Integrations map [string ]string `json:"integrations,omitempty"`
78
69
70
+ raw []byte
79
71
auths map [string ]types.AuthConfig
80
72
authsLock * sync.Mutex
81
73
location string
@@ -108,7 +100,19 @@ func (c *CLIConfig) Save() error {
108
100
}
109
101
c .auths = nil
110
102
}
111
- data , err := json .Marshal (c )
103
+
104
+ // This is to not overwrite additional fields that might be the config file
105
+ out := map [string ]any {}
106
+ if len (c .raw ) > 0 {
107
+ err := json .Unmarshal (c .raw , & out )
108
+ if err != nil {
109
+ return err
110
+ }
111
+ }
112
+ out ["auths" ] = c .Auths
113
+ out ["credsStore" ] = c .CredentialsStore
114
+
115
+ data , err := json .Marshal (out )
112
116
if err != nil {
113
117
return err
114
118
}
@@ -154,34 +158,22 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
154
158
result := & CLIConfig {
155
159
authsLock : & sync.Mutex {},
156
160
location : gptscriptConfigFile ,
161
+ raw : data ,
157
162
}
158
163
if err := json .Unmarshal (data , result ); err != nil {
159
164
return nil , fmt .Errorf ("failed to unmarshal %s: %v" , gptscriptConfigFile , err )
160
165
}
161
166
167
+ if store := os .Getenv ("GPTSCRIPT_CREDENTIALS_STORE" ); store != "" {
168
+ result .CredentialsStore = store
169
+ }
170
+
162
171
if result .CredentialsStore == "" {
163
172
if err := result .setDefaultCredentialsStore (); err != nil {
164
173
return nil , err
165
174
}
166
175
}
167
176
168
- if ! isValidCredentialHelper (result .CredentialsStore ) {
169
- errMsg := fmt .Sprintf ("invalid credential store '%s'" , result .CredentialsStore )
170
- switch runtime .GOOS {
171
- case "darwin" :
172
- errMsg += fmt .Sprintf (" (use %s)" , listAsString (darwinHelpers ))
173
- case "windows" :
174
- errMsg += fmt .Sprintf (" (use %s)" , listAsString (windowsHelpers ))
175
- case "linux" :
176
- errMsg += fmt .Sprintf (" (use %s)" , listAsString (linuxHelpers ))
177
- default :
178
- errMsg += " (use file)"
179
- }
180
- errMsg += fmt .Sprintf ("\n Please edit your config file at %s to fix this." , result .location )
181
-
182
- return nil , errors .New (errMsg )
183
- }
184
-
185
177
return result , nil
186
178
}
187
179
@@ -197,19 +189,6 @@ func (c *CLIConfig) setDefaultCredentialsStore() error {
197
189
return c .Save ()
198
190
}
199
191
200
- func isValidCredentialHelper (helper string ) bool {
201
- switch runtime .GOOS {
202
- case "darwin" :
203
- return slices .Contains (darwinHelpers , helper )
204
- case "windows" :
205
- return slices .Contains (windowsHelpers , helper )
206
- case "linux" :
207
- return slices .Contains (linuxHelpers , helper )
208
- default :
209
- return helper == FileCredHelper
210
- }
211
- }
212
-
213
192
func readFile (path string ) ([]byte , error ) {
214
193
data , err := os .ReadFile (path )
215
194
if os .IsNotExist (err ) {
0 commit comments