|
| 1 | +using MagicTooltips.Services; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using System.Text; |
| 7 | +using System.Text.Json; |
| 8 | + |
| 9 | +namespace MagicTooltips.Providers |
| 10 | +{ |
| 11 | + public class MicrosoftGraphCLIProvider : IProvider |
| 12 | + { |
| 13 | + public string ProviderKey => "mgcli"; |
| 14 | + public string DefaultCommands => "mg"; // macOS will get a new name... |
| 15 | + public string DefaultNounPrefixes => ""; |
| 16 | + public string DefaultFgColor => "#32A5E6"; |
| 17 | + public string DefaultBgColor => ""; |
| 18 | + public string DefaultTemplate => "\uf871 {value}"; |
| 19 | + |
| 20 | + private static string fileHash = null; |
| 21 | + private static string mgAccount = null; |
| 22 | + private static string mgRecordFilePath = null; |
| 23 | + |
| 24 | + public string GetValue() |
| 25 | + { |
| 26 | + string currentFileHash = null; |
| 27 | + try |
| 28 | + { |
| 29 | + if (string.IsNullOrWhiteSpace(mgRecordFilePath)) |
| 30 | + { |
| 31 | + var folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".mg"); |
| 32 | + mgRecordFilePath = Path.Combine(folderPath, "record.txt"); |
| 33 | + LoggingService.LogDebug($"mgRecordFilePath: {mgRecordFilePath}"); |
| 34 | + } |
| 35 | + |
| 36 | + if (!string.IsNullOrEmpty(mgRecordFilePath)) |
| 37 | + { |
| 38 | + currentFileHash = Md5Utility.CalculateMd5(mgRecordFilePath) ?? "null"; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + currentFileHash = "null"; |
| 43 | + } |
| 44 | + } |
| 45 | + catch (Exception ex) |
| 46 | + { |
| 47 | + LoggingService.LogDebug(ex.ToString()); |
| 48 | + } |
| 49 | + |
| 50 | + if (currentFileHash == "null") |
| 51 | + { |
| 52 | + LoggingService.LogDebug("No mgRecordFile found"); |
| 53 | + mgAccount = null; |
| 54 | + } |
| 55 | + if (currentFileHash != fileHash) |
| 56 | + { |
| 57 | + LoggingService.LogDebug("mgRecordFile has changed, clearing cache"); |
| 58 | + fileHash = currentFileHash; |
| 59 | + mgAccount = null; |
| 60 | + } |
| 61 | + |
| 62 | + /* |
| 63 | + * For now, there is no command to get the current user |
| 64 | + * |
| 65 | + * So I'm going to read & parse the record.txt file |
| 66 | + */ |
| 67 | + if (string.IsNullOrWhiteSpace(mgAccount)) |
| 68 | + { |
| 69 | + //var script = "(Get-MgContext).Account"; |
| 70 | + //mgAccount = PowershellInvoker.InvokeScript(script); |
| 71 | + |
| 72 | + if (File.Exists(mgRecordFilePath)) |
| 73 | + { |
| 74 | + var content = File.ReadAllText(mgRecordFilePath); |
| 75 | + using (var contentDoc = JsonDocument.Parse(content)) |
| 76 | + { |
| 77 | + mgAccount = contentDoc.RootElement.GetProperty("username").GetString(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (string.IsNullOrEmpty(mgAccount)) |
| 83 | + { |
| 84 | + return "Not connected"; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + return mgAccount.Trim('"'); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments