Skip to content

Commit

Permalink
fix finding ProcessManager executable
Browse files Browse the repository at this point in the history
  • Loading branch information
capnmidnight committed Feb 25, 2024
1 parent 68e1a41 commit 29f9756
Show file tree
Hide file tree
Showing 3 changed files with 850 additions and 20 deletions.
45 changes: 26 additions & 19 deletions src/Juniper.Processes/ShellCommand.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace Juniper.Processes;

public class ShellCommandNotFoundException : Exception
public class ShellCommandNotFoundException(string message) : Exception(message)
{
public ShellCommandNotFoundException(string message)
: base(message)
{
}
}

public class ShellCommand : AbstractShellCommand
{
private static readonly Dictionary<PlatformID, string[]> exts = new()
private static readonly Dictionary<PlatformID, string[]> Extensions = new()
{
{ PlatformID.Unix, new[] { "", ".app" } },
{ PlatformID.MacOSX, new[] { "", ".app" } },
{ PlatformID.Win32NT, new[] { ".exe", ".cmd" } },
{ PlatformID.Unix, new[] { "" } },
{ PlatformID.Other, new[] { "" } }
};

Expand All @@ -32,14 +30,16 @@ public static bool IsAvailable(string command)
return null;
}

return FindCommandPaths(command).FirstOrDefault();
return FindCommandPaths(command)
.Where(File.Exists)
.FirstOrDefault();
}

public static IEnumerable<string> FindCommandPaths(string? command)
{
if (command is null)
{
return Array.Empty<string>();
return [];
}

var PATH = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
Expand All @@ -55,24 +55,31 @@ public static IEnumerable<string> FindCommandPaths(string? command)
}
}


directories = directories
.Prepend(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!)
.Append("./")
.Distinct()
.Where(d => !string.IsNullOrEmpty(d))
.ToArray();

var platform = Environment.OSVersion.Platform;
return from dir in directories.Distinct()
where !string.IsNullOrEmpty(dir)
&& Directory.Exists(dir)
from ext in exts[Environment.OSVersion.Platform]
let exe = Path.Combine(dir, command + ext)
where File.Exists(exe)
select exe;
var exts = Extensions.Get(platform) ?? Extensions[PlatformID.Other];
return (from dir in directories
from ext in exts
select Path.Combine(dir, command + ext)
).Union(from ext in exts
select command + ext);
}

private static string MakeCommandName(DirectoryInfo? workingDir, ref string? command, ref string[] args)
{
var originalCommandName = command;
command = FindCommandPath(command);
command = FindCommandPath(originalCommandName);

if (command is null)
{
var attemptedCommands = string.Join("\n\t", FindCommandPaths(command).ToArray());
var attemptedCommands = string.Join("\n\t", FindCommandPaths(originalCommandName).ToArray());
throw new ShellCommandNotFoundException($"Could not find command: {originalCommandName}. Tried:\n\t{attemptedCommands}");
}

Expand All @@ -88,7 +95,7 @@ private static string MakeCommandName(DirectoryInfo? workingDir, ref string? com
if (workingDir is not null)
{
var dirName = workingDir.Name;
if(workingDir.Parent is not null)
if (workingDir.Parent is not null)
{
dirName = Path.Combine(workingDir.Parent.Name, dirName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Juniper.Server/Services/JuniperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static WebApplicationBuilder ConfigureJuniperWebApplication(this WebAppli
{
builder.Services.AddHttpLogging(opts =>
{
opts.LoggingFields = HttpLoggingFields.All;
opts.LoggingFields = HttpLoggingFields.All & ~HttpLoggingFields.RequestBody;
opts.RequestHeaders.Add("host");
opts.RequestHeaders.Add("user-agent");
});
Expand Down
Loading

0 comments on commit 29f9756

Please sign in to comment.