fix(config): write profiles and config atomically and report files that cannot be read (#45)

* fix(config): write profiles and config atomically and report files that cannot be read

Profile files and ~/.floci/config.yaml are replaced through a temporary file, so an interrupted write leaves the old file intact; an unknown key in config.yaml no longer resets the default product to aws, and an unreadable config.yaml or profile file is reported on stderr instead of being ignored.

* fix(config): warn when the config file cannot be opened and keep long profile names saving

A config.yaml in a directory the user cannot search is no longer taken for a missing file, the profile list warning quotes the real cause instead of calling every failure bad YAML, and the temporary file name no longer grows with the profile name. Adds tests for failed saves.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-10 15:54:34 -05:00
1 parent fbdc3ef713
commit 522e54ef3c
5 files changed
+320 -10

No files matched your search

@@ -2,6 +2,7 @@ package io.floci.cli.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.floci.cli.output.Ansi;
import java.io.IOException;
import java.nio.file.Files;
@@ -69,7 +70,14 @@ public class ProfileStore {
.forEach(p -> {
try {
profiles.add(read(p));
} catch (IOException ignored) {}
} catch (IOException e) {
// Skipped, but not silently: --profile on the same name would fail.
// The cause is quoted, not guessed: this is a parse error or a
// file that could not be opened.
System.err.println(Ansi.yellow("Warning: ") + "Could not read profile file " + p
+ ": " + firstLine(e.getMessage())
+ "\nCheck the file and its permissions, or delete it.");
}
});
}
return profiles;
@@ -81,6 +89,12 @@ public class ProfileStore {
return Optional.of(read(file));
}
private static String firstLine(String message) {
if (message == null) return "unreadable";
int newline = message.indexOf('\n');
return newline >= 0 ? message.substring(0, newline) : message;
}
// The one read path, so list and show agree on a profile's name.
private static Profile read(Path file) throws IOException {
Profile profile = YAML.readValue(file.toFile(), Profile.class);
@@ -106,8 +120,7 @@ public class ProfileStore {
}
public void save(Profile profile) throws IOException {
Files.createDirectories(profilesDir);
YAML.writeValue(profileFile(profile.name).toFile(), profile);
AtomicFiles.write(profileFile(profile.name), YAML.writeValueAsBytes(profile));
}
/**