package com.lanyuanxiaoyao.service.command.commands;

import org.springframework.shell.component.ConfirmationInput;
import org.springframework.shell.standard.AbstractShellComponent;

public abstract class AbstractUtilShellComponent extends AbstractShellComponent {
    protected Boolean doubleCheck(String message, Boolean ignore) {
        return ignore || doubleCheck(message);
    }

    protected Boolean doubleCheck(String message) {
        ConfirmationInput confirmationInput = new ConfirmationInput(getTerminal(), message, false);
        confirmationInput.setResourceLoader(getResourceLoader());
        confirmationInput.setTemplateExecutor(getTemplateExecutor());
        ConfirmationInput.ConfirmationInputContext context = confirmationInput.run(ConfirmationInput.ConfirmationInputContext.empty());
        return context.getResultValue();
    }

    protected <T extends Throwable> void doubleCheck(String message, T throwable) throws T {
        if (!doubleCheck(message)) {
            throw throwable;
        }
    }

    protected <T extends RuntimeException> void doubleCheckQuiet(String message, T throwable) {
        doubleCheck(message, new RuntimeException(throwable));
    }
}
