-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
231d9f5
commit 6529906
Showing
13 changed files
with
232 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
using System.Diagnostics; | ||
using System.Text.RegularExpressions; | ||
using Spectre.Console; | ||
|
||
namespace DevEx.Core.Helpers | ||
{ | ||
public static class TerminalHelper | ||
{ | ||
public static string Run(ConsoleMode consoleMode, string command, string? directory = null, bool waitForExit = true, bool redirectStandardOutput = true, string regexPattern = null, bool isPrintable = true) | ||
Check warning on line 9 in src/DevEx.Core/Helpers/TerminalHelper.cs GitHub Actions / build-test-publish
|
||
{ | ||
if (isPrintable) | ||
{ | ||
AnsiConsole.WriteLine(); | ||
AnsiConsole.MarkupLine($"[yellow]Command: {command}[/]"); | ||
AnsiConsole.MarkupLine($"[yellow]Directory: {directory}[/]"); | ||
} | ||
|
||
var filename = string.Empty; | ||
var arguments = string.Empty; | ||
|
||
switch (consoleMode) | ||
{ | ||
case ConsoleMode.Cmd: | ||
filename = "cmd.exe"; | ||
arguments = $"/C {command}"; | ||
break; | ||
case ConsoleMode.Powershell: | ||
filename = "powershell.exe"; | ||
arguments = $" -noprofile -nologo -c {command}"; | ||
break; | ||
case ConsoleMode.Wsl: | ||
filename = "wsl"; | ||
arguments = command; | ||
break; | ||
} | ||
|
||
try | ||
{ | ||
var p = new Process(); | ||
if (!string.IsNullOrEmpty(directory)) | ||
{ | ||
p.StartInfo.WorkingDirectory = directory; | ||
} | ||
p.StartInfo.UseShellExecute = false; | ||
//p.StartInfo.RedirectStandardInput = true; | ||
p.StartInfo.RedirectStandardOutput = redirectStandardOutput; | ||
p.StartInfo.FileName = filename; | ||
p.StartInfo.Arguments = arguments; | ||
p.Start(); | ||
//p.StandardInput.WriteLine("Y"); | ||
if (waitForExit) | ||
{ | ||
var output = p.StandardOutput.ReadToEnd(); | ||
p.WaitForExit(); | ||
|
||
if (!string.IsNullOrEmpty(regexPattern)) | ||
{ | ||
Regex regex = new Regex(regexPattern); | ||
Match match = regex.Match(output); | ||
if (match.Success) | ||
{ | ||
output = match.Value; | ||
} | ||
} | ||
|
||
if (isPrintable) | ||
{ | ||
AnsiConsole.MarkupLine($"[yellow]Output: {output.Replace("[", "[[").Replace("]", "]]")}[/]"); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
catch (Exception ex) | ||
{ | ||
AnsiConsole.MarkupLine($"[red]{ex.Message}[/]"); | ||
return string.Empty; | ||
} | ||
} | ||
|
||
public static void RunInteractive(ConsoleMode consoleMode, string command, string directory = null) | ||
Check warning on line 83 in src/DevEx.Core/Helpers/TerminalHelper.cs GitHub Actions / build-test-publish
|
||
{ | ||
try | ||
{ | ||
string filename, arguments; | ||
DisplayLogInConsole(command, directory); | ||
BuildCommand(consoleMode, command, out filename, out arguments); | ||
Process p = StartProcess(directory, filename, arguments, true); | ||
while (!p.HasExited) | ||
{ | ||
var output = p.StandardOutput.ReadLine(); | ||
AnsiConsole.MarkupLine($"[yellow]{output?.Replace("[", "[[").Replace("]", "]]")}[/]"); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
AnsiConsole.MarkupLine($"[red]{ex.Message}[/]"); | ||
} | ||
} | ||
|
||
private static Process StartProcess(string? directory, string filename, string arguments, bool redirectStandard) | ||
{ | ||
var p = new Process(); | ||
if (!string.IsNullOrEmpty(directory)) | ||
{ | ||
p.StartInfo.WorkingDirectory = directory; | ||
} | ||
p.StartInfo.UseShellExecute = false; | ||
p.StartInfo.RedirectStandardOutput = redirectStandard; | ||
p.StartInfo.RedirectStandardError = redirectStandard; | ||
p.StartInfo.FileName = filename; | ||
p.StartInfo.Arguments = arguments; | ||
p.Start(); | ||
return p; | ||
} | ||
|
||
private static void BuildCommand(ConsoleMode consoleMode, string command, out string filename, out string arguments) | ||
{ | ||
filename = string.Empty; | ||
arguments = string.Empty; | ||
switch (consoleMode) | ||
{ | ||
case ConsoleMode.Cmd: | ||
filename = "cmd.exe"; | ||
arguments = $"/C {command}"; | ||
break; | ||
case ConsoleMode.Powershell: | ||
filename = "powershell.exe"; | ||
arguments = $" -noprofile -nologo -c {command}"; | ||
break; | ||
case ConsoleMode.Wsl: | ||
filename = "ubuntu"; | ||
arguments = $"run \"{command}\""; | ||
break; | ||
} | ||
} | ||
|
||
private static void DisplayLogInConsole(string command, string? directory) | ||
{ | ||
AnsiConsole.WriteLine(); | ||
AnsiConsole.MarkupLine($"[yellow]Command: {command}[/]"); | ||
if (!string.IsNullOrEmpty(directory)) | ||
{ | ||
AnsiConsole.MarkupLine($"[yellow]Directory: {directory}[/]"); | ||
} | ||
} | ||
|
||
public enum ConsoleMode | ||
{ | ||
Cmd, | ||
Powershell, | ||
Wsl | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/DevEx.Modules/DevEx.Modules.Run/DevEx.Modules.Run.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\DevEx.Core\DevEx.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
31 changes: 31 additions & 0 deletions
31
src/DevEx.Modules/DevEx.Modules.Run/Docker/DockerComposeRunCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using DevEx.Core; | ||
using DevEx.Core.Helpers; | ||
using DevEx.Core.Storage; | ||
using Spectre.Console; | ||
|
||
namespace DevEx.Modules.Run.Docker | ||
{ | ||
public class DockerComposeRunCommandHandler : ICommandHandler | ||
{ | ||
public void Execute(Dictionary<string, string> options) | ||
{ | ||
var userStorage = UserStorageManager.GetUserStorage(); | ||
var dockerComposePath = string.Empty; | ||
|
||
try | ||
{ | ||
dockerComposePath = userStorage.Vault["DockerComposePath"]; | ||
dockerComposePath = EncryptionHelper.Decrypt(dockerComposePath); | ||
} | ||
catch (Exception) | ||
{ | ||
AnsiConsole.WriteLine("DockerComposePath Not Found in Vault"); | ||
return; | ||
} | ||
|
||
TerminalHelper.Run(TerminalHelper.ConsoleMode.Powershell, "docker-compose down", dockerComposePath); | ||
TerminalHelper.Run(TerminalHelper.ConsoleMode.Powershell, "docker-compose up -d", dockerComposePath); | ||
TerminalHelper.Run(TerminalHelper.ConsoleMode.Powershell, "docker-compose logs nginx", dockerComposePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters