-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathSuggestionStore.cs
More file actions
77 lines (66 loc) · 2.75 KB
/
SuggestionStore.cs
File metadata and controls
77 lines (66 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace System.CommandLine.Suggest
{
public class SuggestionStore : ISuggestionStore
{
public string GetCompletions(string exeFileName, string suggestionTargetArguments, TimeSpan timeout)
{
if (string.IsNullOrWhiteSpace(exeFileName))
{
throw new ArgumentException("Value cannot be null, empty, or consist entirely of whitespace.", nameof(exeFileName));
}
if (string.IsNullOrWhiteSpace(suggestionTargetArguments))
{
throw new ArgumentException("Value cannot be null, empty, or consist entirely of whitespace.", nameof(suggestionTargetArguments));
}
string result = "";
try
{
// Invoke target with args
var processStartInfo = new ProcessStartInfo(
exeFileName,
suggestionTargetArguments)
{
UseShellExecute = false,
RedirectStandardOutput = true
};
using (var process = new Process
{
StartInfo = processStartInfo
})
{
process.Start();
Task<string> readToEndTask = process.StandardOutput.ReadToEndAsync();
if (readToEndTask.Wait(timeout) && process.HasExited && process.ExitCode == 0)
{
result = readToEndTask.Result;
}
else
{
process.Kill();
}
}
}
catch (Win32Exception exception)
{
// We don't check for the existence of exeFileName until the exception in case
// it is a command that start process can resolve to a file name.
if (!File.Exists(exeFileName))
{
var message = $"Unable to find the file '{exeFileName}'";
#if DEBUG
Program.LogDebug($"exception: {message}");
#endif
throw new ArgumentException(
message, nameof(exeFileName), exception);
}
}
return result;
}
}
}