Skip to content

Commit

Permalink
FIX: VmChamp SSH uses correct user to connect to machine
Browse files Browse the repository at this point in the history
- Added: Check if user is not default, read from cloudInit.iso file
- Removed: --user option for SSHCommand
  • Loading branch information
mzwoelfer committed Jun 30, 2024
1 parent 0d4ced8 commit c9947c2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions SshCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public SshCommand(AppConfig appConfig) : base("ssh", "connect to vm via ssh")
() => appConfig.DefaultVmName,
"Name of the new VM");

Option<string> userOption = new("--user",
() => appConfig.DefaultUser,
"Name of the User");

this.AddArgument(nameArgument);
this.AddOption(userOption);

nameArgument.AddCompletions((ctx) =>
Helper.GetAllVmInDirectory(_appConfig.DataDir)
Expand All @@ -31,9 +26,20 @@ public SshCommand(AppConfig appConfig) : base("ssh", "connect to vm via ssh")
Helper.GetAllVmInDirectory(_appConfig.DataDir)
.Select(vmName => new CompletionItem(vmName ?? "")));

this.SetHandler((vmName, user) =>
this.SetHandler((vmName) =>
{
var user = _appConfig.DefaultUser;

var vmDir = Path.Combine(_appConfig.DataDir, vmName);
var userFromIso = Helper.GetUserFromIso(vmDir);

if (userFromIso != "N/A" && userFromIso != _appConfig.DefaultUser)
{
user = userFromIso;
}

AnsiConsole.MarkupLine($"✈️ Connect to: {vmName}. USER: {user}");

using var libvirtConnection = LibvirtConnection.CreateForSession();

var vmId = Interop.virDomainLookupByName(libvirtConnection.NativePtr, vmName);
Expand All @@ -46,6 +52,6 @@ public SshCommand(AppConfig appConfig) : base("ssh", "connect to vm via ssh")
}

Helper.ConnectViaSsh(user, vmIp)?.WaitForExit();
}, nameArgument, userOption);
}, nameArgument);
}
}

0 comments on commit c9947c2

Please sign in to comment.