Skip to content

Commit c923181

Browse files
committed
Preview support for MG CLI
1 parent 43143b3 commit c923181

File tree

7 files changed

+131
-39
lines changed

7 files changed

+131
-39
lines changed

src/MagicTooltips/Dtos/ProviderKeys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public class ProviderKeys
44
{
5+
public static string MGCLI = "mgcli"; // Probably merge w/PWSH. Review after release
56
public static string MicrosoftGraph = "mg";
67
public static string M365 = "m365";
78
public static string Kubernetes = "kubernetes";

src/MagicTooltips/Providers/AzureProvider.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public string GetValue()
3232
}
3333
azProfilePath = Path.Combine(azConfigDir, "azureProfile.json");
3434
}
35-
currentFileHash = CalculateMd5(azProfilePath);
35+
currentFileHash = Md5Utility.CalculateMd5(azProfilePath);
3636
}
3737
catch (Exception ex)
3838
{
@@ -55,17 +55,5 @@ public string GetValue()
5555

5656
return azureAccount;
5757
}
58-
59-
private static string CalculateMd5(string filePath)
60-
{
61-
using (var md5 = MD5.Create())
62-
{
63-
using (var stream = File.OpenRead(filePath))
64-
{
65-
var hash = md5.ComputeHash(stream);
66-
return BitConverter.ToString(hash);
67-
}
68-
}
69-
}
7058
}
7159
}

src/MagicTooltips/Providers/M365Provider.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public string GetValue()
2727
{
2828
m365ConnectionInfoFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cli-m365-tokens.json");
2929
}
30-
currentFileHash = CalculateMd5(m365ConnectionInfoFilePath);
30+
currentFileHash = Md5Utility.CalculateMd5(m365ConnectionInfoFilePath);
3131
}
3232
catch (Exception ex)
3333
{
@@ -55,16 +55,5 @@ public string GetValue()
5555
return m365Account.Trim('"');
5656
}
5757

58-
private static string CalculateMd5(string filePath)
59-
{
60-
using (var md5 = MD5.Create())
61-
{
62-
using (var stream = File.OpenRead(filePath))
63-
{
64-
var hash = md5.ComputeHash(stream);
65-
return BitConverter.ToString(hash);
66-
}
67-
}
68-
}
6958
}
7059
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Security.Cryptography;
5+
using System.Text;
6+
7+
namespace MagicTooltips.Providers
8+
{
9+
static class Md5Utility
10+
{
11+
internal static string CalculateMd5(string filePath)
12+
{
13+
if (File.Exists(filePath))
14+
{
15+
16+
using (var md5 = MD5.Create())
17+
{
18+
using (var stream = File.OpenRead(filePath))
19+
{
20+
var hash = md5.ComputeHash(stream);
21+
return BitConverter.ToString(hash);
22+
}
23+
}
24+
}
25+
else
26+
{
27+
return null;
28+
}
29+
}
30+
31+
}
32+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

src/MagicTooltips/Providers/MicrosoftGraphPowerShellProvider.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public string GetValue()
3535

3636
if (!string.IsNullOrEmpty(mgCacheFilePath))
3737
{
38-
currentFileHash = CalculateMd5(mgCacheFilePath);
38+
currentFileHash = Md5Utility.CalculateMd5(mgCacheFilePath);
3939
}
4040
else
4141
{
@@ -74,17 +74,5 @@ public string GetValue()
7474
return mgAccount.Trim('"');
7575
}
7676
}
77-
78-
private static string CalculateMd5(string filePath)
79-
{
80-
using (var md5 = MD5.Create())
81-
{
82-
using (var stream = File.OpenRead(filePath))
83-
{
84-
var hash = md5.ComputeHash(stream);
85-
return BitConverter.ToString(hash);
86-
}
87-
}
88-
}
8977
}
9078
}

src/MagicTooltips/Services/ProviderFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static Dictionary<string, IProvider> GetAllProviders()
3030
{ ProviderKeys.Azure, new AzureProvider() },
3131
{ ProviderKeys.Aws, new AwsProvider() },
3232
{ ProviderKeys.M365, new M365Provider() },
33-
{ ProviderKeys.MicrosoftGraph, new MicrosoftGraphPowerShellProvider() }
33+
{ ProviderKeys.MicrosoftGraph, new MicrosoftGraphPowerShellProvider() },
34+
{ ProviderKeys.MGCLI, new MicrosoftGraphCLIProvider() }
3435
};
3536
}
3637
return AllProviders;

0 commit comments

Comments
 (0)