@@ -3,11 +3,9 @@ package config
33import (
44 "encoding/base64"
55 "encoding/json"
6- "errors"
76 "fmt"
87 "os"
98 "runtime"
10- "slices"
119 "strings"
1210 "sync"
1311
@@ -23,25 +21,19 @@ const (
2321 FileCredHelper = "file"
2422 SqliteCredHelper = "sqlite"
2523 PostgresCredHelper = "postgres"
26-
27- GPTScriptHelperPrefix = "gptscript-credential-"
2824)
2925
3026var (
3127 darwinHelpers = []string {OsxkeychainCredHelper , FileCredHelper , SqliteCredHelper , PostgresCredHelper }
3228 windowsHelpers = []string {WincredCredHelper , FileCredHelper }
3329 linuxHelpers = []string {SecretserviceCredHelper , PassCredHelper , FileCredHelper , SqliteCredHelper , PostgresCredHelper }
34- )
3530
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 }
4233
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+ )
4537
4638type AuthConfig types.AuthConfig
4739
@@ -74,8 +66,8 @@ func (a *AuthConfig) UnmarshalJSON(data []byte) error {
7466type CLIConfig struct {
7567 Auths map [string ]AuthConfig `json:"auths,omitempty"`
7668 CredentialsStore string `json:"credsStore,omitempty"`
77- Integrations map [string ]string `json:"integrations,omitempty"`
7869
70+ raw []byte
7971 auths map [string ]types.AuthConfig
8072 authsLock * sync.Mutex
8173 location string
@@ -108,7 +100,19 @@ func (c *CLIConfig) Save() error {
108100 }
109101 c .auths = nil
110102 }
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 )
112116 if err != nil {
113117 return err
114118 }
@@ -154,34 +158,22 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
154158 result := & CLIConfig {
155159 authsLock : & sync.Mutex {},
156160 location : gptscriptConfigFile ,
161+ raw : data ,
157162 }
158163 if err := json .Unmarshal (data , result ); err != nil {
159164 return nil , fmt .Errorf ("failed to unmarshal %s: %v" , gptscriptConfigFile , err )
160165 }
161166
167+ if store := os .Getenv ("GPTSCRIPT_CREDENTIALS_STORE" ); store != "" {
168+ result .CredentialsStore = store
169+ }
170+
162171 if result .CredentialsStore == "" {
163172 if err := result .setDefaultCredentialsStore (); err != nil {
164173 return nil , err
165174 }
166175 }
167176
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-
185177 return result , nil
186178}
187179
@@ -197,19 +189,6 @@ func (c *CLIConfig) setDefaultCredentialsStore() error {
197189 return c .Save ()
198190}
199191
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-
213192func readFile (path string ) ([]byte , error ) {
214193 data , err := os .ReadFile (path )
215194 if os .IsNotExist (err ) {
0 commit comments