diff --git a/node/api/src/main/java/eu/cloudnetservice/node/command/annotation/EnableConfirmSkipFlag.java b/node/api/src/main/java/eu/cloudnetservice/node/command/annotation/EnableConfirmSkipFlag.java new file mode 100644 index 0000000000..a898de2f6e --- /dev/null +++ b/node/api/src/main/java/eu/cloudnetservice/node/command/annotation/EnableConfirmSkipFlag.java @@ -0,0 +1,50 @@ +/* + * Copyright 2019-2024 CloudNetService team & contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package eu.cloudnetservice.node.command.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import lombok.NonNull; + +/** + * This annotation allows users to skip the required confirmation + * {@link org.incendo.cloud.processors.confirmation.annotation.Confirmation} of a command. When this annotation is + * applied to a method a flag is implicitly appended to the command which can be used to skip the confirmation. + *

+ * The annotations value is used as the flag name, which defaults to {@code confirm}. + * + * @see org.incendo.cloud.processors.confirmation.annotation.Confirmation + * @since 4.0 + */ +@Documented +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface EnableConfirmSkipFlag { + + /** + * The value of this annotation is used as the flag name to skip the confirmation. Should not include the leading + * dashes. + *

+ * Defaults to {@code confirm}, allowing users to skip the confirmation by appending {@code --confirm}. + * + * @return the flag name that allows skipping. + */ + @NonNull String value() default "confirm"; +} diff --git a/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/defaults/DefaultCommandProvider.java b/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/defaults/DefaultCommandProvider.java index 629f37135f..dcd1da11e7 100644 --- a/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/defaults/DefaultCommandProvider.java +++ b/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/defaults/DefaultCommandProvider.java @@ -29,6 +29,7 @@ import eu.cloudnetservice.node.command.annotation.CommandAlias; import eu.cloudnetservice.node.command.annotation.Description; import eu.cloudnetservice.node.command.annotation.Documentation; +import eu.cloudnetservice.node.command.annotation.EnableConfirmSkipFlag; import eu.cloudnetservice.node.command.source.CommandSource; import eu.cloudnetservice.node.impl.command.exception.CommandExceptionHandler; import eu.cloudnetservice.node.impl.command.sub.ClearCommand; @@ -70,6 +71,7 @@ import org.incendo.cloud.annotations.AnnotationParser; import org.incendo.cloud.key.CloudKey; import org.incendo.cloud.meta.CommandMeta; +import org.incendo.cloud.parser.flag.CommandFlag; import org.incendo.cloud.processors.cache.CaffeineCache; import org.incendo.cloud.processors.confirmation.ConfirmationConfiguration; import org.incendo.cloud.processors.confirmation.ConfirmationManager; @@ -88,6 +90,7 @@ public final class DefaultCommandProvider implements CommandProvider { }); private static final CloudKey DESCRIPTION_KEY = CloudKey.of("cloudnet:description", String.class); private static final CloudKey DOCUMENTATION_KEY = CloudKey.of("cloudnet:documentation", String.class); + private static final CloudKey SKIP_CONFIRMATION_KEY = CloudKey.of("cloudnet:skip-confirmation", String.class); private final I18n i18n; private final CommandExceptionHandler exceptionHandler; @@ -140,6 +143,13 @@ private DefaultCommandProvider( return builder; }); + this.annotationParser.registerBuilderModifier( + EnableConfirmSkipFlag.class, + (enableConfirmSkipFlag, builder) -> { + var flag = CommandFlag.builder(enableConfirmSkipFlag.value()).build(); + return builder.meta(SKIP_CONFIRMATION_KEY, enableConfirmSkipFlag.value()).flag(flag); + }); + // register pre- and post-processor to call our events this.commandManager.suggestionProcessor(suggestionProcessor); this.commandManager.registerCommandPreProcessor(commandPreProcessor); @@ -322,6 +332,11 @@ private void registerCommandConfirmation() { .noPendingCommandNotifier(sender -> sender.sendMessage(this.i18n.translate("command-confirmation-no-requests"))) .confirmationRequiredNotifier( (sender, _) -> sender.sendMessage(this.i18n.translate("command-confirmation-required"))) + .bypassConfirmation(ctx -> { + var meta = ctx.command().commandMeta(); + var skipConfirmationFlag = meta.getOrDefault(SKIP_CONFIRMATION_KEY, null); + return skipConfirmationFlag != null && ctx.flags().hasFlag(skipConfirmationFlag); + }) .build(); var confirmationManager = ConfirmationManager.confirmationManager(configuration); this.commandManager.registerCommandPostProcessor(confirmationManager.createPostprocessor()); diff --git a/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/sub/ExitCommand.java b/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/sub/ExitCommand.java index d886fcfcde..ab5ab609fd 100644 --- a/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/sub/ExitCommand.java +++ b/node/impl/src/main/java/eu/cloudnetservice/node/impl/command/sub/ExitCommand.java @@ -18,6 +18,7 @@ import eu.cloudnetservice.node.command.annotation.CommandAlias; import eu.cloudnetservice.node.command.annotation.Description; +import eu.cloudnetservice.node.command.annotation.EnableConfirmSkipFlag; import eu.cloudnetservice.node.impl.command.source.ConsoleCommandSource; import eu.cloudnetservice.node.impl.tick.DefaultShutdownHandler; import jakarta.inject.Inject; @@ -42,6 +43,7 @@ public ExitCommand(@NonNull Provider shutdownHandlerProv } @Confirmation + @EnableConfirmSkipFlag @Command(value = "exit|shutdown|stop", requiredSender = ConsoleCommandSource.class) public void exit() { // call our shutdown handler, this prevents the Cleaner shutdown hook which is added