Skip to content

Commit

Permalink
Added "run docker-compose" command
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromonaco committed Feb 5, 2025
1 parent 231d9f5 commit 6529906
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 10 deletions.
7 changes: 7 additions & 0 deletions devex-cli.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{92F3
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevEx.Modules.Vault", "src\DevEx.Modules\DevEx.Modules.Vault\DevEx.Modules.Vault.csproj", "{5D74B34E-FBEC-451C-A936-1DE7A5E4B59C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevEx.Modules.Run", "src\DevEx.Modules\DevEx.Modules.Run\DevEx.Modules.Run.csproj", "{519D999F-C7C0-4E16-BE20-7B570C990385}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -44,6 +46,10 @@ Global
{5D74B34E-FBEC-451C-A936-1DE7A5E4B59C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D74B34E-FBEC-451C-A936-1DE7A5E4B59C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D74B34E-FBEC-451C-A936-1DE7A5E4B59C}.Release|Any CPU.Build.0 = Release|Any CPU
{519D999F-C7C0-4E16-BE20-7B570C990385}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{519D999F-C7C0-4E16-BE20-7B570C990385}.Debug|Any CPU.Build.0 = Debug|Any CPU
{519D999F-C7C0-4E16-BE20-7B570C990385}.Release|Any CPU.ActiveCfg = Release|Any CPU
{519D999F-C7C0-4E16-BE20-7B570C990385}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -54,5 +60,6 @@ Global
{186999ED-CCC0-42D0-A5D9-F5828B2E941A} = {3A074D34-F911-46E3-AFDB-B8CFC6E874B0}
{92F3FA74-1E19-46D4-8393-AC9C60BDC32D} = {3A074D34-F911-46E3-AFDB-B8CFC6E874B0}
{5D74B34E-FBEC-451C-A936-1DE7A5E4B59C} = {92F3FA74-1E19-46D4-8393-AC9C60BDC32D}
{519D999F-C7C0-4E16-BE20-7B570C990385} = {92F3FA74-1E19-46D4-8393-AC9C60BDC32D}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions src/DevEx.Console/Commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
]
}
]
},
{
"Name": "run",
"Description": "Run operations",
"SubCommands": [
{
"Name": "docker-compose",
"Description": "Run Docker Compose Configuration",
"Handler": "DevEx.Modules.Run.Docker.DockerComposeRunCommandHandler",
"Parameters": []
}
]
}
]
}
1 change: 1 addition & 0 deletions src/DevEx.Console/DevEx.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<ItemGroup>
<ProjectReference Include="..\DevEx.Core\DevEx.Core.csproj" />
<ProjectReference Include="..\DevEx.Modules\DevEx.Modules.Run\DevEx.Modules.Run.csproj" />
<ProjectReference Include="..\DevEx.Modules\DevEx.Modules.Vault\DevEx.Modules.Vault.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/DevEx.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"DevEx.Console": {
"commandName": "Project",
"commandLineArgs": "vault delete --key ab"
"commandLineArgs": "run docker-compose"
}
}
}
2 changes: 2 additions & 0 deletions src/DevEx.Core/DevEx.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="System.Management" Version="9.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Security.Cryptography;
using System.Text;

namespace DevEx.Modules.Vault.Windows
namespace DevEx.Core.Helpers
{
public class WindowsVaultHelper
public class EncryptionHelper
{
private static readonly byte[] _key = GenerateKey();
private static readonly byte[] _iv = GenerateIV();
Expand Down
157 changes: 157 additions & 0 deletions src/DevEx.Core/Helpers/TerminalHelper.cs
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

View workflow job for this annotation

GitHub Actions / build-test-publish

Cannot convert null literal to non-nullable reference type.

Check warning on line 9 in src/DevEx.Core/Helpers/TerminalHelper.cs

View workflow job for this annotation

GitHub Actions / build-test-publish

Cannot convert null literal to non-nullable reference type.
{
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

View workflow job for this annotation

GitHub Actions / build-test-publish

Cannot convert null literal to non-nullable reference type.

Check warning on line 83 in src/DevEx.Core/Helpers/TerminalHelper.cs

View workflow job for this annotation

GitHub Actions / build-test-publish

Cannot convert null literal to non-nullable reference type.
{
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 src/DevEx.Modules/DevEx.Modules.Run/DevEx.Modules.Run.csproj
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>
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@
<ProjectReference Include="..\..\DevEx.Core\DevEx.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Management" Version="9.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DevEx.Core;
using DevEx.Core.Helpers;
using DevEx.Core.Storage;

namespace DevEx.Modules.Vault.Windows.Handlers
Expand All @@ -17,7 +18,7 @@ public void Execute(Dictionary<string, string> options)
}

var userStorage = UserStorageManager.GetUserStorage();
value = WindowsVaultHelper.Encrypt(value);
value = EncryptionHelper.Encrypt(value);
userStorage.Vault.Add(key, value);
UserStorageManager.SaveUserStorage(userStorage);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DevEx.Core;
using DevEx.Core.Helpers;
using DevEx.Core.Storage;

namespace DevEx.Modules.Vault.Windows.Handlers
Expand All @@ -16,7 +17,7 @@ public void Execute(Dictionary<string, string> options)

var userStorage = UserStorageManager.GetUserStorage();
var value = userStorage.Vault.FirstOrDefault(v => v.Key.Equals(key)).Value;
value = WindowsVaultHelper.Decrypt(value);
value = EncryptionHelper.Decrypt(value);
Console.WriteLine($"Fetched item with Key={options["key"]} and Value={value}");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DevEx.Core;
using DevEx.Core.Helpers;
using DevEx.Core.Storage;

namespace DevEx.Modules.Vault.Windows.Handlers
Expand All @@ -17,7 +18,7 @@ public void Execute(Dictionary<string, string> options)
}

var userStorage = UserStorageManager.GetUserStorage();
userStorage.Vault[key] = WindowsVaultHelper.Encrypt(value);
userStorage.Vault[key] = EncryptionHelper.Encrypt(value);
UserStorageManager.SaveUserStorage(userStorage);

Console.WriteLine($"Modified item: Key={key}, New Value={value}");
Expand Down

0 comments on commit 6529906

Please sign in to comment.