Skip to content

Commit

Permalink
Fix queryer did not work in git bash
Browse files Browse the repository at this point in the history
  • Loading branch information
Flanker32 committed Nov 24, 2021
1 parent 5e74f23 commit d60cd92
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.microsoft.azure.maven.utils.CustomTextIoStringListReader;
import com.microsoft.azure.maven.utils.MavenAuthUtils;
import com.microsoft.azure.maven.utils.SystemPropertyUtils;
import com.microsoft.azure.maven.utils.TextIOUtils;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.auth.Account;
import com.microsoft.azure.toolkit.lib.auth.AzureAccount;
Expand Down Expand Up @@ -57,7 +58,7 @@
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import org.beryx.textio.console.ConsoleTextTerminal;
import org.beryx.textio.TextTerminal;
import reactor.core.publisher.Mono;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -281,7 +282,7 @@ protected String selectSubscription(Subscription[] subscriptions) throws AzureEx
.sorted()
.collect(Collectors.toList());
final SubscriptionOption defaultValue = wrapSubs.get(0);
final SubscriptionOption subscriptionOptionSelected = new CustomTextIoStringListReader<SubscriptionOption>(ConsoleTextTerminal::new, null)
final SubscriptionOption subscriptionOptionSelected = new CustomTextIoStringListReader<SubscriptionOption>(TextIOUtils::getTextTerminal, null)
.withCustomPrompt(String.format("Please choose a subscription%s: ",
highlightDefaultValue(defaultValue == null ? null : defaultValue.getSubscriptionName())))
.withNumberedPossibleValues(wrapSubs).withDefaultValue(defaultValue).read("Available subscriptions:");
Expand Down Expand Up @@ -496,6 +497,7 @@ public void execute() throws MojoExecutionException {
// into endless loop when close, we need to call it in main thread.
// Refer here for detail codes: https://github.com/Microsoft/ApplicationInsights-Java/blob/master/core/src
// /main/java/com/microsoft/applicationinsights/internal/channel/common/ApacheSender43.java#L103
Optional.ofNullable(TextIOUtils.getTextTerminal()).ifPresent(TextTerminal::dispose);
try {
// Sleep to wait ai sdk flush telemetries
Thread.sleep(2 * 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@
*/
package com.microsoft.azure.maven.queryer;

import com.microsoft.azure.maven.utils.TextIOUtils;
import com.microsoft.azure.toolkit.lib.common.logging.Log;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.beryx.textio.GenericInputReader;
import org.beryx.textio.StringInputReader;
import org.beryx.textio.console.ConsoleTextTerminal;

import java.util.List;

public class TextIOMavenPluginQueryer extends MavenPluginQueryer {
private static final String FOUND_VALID_VALUE = "Found valid value. Skip user input.";
private static final String PROMPT_STRING_WITHOUT_DEFAULTVALUE = "Define value for %s";

private ConsoleTextTerminal terminal;

public TextIOMavenPluginQueryer() {
this.terminal = new ConsoleTextTerminal();
}

@Override
public String assureInputFromUser(String attribute, String defaultValue, List<String> options, String prompt) {
final String initValue = getInitValue(attribute);
Expand All @@ -31,21 +24,21 @@ public String assureInputFromUser(String attribute, String defaultValue, List<St
return initValue;
}
prompt = StringUtils.isEmpty(prompt) ? getPromptString(attribute) : prompt;
return new GenericInputReader<String>(() -> terminal, null)
return new GenericInputReader<String>(TextIOUtils::getTextTerminal, null)
.withNumberedPossibleValues(options).withDefaultValue(defaultValue).withEqualsFunc(StringUtils::equalsIgnoreCase).read(prompt);
}

@Override
public String assureInputFromUser(String attribute, String defaultValue, String regex,
String prompt, String errorMessage) throws MojoFailureException {
String prompt, String errorMessage) {
final String initValue = getInitValue(attribute);
if (initValue != null && validateInputByRegex(initValue, regex)) {
Log.info(FOUND_VALID_VALUE);
return initValue;
}

prompt = StringUtils.isEmpty(prompt) ? getPromptString(attribute) : prompt;
return new StringInputReader(() -> terminal).withPattern(regex).withDefaultValue(defaultValue).withMinLength(0).read(prompt);
return new StringInputReader(TextIOUtils::getTextTerminal).withPattern(regex).withDefaultValue(defaultValue).withMinLength(0).read(prompt);
}

private String getPromptString(String attributeName) {
Expand All @@ -54,6 +47,5 @@ private String getPromptString(String attributeName) {

@Override
public void close() {
terminal.dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package com.microsoft.azure.maven.utils;

import org.beryx.textio.TextTerminal;
import org.beryx.textio.console.ConsoleTextTerminalProvider;
import org.beryx.textio.jline.JLineTextTerminalProvider;
import org.beryx.textio.swing.SwingTextTerminalProvider;
import org.beryx.textio.system.SystemTextTerminalProvider;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

public class TextIOUtils {
private static TextTerminal textTerminal;
private static List<Supplier<TextTerminal>> terminalSupplierList = Arrays.asList(
() -> (new JLineTextTerminalProvider()).getTextTerminal(),
() -> (new ConsoleTextTerminalProvider()).getTextTerminal(),
() -> (new SwingTextTerminalProvider()).getTextTerminal(),
() -> (new SystemTextTerminalProvider()).getTextTerminal()
);

public static synchronized TextTerminal getTextTerminal() {
if (textTerminal == null) {
for (Supplier<TextTerminal> supplier : terminalSupplierList) {
try {
textTerminal = supplier.get();
} catch (Throwable throwable) {
// swallow exception when initialize terminal
}
if (textTerminal != null) {
break;
}
}
}
return textTerminal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.microsoft.azure.maven.queryer.QueryFactory;
import com.microsoft.azure.maven.utils.CustomTextIoStringListReader;
import com.microsoft.azure.maven.utils.MavenConfigUtils;
import com.microsoft.azure.maven.utils.TextIOUtils;
import com.microsoft.azure.maven.webapp.configuration.Deployment;
import com.microsoft.azure.maven.webapp.configuration.SchemaVersion;
import com.microsoft.azure.maven.webapp.handlers.WebAppPomHandler;
Expand Down Expand Up @@ -38,7 +39,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.beryx.textio.console.ConsoleTextTerminal;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.dom4j.DocumentException;

Expand Down Expand Up @@ -518,7 +518,7 @@ private static WebAppOption selectAzureWebApp(List<WebAppOption> javaOrDockerWeb
return null;
}
options.addAll(javaOrDockerWebapps);
return new CustomTextIoStringListReader<WebAppOption>(ConsoleTextTerminal::new, null)
return new CustomTextIoStringListReader<WebAppOption>(TextIOUtils::getTextTerminal, null)
.withCustomPrompt(String.format("Please choose a %s Web App%s: ", webAppType, highlightDefaultValue(WebAppOption.CREATE_NEW.toString())))
.withNumberedPossibleValues(options).withDefaultValue(WebAppOption.CREATE_NEW)
.read(String.format("%s Web Apps in subscription %s:", webAppType, TextUtils.blue(targetSubscription.getName())));
Expand Down

0 comments on commit d60cd92

Please sign in to comment.