diff --git a/MycroForge.CLI/Bash.cs b/MycroForge.CLI/Bash.cs index 380fd8f..2a0e74e 100644 --- a/MycroForge.CLI/Bash.cs +++ b/MycroForge.CLI/Bash.cs @@ -1,46 +1,9 @@ using System.Diagnostics; -using System.Text; -using MycroForge.CLI.Exceptions; namespace MycroForge.CLI; public static class Bash { - public static async Task RunAsync(params string[] script) - { - var info = new ProcessStartInfo - { - FileName = "bash", - UseShellExecute = false, - CreateNoWindow = true, - RedirectStandardInput = true, - RedirectStandardOutput = true, - RedirectStandardError = true, - }; - - using var process = Process.Start(info); - - if (process is null) - throw new NullReferenceException("Could not initialize bash process."); - - process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data); - process.BeginOutputReadLine(); - process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data); - process.BeginErrorReadLine(); - - await using var input = process.StandardInput; - foreach (var line in script) - await input.WriteLineAsync(line); - - await input.FlushAsync(); - input.Close(); - - await process.WaitForExitAsync(); - - if (process.ExitCode != 0) - Console.WriteLine($"Process exited with status code {process.ExitCode}."); - } - public static async Task ExecuteAsync(params string[] script) { var info = new ProcessStartInfo @@ -58,6 +21,17 @@ public static class Bash if (process is null) throw new NullReferenceException("Could not initialize bash process."); + process.OutputDataReceived += (sender, args) => { Console.WriteLine(args.Data); }; + process.BeginOutputReadLine(); + + process.ErrorDataReceived += (sender, args) => + { + // Only print error data when it's not empty to prevent noise in the shell + if (!string.IsNullOrEmpty(args.Data)) + Console.WriteLine(args.Data); + }; + process.BeginErrorReadLine(); + await using var input = process.StandardInput; foreach (var line in script) await input.WriteLineAsync(line); @@ -65,16 +39,8 @@ public static class Bash await input.FlushAsync(); input.Close(); - var sb = new StringBuilder(); - sb.Append(await process.StandardOutput.ReadToEndAsync()); - sb.Append(await process.StandardError.ReadToEndAsync()); - Console.WriteLine(sb.ToString()); - await process.WaitForExitAsync(); - // if (process.ExitCode != 0) - // throw new BashException($"Process exited with status code {process.ExitCode}."); - if (process.ExitCode != 0) Console.WriteLine($"Process exited with status code {process.ExitCode}."); } diff --git a/MycroForge.CLI/Commands/MycroForge.Add.cs b/MycroForge.CLI/Commands/MycroForge.Add.cs index 50132ba..68b3f78 100644 --- a/MycroForge.CLI/Commands/MycroForge.Add.cs +++ b/MycroForge.CLI/Commands/MycroForge.Add.cs @@ -24,7 +24,7 @@ public partial class MycroForge private async Task ExecuteAsync() { - await Bash.RunAsync([ + await Bash.ExecuteAsync([ "source .venv/bin/activate", "uvicorn main:app --reload" ]); diff --git a/MycroForge.CLI/Commands/MycroForge.cs b/MycroForge.CLI/Commands/MycroForge.cs index 0b3f6d1..5fb80c3 100644 --- a/MycroForge.CLI/Commands/MycroForge.cs +++ b/MycroForge.CLI/Commands/MycroForge.cs @@ -9,9 +9,7 @@ public partial class MycroForge : RootCommand public MycroForge(IEnumerable> commands) : base("The MycroForge CLI tool.") { - commands - .Cast() - .ToList() - .ForEach(AddCommand); + foreach (var subCommandOf in commands.Cast()) + AddCommand(subCommandOf); } } \ No newline at end of file diff --git a/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs b/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs index 75a6f6c..f00f5ff 100644 --- a/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs +++ b/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs @@ -8,7 +8,12 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddServices(this IServiceCollection services, string[] args) { - services.AddScoped(_ => new ArgsContext { Args = args }); + services.AddScoped(_ => + new ArgsContext + { + // Make sure to display the help text when no args are passed + Args = args.Length == 0 ? ["--help"] : args + }); services.AddScoped(); services.AddScoped(); services.AddScoped();