Fix file write

Signed-off-by: Scott Rigby <scott@r6by.com>
pull/30924/head
Scott Rigby 4 months ago
parent 6099f13490
commit f8526b1556
No known key found for this signature in database
GPG Key ID: C7C6FBB5B91C1155

@ -105,13 +105,19 @@ func handleEmptyConfgFile(credentialsFile string, client *Client) {
// handle only if credentials file exists
// ORAS already handles if file does not exist
if err == nil {
var configData map[string]json.RawMessage
fileInfo, err := os.Stat(credentialsFile)
if err != nil {
client.err = err
}
// handle only if credentials file is empty
if err := json.NewDecoder(f).Decode(&configData); err != nil && errors.Is(err, io.EOF) {
if fileInfo.Size() == 0 {
emptyMap := make(map[string]interface{})
jsonData, err := json.Marshal(emptyMap)
if err != nil {
client.err = err
}
// Attempt to write empty JSON map to config file
client.credentialsFile = ""
encoder := json.NewEncoder(f)
err = encoder.Encode(configData)
err = os.WriteFile(credentialsFile, jsonData, 0644)
// Note that <3.18.0 Helm would fail if the config file was not
// writable, so we can continue to error if that is the case
if err != nil {

Loading…
Cancel
Save