Initial Commit

This commit is contained in:
2023-06-01 12:48:01 -07:00
commit 1f5deedc73
35 changed files with 2262 additions and 0 deletions

View File

@ -0,0 +1,44 @@
using CliWrap;
using CliWrap.Buffered;
namespace Barcode.Host.Shared.Linux;
public class LinuxGroupManager : ILinuxGroupManager
{
public async Task<bool> IsInInputGroup()
{
BufferedCommandResult result = await Cli.Wrap("id")
.ExecuteBufferedAsync();
string output = result.StandardOutput;
const StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries;
bool inInputGroup = output.Split(new[] { ' ' }, options)
.First(p => p.StartsWith("groups"))
.Remove(0, "groups".Length)
.Split(',', options)
.Any(p => p.Contains("input"));
return inInputGroup;
}
public async Task AddUserToInputGroup(string password)
{
using CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(10));
_ = await Cli.Wrap("bash")
.WithArguments($"-c \"echo '{password}' | sudo -S gpasswd -a $USER input")
.ExecuteBufferedAsync(cts.Token);
}
public async Task RebootSystem(string password)
{
using CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(10));
_ = await Cli.Wrap("bash")
.WithArguments($"-c \"echo '{password}' | sudo -S reboot\"")
.ExecuteBufferedAsync(cts.Token);
}
}

View File

@ -0,0 +1,10 @@
namespace Barcode.Host.Shared.Linux;
public interface ILinuxGroupManager
{
Task<bool> IsInInputGroup();
Task AddUserToInputGroup(string password);
Task RebootSystem(string password);
}