Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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.
* <p>
* 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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -88,6 +90,7 @@ public final class DefaultCommandProvider implements CommandProvider {
});
private static final CloudKey<String> DESCRIPTION_KEY = CloudKey.of("cloudnet:description", String.class);
private static final CloudKey<String> DOCUMENTATION_KEY = CloudKey.of("cloudnet:documentation", String.class);
private static final CloudKey<String> SKIP_CONFIRMATION_KEY = CloudKey.of("cloudnet:skip-confirmation", String.class);

private final I18n i18n;
private final CommandExceptionHandler exceptionHandler;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +43,7 @@ public ExitCommand(@NonNull Provider<DefaultShutdownHandler> 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
Expand Down
Loading