mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(profile): apply --profile to every command, not just config show (#23)
`--profile <name>` was declared on the shared GlobalOptions mixin but only
ConfigShowCommand ever read it. Every other command, `start` included, fell
through to the built-in defaults, so a profile's container, image, port,
persistDir and services were silently dropped.
The worst consequence was silent data loss: the `-v <dir>:/app/data` mount and
`FLOCI_STORAGE_MODE=persistent` were gated on the `--persist` flag alone, so a
profile with `persistDir` set produced neither. The image declares
`VOLUME /app/data`, so every start attached a fresh anonymous volume and the
server stayed in-memory.
A profile is now applied by a single picocli IDefaultValueProvider registered in
FlociCli.buildCommandLine(). Picocli consults it only for options the user did
not pass, and only after that command's arguments are processed, which gives the
documented precedence without per-command plumbing:
command-line flag > --profile <name> > FLOCI_* env var > product default
Behaviour changes:
- An unknown, unreadable or invalid profile is now an error (exit 2) rather than
a silent fall back to the defaults. `--help` still works with a bad profile.
- `restart --profile <name>` re-applies the profile's persistDir, image, port
and services. It builds its StartCommand programmatically, so it carries the
profile across by hand.
- `config show` merges instead of replacing, so an explicit --endpoint /
--container / -o is no longer discarded when --profile resolves. It now also
reports the profile's start-only fields.
- `config profile create` seeds the file from the tree it was run under;
previously every tree wrote AWS values.
- Profile names are validated, closing a path traversal
(`config profile delete ../../foo` resolved the raw name).
- Reads accept the `.yml` spelling that `config profile list` has always
listed, and `list` backfills a missing `name:` from the file name.
- The readiness poll after `start` keeps the endpoint's host when swapping in
the bound port, instead of always rewriting it to localhost.
Adds 64 tests, including the first coverage of ProfileStore.
Closes #22
This commit is contained in:
1 parent
588edc235a
commit
52c08ecec5
19 files changed
+1193
-62
No files matched your search
@@ -9,11 +9,16 @@ import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ProfileStore {
|
||||
|
||||
private static final ObjectMapper YAML = new ObjectMapper(new YAMLFactory());
|
||||
|
||||
// A profile name becomes a file name, so it must not be able to traverse out of the
|
||||
// profiles directory: 'floci config profile delete ../../foo' resolved the raw name.
|
||||
private static final Pattern VALID_NAME = Pattern.compile("[A-Za-z0-9._-]+");
|
||||
|
||||
private final Path profilesDir;
|
||||
|
||||
public ProfileStore() {
|
||||
@@ -24,6 +29,24 @@ public class ProfileStore {
|
||||
this.profilesDir = profilesDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects anything that could escape {@link #profilesDir} or name a hidden path segment.
|
||||
* @throws IllegalArgumentException with a message suitable for printing to the user
|
||||
*/
|
||||
public static String validateName(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Profile name must not be empty.\n"
|
||||
+ "Run 'floci config profile list' to see available profiles.");
|
||||
}
|
||||
if (".".equals(name) || "..".equals(name) || !VALID_NAME.matcher(name).matches()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid profile name '" + name + "'. Use only letters, digits, '.', '_' and '-'.\n"
|
||||
+ "Run 'floci config profile list' to see available profiles.");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Profile> list() throws IOException {
|
||||
if (!Files.exists(profilesDir)) return List.of();
|
||||
List<Profile> profiles = new ArrayList<>();
|
||||
@@ -32,6 +55,11 @@ public class ProfileStore {
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Profile profile = YAML.readValue(p.toFile(), Profile.class);
|
||||
// Hand-written files often omit 'name:'; the file name is the name.
|
||||
if (profile.name == null || profile.name.isBlank()) {
|
||||
String file = p.getFileName().toString();
|
||||
profile.name = file.substring(0, file.lastIndexOf('.'));
|
||||
}
|
||||
profiles.add(profile);
|
||||
} catch (IOException ignored) {}
|
||||
});
|
||||
@@ -40,7 +68,7 @@ public class ProfileStore {
|
||||
}
|
||||
|
||||
public Optional<Profile> get(String name) throws IOException {
|
||||
Path file = profileFile(name);
|
||||
Path file = existingProfileFile(name);
|
||||
if (!Files.exists(file)) return Optional.empty();
|
||||
return Optional.of(YAML.readValue(file.toFile(), Profile.class));
|
||||
}
|
||||
@@ -51,11 +79,26 @@ public class ProfileStore {
|
||||
}
|
||||
|
||||
public boolean delete(String name) throws IOException {
|
||||
Path file = profileFile(name);
|
||||
Path file = existingProfileFile(name);
|
||||
return Files.deleteIfExists(file);
|
||||
}
|
||||
|
||||
/** Where {@link #save} writes {@code name}. Always {@code .yaml}. */
|
||||
public Path profileFile(String name) {
|
||||
return profilesDir.resolve(name + ".yaml");
|
||||
return profilesDir.resolve(validateName(name) + ".yaml");
|
||||
}
|
||||
|
||||
// list() has always accepted .yml, so reads must too — otherwise a .yml profile shows up in
|
||||
// 'config profile list' and then reports "not found" when passed to --profile.
|
||||
private Path existingProfileFile(String name) {
|
||||
Path yaml = profileFile(name);
|
||||
if (Files.exists(yaml)) return yaml;
|
||||
Path yml = profilesDir.resolve(validateName(name) + ".yml");
|
||||
return Files.exists(yml) ? yml : yaml;
|
||||
}
|
||||
|
||||
/** The directory profiles are read from, for user-facing messages. */
|
||||
public Path profilesDir() {
|
||||
return profilesDir;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user