Skip to content

Commit

Permalink
Updates in hosting, spectre version, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakenuts committed Jan 1, 2024
1 parent d43f077 commit 36c2ea8
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 271 deletions.
18 changes: 6 additions & 12 deletions src/Commands/AuthorizedCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@

namespace devops.Commands;

public abstract class AuthorizedCommandBase<TSettings> : AsyncCommand<TSettings> where TSettings : CommandSettings
public abstract class AuthorizedCommandBase<TSettings>(IAnsiConsole console, DevOpsConfigurationAccessor devoptions)
: AsyncCommand<TSettings>
where TSettings : CommandSettings
{
protected readonly IAnsiConsole _console;

private readonly DevOpsConfigurationAccessor _devoptionsAccessor;
protected readonly IAnsiConsole Console = console;

protected VssConnection? VssConnection;

protected AuthorizedCommandBase(IAnsiConsole console, DevOpsConfigurationAccessor devoptions)
{
_console = console;
_devoptionsAccessor = devoptions;
}

public override async Task<int> ExecuteAsync(CommandContext context, TSettings settings)
{
try
{
var config = _devoptionsAccessor.GetSettings();
var config = devoptions.GetSettings();

var devopsUri = new Uri(config.CollectionUri);
var deopsCreds = new VssBasicCredential(string.Empty, config.CollectionPAT);
Expand All @@ -35,7 +29,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, TSettings s
}
catch (OptionsValidationException ex)
{
_console.WriteLine("Could not connect to Azure Devops - " + ex.Message);
Console.WriteLine("Could not connect to Azure Devops - " + ex.Message);
return Constants.UnauthorizedExitCode;
}

Expand Down
88 changes: 23 additions & 65 deletions src/Commands/InitCommand.cs
Original file line number Diff line number Diff line change
@@ -1,90 +1,48 @@
ο»Ώusing System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
ο»Ώusing System.Diagnostics.CodeAnalysis;
using devops.Internal;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.Services.Common.CommandLine;
using Spectre.Console;
using Spectre.Console.Cli;

namespace devops.Commands;

public class InitCommand : Command<InitCommand.Settings>
public class InitCommand(
IAnsiConsole console,
DevOpsConfigurationStore configStore,
DevOpsConfigurationAccessor configurationAccessor)
: Command<InitCommand.Settings>
{
private readonly IAnsiConsole _console;

private readonly DevOpsConfigurationProtector _protector;

private readonly DevOpsConfigurationAccessor _configurationAccessor;

public sealed class Settings : CommandSettings
{
}

public InitCommand(IAnsiConsole console, DevOpsConfigurationProtector protector, DevOpsConfigurationAccessor configurationAccessor)
{
_console = console;
_protector = protector;
_configurationAccessor = configurationAccessor;
}

private void SaveConfiguration(DevOpsConfiguration config)
{
var saveConfig = new DevOpsConfiguration()
{
CollectionPAT = config.CollectionPAT,
CollectionUri = config.CollectionUri
};

_protector.Encrypt(saveConfig);

var configAsJson = JsonSerializer.Serialize(config);
var configFile = "{\"DevOps\":" + configAsJson + "\n}";

if (!Directory.Exists(Constants.SettingsDirectory))
{
Directory.CreateDirectory(Constants.SettingsDirectory);
}

var path = Constants.SettingsPath;
File.WriteAllText(path, configFile);

_console.WriteLine($"Updated encrypted devops settings at '${path}'.");
}

public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{


_console.WriteLine("To connect to AzureDevops we'll need the url to your project collection and a PAT authorized to view it");
_console.WriteLine("Examples:");
_console.WriteLine("Url - https://mycompany.visualstudio.com/defaultcollection");
_console.WriteLine("PAT - apatfromazuredevopswhichisalongstringofcharacters");
_console.WriteLine("");
console.WriteLine(
"To connect to AzureDevops we'll need the url to your project collection and a PAT authorized to view it");
console.WriteLine("Examples:");
console.WriteLine("Url - https://mycompany.visualstudio.com/defaultcollection");
console.WriteLine("PAT - apatfromazuredevopswhichisalongstringofcharacters");
console.WriteLine("");

var uri = _console.Ask<string>("Enter DevOps [green]Url[/] :");
var uri = console.Ask<string>("Enter DevOps [green]Url[/] :");

var pat = _console.Ask<string>("Enter DevOps [green]PAT[/] :");
var pat = console.Ask<string>("Enter DevOps [green]PAT[/] :");

var update = new DevOpsConfiguration
{
CollectionUri = uri,
CollectionPAT = pat
};

_configurationAccessor.OverrideSettings(update);
// Save it to disk
configStore.SaveConfiguration(update, Constants.SettingsPath);


SaveConfiguration(update);
// Update the loaded settings
configurationAccessor.UpdateSettings(update);

_console.WriteLine("");
console.WriteLine($"Updated encrypted devops settings at '${Constants.SettingsPath}'.");
console.WriteLine("");

return 0;
}

public sealed class Settings : CommandSettings
{
}
}
68 changes: 29 additions & 39 deletions src/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
ο»Ώusing System.Collections.Concurrent;
using System.ComponentModel;
using devops.Internal;
using Microsoft.Extensions.Options;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.Core.WebApi;
using Spectre.Console;
using Spectre.Console.Cli;

namespace devops.Commands;

public class ListCommand : AuthorizedCommandBase<ListCommand.Settings>
public class ListCommand(IAnsiConsole console, DevOpsConfigurationAccessor devoptions)
: AuthorizedCommandBase<ListCommand.Settings>(console, devoptions)
{
public ListCommand(IAnsiConsole console, DevOpsConfigurationAccessor devoptions) : base(console, devoptions)
{

}

public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
var authResult = await base.ExecuteAsync(context, settings);
Expand All @@ -32,14 +27,9 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se

var data = new ConcurrentDictionary<TeamProjectReference, List<string[]>>();

await _console.Progress()
await Console.Progress()
.HideCompleted(false)
.Columns(new ProgressColumn[]
{
new TaskDescriptionColumn(), // Task description
new ProgressBarColumn(), // Progress bar
new SpinnerColumn() // Spinner
})
.Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new SpinnerColumn())
.StartAsync(async ctx =>
{
var task1 = ctx.AddTask("[green] Checking projects..[/]", true, projects.Count);
Expand Down Expand Up @@ -91,11 +81,35 @@ await Parallel.ForEachAsync(projects, CancellationToken.None, async (project, to
}

// Render the table to the console
_console.Write(table);
Console.Write(table);

return 0;
}

private static string GetBuildResultEmoji(BuildResult? result)
{
if (result == null)
{
return "πŸƒ";
}

switch (result.Value)
{
case BuildResult.None:
return "❔";
case BuildResult.Succeeded:
return "βœ…"; //"βœ”";
case BuildResult.PartiallySucceeded:
return "⚠";
case BuildResult.Failed:
return "β›”";
case BuildResult.Canceled:
return "πŸ›‘";
}

return "❔";
}

private static List<string[]> GetBuildRows(Settings settings, List<Build> builds)
{
var grouped = builds.GroupBy(b => new
Expand Down Expand Up @@ -144,30 +158,6 @@ private static List<string[]> GetBuildRows(Settings settings, List<Build> builds
return rows;
}

private static string GetBuildResultEmoji(BuildResult? result)
{
if (result == null)
{
return "πŸƒ";
}

switch (result.Value)
{
case BuildResult.None:
return "❔";
case BuildResult.Succeeded:
return "βœ…"; //"βœ”";
case BuildResult.PartiallySucceeded:
return "⚠";
case BuildResult.Failed:
return "β›”";
case BuildResult.Canceled:
return "πŸ›‘";
}

return "❔";
}

public class Settings : CommandSettings
{
[CommandOption("-f|--failed")]
Expand Down
Loading

0 comments on commit 36c2ea8

Please sign in to comment.