using System.Diagnostics;
using System.Runtime.Versioning;

namespace Insight.Agent.Extensions
{
    public static class Linux
    {
        [SupportedOSPlatform("linux")]
        public static string Bash(this string cmd)
        {
            var escaped = cmd.Replace("\"", "\\\"");

            using var proc = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = $"-c \"{escaped}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                }
            };

            proc.Start();
            var result = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();

            return result;
        }
    }
}