1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-28 08:58:41 +02:00

Use OperatingSystem class to get OS information

This commit is contained in:
Hadrien Patte 2024-10-26 23:14:20 +02:00 committed by GitHub
parent 0784f56b9a
commit 135b5c2ddd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using NLog;
@ -25,22 +24,25 @@ public class OsInfo : IOsInfo
static OsInfo()
{
var platform = Environment.OSVersion.Platform;
switch (platform)
if (OperatingSystem.IsWindows())
{
case PlatformID.Win32NT:
{
Os = Os.Windows;
break;
}
case PlatformID.MacOSX:
case PlatformID.Unix:
{
Os = GetPosixFlavour();
break;
}
Os = Os.Windows;
}
else if (OperatingSystem.IsMacOS())
{
Os = Os.Osx;
}
else if (OperatingSystem.IsFreeBSD())
{
Os = Os.Bsd;
}
else
{
#if ISMUSL
Os = Os.LinuxMusl;
#else
Os = Os.Linux;
#endif
}
}
@ -84,59 +86,6 @@ public OsInfo(IEnumerable<IOsVersionAdapter> versionAdapters, Logger logger)
IsDocker = true;
}
}
private static Os GetPosixFlavour()
{
var output = RunAndCapture("uname", "-s");
if (output.StartsWith("Darwin"))
{
return Os.Osx;
}
else if (output.Contains("BSD"))
{
return Os.Bsd;
}
else
{
#if ISMUSL
return Os.LinuxMusl;
#else
return Os.Linux;
#endif
}
}
private static string RunAndCapture(string filename, string args)
{
var processStartInfo = new ProcessStartInfo
{
FileName = filename,
Arguments = args,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
var output = string.Empty;
try
{
using (var p = Process.Start(processStartInfo))
{
// To avoid deadlocks, always read the output stream first and then wait.
output = p.StandardOutput.ReadToEnd();
p.WaitForExit(1000);
}
}
catch (Exception)
{
output = string.Empty;
}
return output;
}
}
public interface IOsInfo