Skip to content

Commit

Permalink
Cleaning up code based on SonarQube scans
Browse files Browse the repository at this point in the history
  • Loading branch information
rprouse committed Feb 14, 2024
1 parent 18516a2 commit 1fb02af
Show file tree
Hide file tree
Showing 30 changed files with 284 additions and 337 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

# SonarQube
.sonarqube/
coverage.xml

# User-specific files
*.rsuser
*.suo
Expand Down
22 changes: 11 additions & 11 deletions Guppi.Application/Configurations/CalendarConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using Guppi.Application.Attributes;
using Guppi.Domain.Interfaces;
using Spectre.Console;
Expand All @@ -8,12 +10,10 @@ namespace Guppi.Application.Configurations
public class CalendarConfiguration : Configuration
{
[Display("Enabled Calendars")]
public List<EnabledCalendar> EnabledCalendars { get; set; }
= new List<EnabledCalendar>();
public List<EnabledCalendar> EnabledCalendars { get; set; } = [];

[Display("ICal Urls")]
public List<string> ICalUrls { get; set; }
= new List<string>();
public List<string> ICalUrls { get; set; } = [];

IEnumerable<ICalendarService> _calendarServices;
public void SetCalendarServices(IEnumerable<ICalendarService> calendarServices)
Expand All @@ -27,23 +27,23 @@ protected override void ConfigureCustomProperties()
ConfigureICalUrls();
}

private static readonly char[] YES_NO = [ 'y', 'Y', 'n', 'N' ];

private void ConfigureEnabledCalendars()
{
var enabledCalendars = new List<EnabledCalendar>();
foreach (var service in _calendarServices)
EnabledCalendars = _calendarServices.Select(service =>
{
var yesno = AnsiConsole.Prompt<char>(
new TextPrompt<char>($"[green]Enable {service.Name} (y/N)?[/]")
.AddChoices(new[] { 'y', 'Y', 'n', 'N' })
.AddChoices(YES_NO)
.DefaultValue('n')
.ShowDefaultValue(false)
.ShowChoices(false)
.AllowEmpty()
);
bool enabled = yesno == 'y' || yesno == 'Y';
enabledCalendars.Add(new EnabledCalendar { Name = service.Name, Enabled = enabled });
}
EnabledCalendars = enabledCalendars;
return new EnabledCalendar { Name = service.Name, Enabled = enabled };
}).ToList();
}

private void ConfigureICalUrls()
Expand All @@ -54,7 +54,7 @@ private void ConfigureICalUrls()
{
var yesno = AnsiConsole.Prompt<char>(
new TextPrompt<char>($"[green]Delete {url} (y/N)?[/]")
.AddChoices(new[] { 'y', 'Y', 'n', 'N' })
.AddChoices(YES_NO)
.DefaultValue('n')
.ShowDefaultValue(false)
.ShowChoices(false)
Expand Down
5 changes: 1 addition & 4 deletions Guppi.Application/Exceptions/WarningException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace Guppi.Application.Exceptions
{
public class WarningException : Exception
public class WarningException(string message) : Exception(message)
{
public WarningException(string message) : base(message)
{
}
}
}
2 changes: 1 addition & 1 deletion Guppi.Application/Extensions/LongExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class LongExtensions
public static DateTime UnixTimeStampToDateTime(this long unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
DateTime dtDateTime = DateTime.UnixEpoch;
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
Expand Down
7 changes: 5 additions & 2 deletions Guppi.Application/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Guppi.Application.Extensions
{
public static class StringExtensions
public static partial class StringExtensions
{
static readonly Regex emojiRegex = new Regex(@"(:[a-z_]+:)", RegexOptions.Compiled);
static readonly Regex emojiRegex = EmojiRegex();

public static string StripEmoji(this string str)
{
Expand All @@ -17,5 +17,8 @@ public static string StripEmoji(this string str)
}
return str;
}

[GeneratedRegex(@"(:[a-z_]+:)", RegexOptions.Compiled)]
private static partial Regex EmojiRegex();
}
}
1 change: 1 addition & 0 deletions Guppi.Application/Guppi.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>
Expand Down
160 changes: 80 additions & 80 deletions Guppi.Application/Services/AsciiService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Guppi.Domain.Entities.Ascii;

namespace Guppi.Application.Services;
Expand All @@ -9,96 +10,95 @@ public AsciiData[] GetAsciiTable()
{
var result = new List<AsciiData>(128)
{
new AsciiData(0, "NUL", "Null"),
new AsciiData(1, "SOH", "Start of Header"),
new AsciiData(2, "STX", "Start of Text"),
new AsciiData(3, "ETX", "End of Text"),
new AsciiData(4, "EOT", "End of Transmission"),
new AsciiData(5, "ENQ", "Enquiry"),
new AsciiData(6, "ACK", "Acknowledge"),
new AsciiData(7, "BEL", "Bell"),
new AsciiData(8, "BS", "Backspace"),
new AsciiData(9, "HT", "Horizontal Tab"),
new AsciiData(10, "LF", "Line Feed"),
new AsciiData(11, "VT", "Vertical Tab"),
new AsciiData(12, "FF", "Form Feed"),
new AsciiData(13, "CR", "Carriage Return"),
new AsciiData(14, "SO", "Shift Out"),
new AsciiData(15, "SI", "Shift In"),
new AsciiData(16, "DLE", "Data Link Escape"),
new AsciiData(17, "DC1", "Device Control 1"),
new AsciiData(18, "DC2", "Device Control 2"),
new AsciiData(19, "DC3", "Device Control 3"),
new AsciiData(20, "DC4", "Device Control 4"),
new AsciiData(21, "NAK", "Negative Acknowledge"),
new AsciiData(22, "SYN", "Synchronize"),
new AsciiData(23, "ETB", "End of Transmission Block"),
new AsciiData(24, "CAN", "Cancel"),
new AsciiData(25, "EM", "End of Medium"),
new AsciiData(26, "SUB", "Substitute"),
new AsciiData(27, "ESC", "Escape"),
new AsciiData(28, "FS", "File Separator"),
new AsciiData(29, "GS", "Group Separator"),
new AsciiData(30, "RS", "Record Separator"),
new AsciiData(31, "US", "Unit Separator"),
new AsciiData(32, "SPACE", "Space"),
new AsciiData(33, "!", "Exclamation Point"),
new AsciiData(34, "\"", "Double Quote"),
new AsciiData(35, "#", "Hash"),
new AsciiData(36, "$", "Dollar"),
new AsciiData(37, "%", "Percent"),
new AsciiData(38, "&", "Ampersand"),
new AsciiData(39, "'", "Single Quote"),
new AsciiData(40, "(", "Left Parenthesis"),
new AsciiData(41, ")", "Right Parenthesis"),
new AsciiData(42, "*", "Asterisk"),
new AsciiData(43, "+", "Plus"),
new AsciiData(44, ",", "Comma"),
new AsciiData(45, "-", "Minus"),
new AsciiData(46, ".", "Period"),
new AsciiData(47, "/", "Slash"),
new (0, "NUL", "Null"),
new (1, "SOH", "Start of Header"),
new (2, "STX", "Start of Text"),
new (3, "ETX", "End of Text"),
new (4, "EOT", "End of Transmission"),
new (5, "ENQ", "Enquiry"),
new (6, "ACK", "Acknowledge"),
new (7, "BEL", "Bell"),
new (8, "BS", "Backspace"),
new (9, "HT", "Horizontal Tab"),
new (10, "LF", "Line Feed"),
new (11, "VT", "Vertical Tab"),
new (12, "FF", "Form Feed"),
new (13, "CR", "Carriage Return"),
new (14, "SO", "Shift Out"),
new (15, "SI", "Shift In"),
new (16, "DLE", "Data Link Escape"),
new (17, "DC1", "Device Control 1"),
new (18, "DC2", "Device Control 2"),
new (19, "DC3", "Device Control 3"),
new (20, "DC4", "Device Control 4"),
new (21, "NAK", "Negative Acknowledge"),
new (22, "SYN", "Synchronize"),
new (23, "ETB", "End of Transmission Block"),
new (24, "CAN", "Cancel"),
new (25, "EM", "End of Medium"),
new (26, "SUB", "Substitute"),
new (27, "ESC", "Escape"),
new (28, "FS", "File Separator"),
new (29, "GS", "Group Separator"),
new (30, "RS", "Record Separator"),
new (31, "US", "Unit Separator"),
new (32, "SPACE", "Space"),
new (33, "!", "Exclamation Point"),
new (34, "\"", "Double Quote"),
new (35, "#", "Hash"),
new (36, "$", "Dollar"),
new (37, "%", "Percent"),
new (38, "&", "Ampersand"),
new (39, "'", "Single Quote"),
new (40, "(", "Left Parenthesis"),
new (41, ")", "Right Parenthesis"),
new (42, "*", "Asterisk"),
new (43, "+", "Plus"),
new (44, ",", "Comma"),
new (45, "-", "Minus"),
new (46, ".", "Period"),
new (47, "/", "Slash"),
};

for (char c = '0'; c <= '9'; c++)
{
result.Add(new AsciiData(c, $"{c}", ""));
}
result.AddRange(CreateAsciiDataFor('0', '9'));

result.AddRange(new[] {
new AsciiData(58, ":", "Colon"),
new AsciiData(59, ";", "Semicolon"),
new AsciiData(60, "<", "Less Than"),
new AsciiData(61, "=", "Equals"),
new AsciiData(62, ">", "Greater Than"),
new AsciiData(63, "?", "Question Mark"),
new AsciiData(64, "@", "At Sign"),
result.AddRange(new AsciiData[] {
new (58, ":", "Colon"),
new (59, ";", "Semicolon"),
new (60, "<", "Less Than"),
new (61, "=", "Equals"),
new (62, ">", "Greater Than"),
new (63, "?", "Question Mark"),
new (64, "@", "At Sign"),
});

for (char c = 'A'; c <= 'Z'; c++)
{
result.Add(new AsciiData(c, $"{c}", ""));
}
result.AddRange(CreateAsciiDataFor('A', 'Z'));

result.AddRange(new[] {
new AsciiData(91, "[", "Left Square Bracket"),
new AsciiData(92, "\\", "Backslash"),
new AsciiData(93, "]", "Right Square Bracket"),
new AsciiData(94, "^", "Circumflex"),
new AsciiData(95, "_", "Underscore"),
new AsciiData(96, "`", "Grave/Accent"),
result.AddRange(new AsciiData[] {
new (91, "[", "Left Square Bracket"),
new (92, "\\", "Backslash"),
new (93, "]", "Right Square Bracket"),
new (94, "^", "Circumflex"),
new (95, "_", "Underscore"),
new (96, "`", "Grave/Accent"),
});

for (char c = 'a'; c <= 'z'; c++)
{
result.Add(new AsciiData(c, $"{c}", ""));
}
result.AddRange(CreateAsciiDataFor('a', 'z'));

result.Add(new AsciiData(123, "{", "Left Curly Brace"));
result.Add(new AsciiData(124, "|", "Vertical Bar"));
result.Add(new AsciiData(125, "}", "Right Curly Brace"));
result.Add(new AsciiData(126, "~", "Tilde"));
result.Add(new AsciiData(127, "DEL", "Delete"));
result.Add(new (123, "{", "Left Curly Brace"));
result.Add(new (124, "|", "Vertical Bar"));
result.Add(new (125, "}", "Right Curly Brace"));
result.Add(new (126, "~", "Tilde"));
result.Add(new (127, "DEL", "Delete"));

return result.ToArray();
}

private static IEnumerable<AsciiData> CreateAsciiDataFor(char start, char end)
{
for (char c = start; c <= end; c++)
{
yield return new (c, $"{c}", "");
}
}
}
2 changes: 1 addition & 1 deletion Guppi.Application/Services/INoteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Guppi.Application.Services;
public interface INoteService
{
void Configure();
void AddFile(string vault, string title);
void AddFile(string title, string vault);
void OpenVsCode();
void OpenObsidian(string vault, string filename);
}
13 changes: 4 additions & 9 deletions Guppi.Application/Services/NoteService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Runtime.InteropServices;
using Guppi.Application.Configurations;
Expand All @@ -7,14 +7,9 @@

namespace Guppi.Application.Services;

internal sealed class NoteService : INoteService
internal sealed class NoteService(IProcessService process) : INoteService
{
private readonly IProcessService _process;

public NoteService(IProcessService process)
{
_process = process;
}
private readonly IProcessService _process = process;

public void AddFile(string title, string vault)
{
Expand Down Expand Up @@ -46,7 +41,7 @@ public void OpenObsidian(string vault, string filename)
_process.Open(uri);
}

internal string GetUriHandler(string vault, string filename) =>
internal static string GetUriHandler(string vault, string filename) =>
string.IsNullOrWhiteSpace(filename) ?
$"obsidian://open?vault={vault}" :
$"obsidian://open?vault={vault}&file={filename}";
Expand Down
21 changes: 7 additions & 14 deletions Guppi.Application/Services/StravaService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand All @@ -16,18 +16,11 @@

namespace Guppi.Application.Services;

internal sealed class StravaService : IStravaService
internal sealed class StravaService(IHttpRestService restService, IProcessService processService) : IStravaService
{
private readonly IHttpRestService _restService;
private readonly IProcessService _processService;
private readonly StravaConfiguration _configuration;

public StravaService(IHttpRestService restService, IProcessService processService)
{
_restService = restService;
_processService = processService;
_configuration = Configuration.Load<StravaConfiguration>("strava");
}
private readonly IHttpRestService _restService = restService;
private readonly IProcessService _processService = processService;
private readonly StravaConfiguration _configuration = Configuration.Load<StravaConfiguration>("strava");

public void Configure()
{
Expand All @@ -54,7 +47,7 @@ public async Task<IEnumerable<Activity>> GetActivities()

_restService.AddHeader("Authorization", $"Bearer {access_token}");

TimeSpan t = DateTime.UtcNow.AddDays(-90) - new DateTime(1970, 1, 1);
TimeSpan t = DateTime.UtcNow.AddDays(-90) - DateTime.UnixEpoch;
int epoch = (int)t.TotalSeconds;
var activities = await _restService.GetData<List<StravaActivity>>($"https://www.strava.com/api/v3/athlete/activities?after={epoch}&per_page=200");
return activities.Select(a => a.GetActivity());
Expand All @@ -63,7 +56,7 @@ public async Task<IEnumerable<Activity>> GetActivities()
private async Task<string> Authorize()
{
int port = 39428;
string url = $"http://www.strava.com/oauth/authorize?client_id={_configuration.ClientId}&response_type=code&redirect_uri=http://localhost:{port}&approval_prompt=auto&scope=read,activity:read_all";
string url = $"https://www.strava.com/oauth/authorize?client_id={_configuration.ClientId}&response_type=code&redirect_uri=http://localhost:{port}&approval_prompt=auto&scope=read,activity:read_all";
OpenUrl(url);

using var listener = new HttpListener();
Expand Down
Loading

0 comments on commit 1fb02af

Please sign in to comment.