Skip to content

Commit

Permalink
[apache#5932] improve(CLI): Fix the CLI delete the anonymous user
Browse files Browse the repository at this point in the history
Fix the CLI  delete the anonymous user
  • Loading branch information
Abyss-lord committed Feb 6, 2025
1 parent 4534985 commit bbfd2c9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
18 changes: 16 additions & 2 deletions clients/cli/src/main/java/org/apache/gravitino/cli/AreYouSure.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,34 @@ public class AreYouSure {
* @return {@code true} if the action is to continue {@code false} otherwise.
*/
public static boolean really(boolean force) {
return really(
force,
"This command could result in data loss or other issues. Are you sure you want to do this? "
+ "(Y/N)");
}

/**
* Prompts the user with a confirmation message to confirm an action.
*
* @param force if {@code true}, skips user confirmation and proceeds.
* @param message the confirmation message to display to the user.
* @return {@code true} if the action is to continue {@code false} otherwise.
*/
public static boolean really(boolean force, String message) {
/* force option for scripting */
if (force) {
return true;
}

try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name())) {
System.out.println(
"This command could result in data loss or other issues. Are you sure you want to do this? (Y/N)");
System.out.println(message);
String answer = scanner.next();
return "Y".equals(answer);
} catch (Exception e) {
System.err.println("Error while reading user input: " + e.getMessage());
Main.exit(-1);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ public DeleteUser(
public void handle() {
boolean deleted = false;

if (!AreYouSure.really(force)) {
boolean shouldProceed =
"anonymous".equalsIgnoreCase(user)
? AreYouSure.really(
false,
"Are you sure you want to delete the anonymous user? This will cause unexpected behavior.")
: AreYouSure.really(force);

if (!shouldProceed) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,38 @@
package org.apache.gravitino.cli;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestAreYouSure {

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;

@BeforeEach
void setUp() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}

@AfterEach
void restoreExitFlg() {
Main.useExit = true;
}

@AfterEach
public void restoreStreams() {
System.setOut(originalOut);
System.setErr(originalErr);
}

@Test
void testCommandWithForce() {
Assertions.assertTrue(AreYouSure.really(true));
Expand Down Expand Up @@ -57,4 +83,15 @@ void testCommandWithInputInvalid() {

Assertions.assertFalse(AreYouSure.really(false));
}

@Test
void testCommandWithInputMessage() {
ByteArrayInputStream inputStream =
new ByteArrayInputStream("Y".getBytes(StandardCharsets.UTF_8));
System.setIn(inputStream);

Assertions.assertTrue(AreYouSure.really(false, "output information"));
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals("output information", output);
}
}

0 comments on commit bbfd2c9

Please sign in to comment.