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
@@ -0,0 +1,111 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.config.ProfileNotFoundException;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* A profile that does not resolve must stop the command rather than silently fall back to the
|
||||
* built-in defaults — silently falling back is what made issue #22 lose data unnoticed.
|
||||
*/
|
||||
class MissingProfileTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private CommandLine cli() {
|
||||
return FlociCli.buildCommandLine(new ProfileStore(tempDir));
|
||||
}
|
||||
|
||||
private void writeProfile(String name, String yaml) throws IOException {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve(name + ".yaml"), yaml);
|
||||
}
|
||||
|
||||
private static String captureStderr(Supplier<Integer> action, int[] exitCode) {
|
||||
PrintStream original = System.err;
|
||||
ByteArrayOutputStream captured = new ByteArrayOutputStream();
|
||||
System.setErr(new PrintStream(captured));
|
||||
try {
|
||||
exitCode[0] = action.get();
|
||||
} finally {
|
||||
System.setErr(original);
|
||||
}
|
||||
return captured.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownProfileExitsTwoWithAMessageThatNamesTheNextStep() {
|
||||
int[] exit = new int[1];
|
||||
String stderr = captureStderr(() -> cli().execute("start", "--profile", "typo"), exit);
|
||||
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(stderr.contains("Profile 'typo' not found"), stderr);
|
||||
assertTrue(stderr.contains(tempDir.toString()), stderr);
|
||||
assertTrue(stderr.contains("floci config profile list"), stderr);
|
||||
// The message stands alone; picocli's usage dump would bury it.
|
||||
assertFalse(stderr.contains("Usage:"), stderr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsEvenWhenEveryProfileBackedOptionWasPassedExplicitly() {
|
||||
int[] exit = new int[1];
|
||||
captureStderr(() -> cli().execute("start", "--profile", "typo",
|
||||
"--endpoint", "http://x:1", "--container", "c", "--image", "i",
|
||||
"--port", "1", "--persist", "/p", "--services", "s", "-o", "json"), exit);
|
||||
|
||||
assertEquals(2, exit[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void helpStillWorksWithABadProfile() {
|
||||
int[] exit = new int[1];
|
||||
captureStderr(() -> cli().execute("start", "--profile", "typo", "--help"), exit);
|
||||
|
||||
assertEquals(0, exit[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void traversalInTheProfileNameIsRejected() {
|
||||
int[] exit = new int[1];
|
||||
String stderr = captureStderr(() -> cli().execute("start", "--profile", "../../escape"), exit);
|
||||
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(stderr.contains("Invalid profile name"), stderr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unusableProfileValuesNameTheFileRatherThanAnOptionTheUserNeverTyped() throws Exception {
|
||||
writeProfile("bad-output", "output: bogus\n");
|
||||
writeProfile("bad-port", "port: 99999\n");
|
||||
|
||||
int[] exit = new int[1];
|
||||
String outputErr = captureStderr(() -> cli().execute("start", "--profile", "bad-output"), exit);
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(outputErr.contains("bad-output.yaml"), outputErr);
|
||||
assertTrue(outputErr.contains("output must be text, json or yaml"), outputErr);
|
||||
|
||||
String portErr = captureStderr(() -> cli().execute("start", "--profile", "bad-port"), exit);
|
||||
assertEquals(2, exit[0]);
|
||||
assertTrue(portErr.contains("bad-port.yaml"), portErr);
|
||||
assertTrue(portErr.contains("port must be between 1 and 65535"), portErr);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsingRaisesTheDedicatedException() {
|
||||
assertThrows(ProfileNotFoundException.class,
|
||||
() -> cli().parseArgs("start", "--profile", "typo"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user