Skip to content

Commit

Permalink
Fix issue with query to Cloudsmith (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Mar 31, 2022
1 parent c07ffd5 commit 41777d2
Showing 1 changed file with 54 additions and 23 deletions.
77 changes: 54 additions & 23 deletions nanoFirmwareFlasher/FirmwarePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
var fwVersion = string.IsNullOrEmpty(_fwVersion) ? "latest" : _fwVersion;

// compose query
string requestUri = $"{repoName}/?query=name:{_targetName} version:^{fwVersion}$";
string requestUri = $"{repoName}/?query=name:^{_targetName}$ version:^{fwVersion}$";

string downloadUrl = string.Empty;

Expand Down Expand Up @@ -233,6 +233,7 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
string responseBody = await response.Content.ReadAsStringAsync();

bool targetNotFound = false;

bool packageOutdated = false;
List<CloudsmithPackageInfo> packageInfo = null;

Expand All @@ -246,39 +247,69 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
// parse response
packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);

// if no specific version was requested, use latest available
if (string.IsNullOrEmpty(_fwVersion))
// sanity check
if (packageInfo.Count() != 1)
{
_fwVersion = packageInfo.ElementAt(0).Version;
// grab download URL
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;
Console.WriteLine("");

if (Verbosity >= VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine($"Several hits returned, expecting only one!");
Console.Write("Please report this issue.");

return ExitCodes.E9005;
}
}
else
{
//get the download Url from the Cloudsmith Package info
// addition check if the cloudsmith json return empty json
if (packageInfo is null || packageInfo.Count == 0)
// if no specific version was requested, use latest available
if (string.IsNullOrEmpty(_fwVersion))
{
return ExitCodes.E9005;
_fwVersion = packageInfo.ElementAt(0).Version;
// grab download URL
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;
}
else
{
downloadUrl = packageInfo.Where(w => w.Version == _fwVersion).Select(s => s.DownloadUrl).FirstOrDefault();
//get the download Url from the Cloudsmith Package info
// addition check if the cloudsmith json return empty json
if (packageInfo is null || packageInfo.Count == 0)
{
return ExitCodes.E9005;
}
else
{
downloadUrl = packageInfo.Where(w => w.Version == _fwVersion).Select(s => s.DownloadUrl).FirstOrDefault();
}
}
}

// sanity check for target name matching requested
if (packageInfo.ElementAt(0).TargetName != _targetName)
{
targetNotFound = true;
}
else
{
// check package published date
if (packageInfo.ElementAt(0).PackageDate < DateTime.UtcNow.AddMonths(-2))
// sanity check for target name matching requested
if (packageInfo.ElementAt(0).TargetName != _targetName)
{
targetNotFound = true;

Console.WriteLine("");

if (Verbosity >= VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine($"There's a mismatch in the target name. Requested '{_targetName}' but got '{packageInfo.ElementAt(0).TargetName}'!");
Console.Write("Please report this issue.");

return ExitCodes.E9005;
}
}
else
{
// if older than 2 months warn user
packageOutdated = true;
// check package published date
if (packageInfo.ElementAt(0).PackageDate < DateTime.UtcNow.AddMonths(-2))
{
// if older than 2 months warn user
packageOutdated = true;
}
}
}
}
Expand Down

0 comments on commit 41777d2

Please sign in to comment.