Created
March 8, 2019 10:09
-
-
Save Epictek/b6205cd8d655bd337928e60667e2acaa to your computer and use it in GitHub Desktop.
Get latest debian updates in C# .NET Core
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
using CRplc.Ics.Pi.Data; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
public static class Apt { | |
public static async Task Update() { | |
using(var process = new Process() { | |
StartInfo = new ProcessStartInfo { | |
FileName = "/bin/bash", | |
Arguments = "-c \"sudo unbuffer apt update\"", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}) { | |
process.Start(); | |
StreamReader stdout = process.StandardOutput; | |
string line; | |
while ((line = stdout.ReadLine()) != null) { | |
Console.WriteLine(line); | |
} | |
string result = await process.StandardOutput.ReadToEndAsync(); | |
process.WaitForExit(); | |
} | |
} | |
public static async Task < String > GetUpgrades() { | |
await Update(); | |
using(var process = new Process() { | |
StartInfo = new ProcessStartInfo { | |
FileName = "/bin/bash", | |
Arguments = "-c \"sudo apt list --upgradable\"", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}) { | |
process.Start(); | |
StreamReader stdout = process.StandardOutput; | |
string line; | |
List < Package > packageList = new List < Package > (); | |
while ((line = stdout.ReadLine()) != null) { | |
var pattern = @ "(\S+)\/(\S+) (\S+) ([a-z]+) \[upgradable from: (\S+)]"; | |
Match match = Regex.Match(line, pattern); | |
if (match.Success) { | |
var package = new Package { | |
Name = match.Groups[1].Value, | |
CurrentVersion = match.Groups[2].Value, | |
Arch = match.Groups[3].Value, | |
UpgradeVersion = match.Groups[4].Value | |
}; | |
packageList.Add(package); | |
} | |
} | |
process.WaitForExit(); | |
Console.WriteLine(JsonConvert.SerializeObject(packageList)); | |
return JsonConvert.SerializeObject(packageList); | |
} | |
} | |
public static async Task Upgrade() { | |
using(var process = new Process() { | |
StartInfo = new ProcessStartInfo { | |
FileName = "/bin/bash", | |
Arguments = "-c \"sudo unbuffer apt upgrade -y\"", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}) { | |
process.Start(); | |
StreamReader stdout = process.StandardOutput; | |
string line; | |
while ((line = stdout.ReadLine()) != null) { | |
Console.WriteLine(line); | |
} | |
string result = await process.StandardOutput.ReadToEndAsync(); | |
process.WaitForExit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment