@@ -11,6 +11,7 @@ import (
11
11
12
12
"github.com/adrg/xdg"
13
13
"github.com/docker/cli/cli/config/types"
14
+ "github.com/gptscript-ai/gptscript/pkg/mvl"
14
15
)
15
16
16
17
const (
@@ -24,6 +25,7 @@ const (
24
25
var (
25
26
// Helpers is a list of all supported credential helpers from github.com/gptscript-ai/gptscript-credential-helpers
26
27
Helpers = []string {WincredCredHelper , OsxkeychainCredHelper , SecretserviceCredHelper , PassCredHelper }
28
+ log = mvl .Package ()
27
29
)
28
30
29
31
type AuthConfig types.AuthConfig
@@ -85,9 +87,9 @@ func (c *CLIConfig) Save() error {
85
87
}
86
88
87
89
if c .auths != nil {
88
- c .Auths = map [string ]AuthConfig {}
90
+ c .Auths = make ( map [string ]AuthConfig , len ( c . auths ))
89
91
for k , v := range c .auths {
90
- c .Auths [k ] = ( AuthConfig ) (v )
92
+ c .Auths [k ] = AuthConfig (v )
91
93
}
92
94
c .auths = nil
93
95
}
@@ -116,13 +118,21 @@ func (c *CLIConfig) GetAuthConfigs() map[string]types.AuthConfig {
116
118
defer c .authsLock .Unlock ()
117
119
}
118
120
121
+ if err := c .readFileIntoConfig (c .location ); err != nil {
122
+ // This is implementing an interface, so we can't return this error.
123
+ log .Warnf ("Failed to read config file: %v" , err )
124
+ }
125
+
119
126
if c .auths == nil {
120
- c .auths = map [string ]types.AuthConfig {}
121
- for k , v := range c .Auths {
122
- authConfig := (types .AuthConfig )(v )
123
- c .auths [k ] = authConfig
124
- }
127
+ c .auths = make (map [string ]types.AuthConfig , len (c .Auths ))
128
+ }
129
+
130
+ // Assume that whatever was pulled from the file is more recent.
131
+ // The docker creds framework will save the file after creating or updating a credential.
132
+ for k , v := range c .Auths {
133
+ c .auths [k ] = types .AuthConfig (v )
125
134
}
135
+
126
136
return c .auths
127
137
}
128
138
@@ -142,17 +152,13 @@ func ReadCLIConfig(gptscriptConfigFile string) (*CLIConfig, error) {
142
152
}
143
153
}
144
154
145
- data , err := readFile (gptscriptConfigFile )
146
- if err != nil {
147
- return nil , err
148
- }
149
155
result := & CLIConfig {
150
156
authsLock : & sync.Mutex {},
151
157
location : gptscriptConfigFile ,
152
- raw : data ,
153
158
}
154
- if err := json .Unmarshal (data , result ); err != nil {
155
- return nil , fmt .Errorf ("failed to unmarshal %s: %v" , gptscriptConfigFile , err )
159
+
160
+ if err := result .readFileIntoConfig (gptscriptConfigFile ); err != nil {
161
+ return nil , err
156
162
}
157
163
158
164
if store := os .Getenv ("GPTSCRIPT_CREDENTIAL_STORE" ); store != "" {
@@ -180,13 +186,18 @@ func (c *CLIConfig) setDefaultCredentialsStore() error {
180
186
return c .Save ()
181
187
}
182
188
183
- func readFile ( path string ) ([] byte , error ) {
189
+ func ( c * CLIConfig ) readFileIntoConfig ( path string ) error {
184
190
data , err := os .ReadFile (path )
185
191
if os .IsNotExist (err ) {
186
- return [] byte ( "{}" ), nil
192
+ return nil
187
193
} else if err != nil {
188
- return nil , fmt .Errorf ("failed to read user config %s: %w" , path , err )
194
+ return fmt .Errorf ("failed to read user config %s: %w" , path , err )
189
195
}
190
196
191
- return data , nil
197
+ c .raw = data
198
+ if err := json .Unmarshal (data , c ); err != nil {
199
+ return fmt .Errorf ("failed to unmarshal %s: %v" , path , err )
200
+ }
201
+
202
+ return nil
192
203
}
0 commit comments