Skip to content

Commit b8a7503

Browse files
committed
Code fix
1 parent 4bafa46 commit b8a7503

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

Plugins/Flow.Launcher.Plugin.ProcessKiller/PortHelper.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ internal class PortHelper
4040

4141
private static string ClassName => nameof(PortHelper);
4242

43-
public static Tuple<bool, PortDetail> GetPortDetails(int port, PluginInitContext context)
43+
public static (bool Result, PortDetail Detail) GetPortDetails(int port, PluginInitContext context)
4444
{
45-
PortDetail PortDetail = new PortDetail();
46-
Tuple<bool, PortDetail> result = Tuple.Create(false, PortDetail);
45+
var portDetail = new PortDetail();
4746

4847
// execute netstat command for the given port
4948
string commandArgument = string.Format("/c netstat -an -o -p tcp|findstr \":{0}.*LISTENING\"", port);
@@ -52,49 +51,49 @@ public static Tuple<bool, PortDetail> GetPortDetails(int port, PluginInitContext
5251
if (string.IsNullOrEmpty(commandOut))
5352
{
5453
// port is not in use
55-
return result;
54+
return (false, portDetail);
5655
}
5756

5857
var stringTokens = commandOut.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
5958
if (stringTokens.Length < MINIMUM_TOKEN_IN_A_LINE)
6059
{
61-
return result;
60+
return (false, portDetail);
6261
}
6362

6463
// split host:port
6564
var hostPortTokens = stringTokens[1].Split(new char[] { ':' });
6665
if (hostPortTokens.Length < 2)
6766
{
68-
return result;
67+
return (false, portDetail);
6968
}
7069

7170
if (!int.TryParse(hostPortTokens[1], out var portFromHostPortToken))
7271
{
73-
return result;
72+
return (false, portDetail);
7473
}
7574

7675
if (portFromHostPortToken != port)
7776
{
78-
return result;
77+
return (false, portDetail);
7978
}
8079

81-
PortDetail.Port = port;
82-
PortDetail.ProcessID = int.Parse(stringTokens[4].Trim());
80+
portDetail.Port = port;
81+
portDetail.ProcessID = int.Parse(stringTokens[4].Trim());
8382
(string Name, string Path) processNameAndPath;
8483
try
8584
{
86-
processNameAndPath = GetProcessNameAndCommandLineArgs(PortDetail.ProcessID, context);
87-
PortDetail.ProcessName = processNameAndPath.Name;
88-
PortDetail.Path = processNameAndPath.Path;
89-
PortDetail.Process = Process.GetProcessById(PortDetail.ProcessID);
90-
result = Tuple.Create(true, PortDetail);
85+
processNameAndPath = GetProcessNameAndCommandLineArgs(portDetail.ProcessID, context);
86+
portDetail.ProcessName = processNameAndPath.Name;
87+
portDetail.Path = processNameAndPath.Path;
88+
portDetail.Process = Process.GetProcessById(portDetail.ProcessID);
89+
return (true, portDetail);
9190
}
9291
catch (Exception e)
9392
{
9493
context.API.LogException(ClassName, "Failed to get process name and path", e);
9594
}
9695

97-
return result;
96+
return (false, portDetail);
9897
}
9998

10099
/// <summary>

Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ public List<ProcessResult> GetMatchingProcesses(string searchTerm, PluginInitCon
4141
var processlist = new List<ProcessResult>();
4242

4343
bool canConvert = int.TryParse(searchTerm, out var portNum);
44-
Tuple<bool, PortDetail> tcpPortListeningProcess = null;
44+
bool portResult = false;
45+
PortDetail portDetail = new();
4546
if (canConvert)
4647
{
47-
tcpPortListeningProcess = PortHelper.GetPortDetails(portNum, context);
48+
(portResult, portDetail) = PortHelper.GetPortDetails(portNum, context);
4849
}
4950

5051
foreach (var p in Process.GetProcesses())
5152
{
5253
if (IsSystemProcess(p)) continue;
53-
if (tcpPortListeningProcess != null && tcpPortListeningProcess.Item1 && tcpPortListeningProcess.Item2.Process.Id == p.Id)
54+
if (portResult && portDetail.Process.Id == p.Id)
5455
{
5556
continue;
5657
}
@@ -68,9 +69,10 @@ public List<ProcessResult> GetMatchingProcesses(string searchTerm, PluginInitCon
6869
}
6970
}
7071
}
71-
if (tcpPortListeningProcess != null && tcpPortListeningProcess.Item1)
72+
73+
if (portResult)
7274
{
73-
var p = tcpPortListeningProcess.Item2.Process;
75+
var p = portDetail.Process;
7476
processlist.Add(new ProcessResult(p, StringMatcher.FuzzySearch(searchTerm, p.ProcessName + p.Id).Score, portNum));
7577
}
7678
return processlist;

0 commit comments

Comments
 (0)