Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix headless prompt reading from pipes #3617

Merged
merged 1 commit into from
Aug 4, 2022
Merged
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
15 changes: 9 additions & 6 deletions Cmdline/Action/Prompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public Prompt(GameInstanceManager mgr)
public int RunCommand(object raw_options)
{
CommonOptions opts = raw_options as CommonOptions;
bool headless = opts?.Headless ?? false;
// Print an intro if not in headless mode
if (!(opts?.Headless ?? false))
if (!headless)
{
Console.WriteLine("Welcome to CKAN!");
Console.WriteLine("");
Expand All @@ -33,7 +34,7 @@ public int RunCommand(object raw_options)
while (!done)
{
// Prompt if not in headless mode
if (!(opts?.Headless ?? false))
if (!headless)
{
Console.Write(
manager.CurrentInstance != null
Expand All @@ -44,7 +45,7 @@ public int RunCommand(object raw_options)
try
{
// Get input
string command = ReadLineWithCompletion();
string command = ReadLineWithCompletion(headless);
if (command == null || command == exitCommand)
{
done = true;
Expand All @@ -55,7 +56,7 @@ public int RunCommand(object raw_options)
// but with a persistent GameInstanceManager object.
int cmdExitCode = MainClass.Execute(manager, opts, command.Split(' '));
// Clear the command if no exception was thrown
if ((opts?.Headless ?? false) && cmdExitCode != Exit.OK)
if (headless && cmdExitCode != Exit.OK)
{
// Pass failure codes to calling process in headless mode
// (in interactive mode the user can see the error and try again)
Expand All @@ -73,11 +74,13 @@ public int RunCommand(object raw_options)
return Exit.OK;
}

private string ReadLineWithCompletion()
private string ReadLineWithCompletion(bool headless)
{
try
{
return ReadLine.Read("");
// ReadLine.Read can't read from pipes
return headless ? Console.ReadLine()
: ReadLine.Read("");
}
catch (InvalidOperationException)
{
Expand Down