Skip to content

Commit

Permalink
Improve sanity checks (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Dec 17, 2021
1 parent 482384c commit fc4138d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
3 changes: 3 additions & 0 deletions nanoFirmwareFlasher/CloudsmithPackageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ internal class CloudsmithPackageInfo

[JsonProperty("cdn_url")]
public string DownloadUrl { get; set; }

[JsonProperty("name")]
public string TargetName { get; set; }
}
}
2 changes: 1 addition & 1 deletion nanoFirmwareFlasher/Esp32Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync

if (fitCheck)
{
if (targetName.Contains("ESP32_WROOM_32_V3") &&
if (targetName.EndsWith("REV3") &&
(esp32Device.ChipName.Contains("revision 0") ||
esp32Device.ChipName.Contains("revision 1") ||
esp32Device.ChipName.Contains("revision 2")))
Expand Down
98 changes: 48 additions & 50 deletions nanoFirmwareFlasher/FirmwarePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,46 +219,68 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()

string responseBody = await response.Content.ReadAsStringAsync();

bool targetNotFound = false;

// check for empty array
if (responseBody == "[]")
{
if (Verbosity >= VerbosityLevel.Normal)
targetNotFound = true;
}
else
{
// parse response
List<CloudsmithPackageInfo> packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);

// if no specific version was requested, use latest available
if (string.IsNullOrEmpty(_fwVersion))
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
Console.Write($"Trying to find {_targetName} in community targets repository...");
_fwVersion = packageInfo.ElementAt(0).Version;
// grab download URL
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;
}
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)
{
return ExitCodes.E9005;
}
else
{
downloadUrl = packageInfo.Where(w => w.Version == _fwVersion).Select(s => s.DownloadUrl).FirstOrDefault();
}
}

// try with community targets

requestUri = $"{_cloudsmithPackages}/{_communityTargetsRepo}/?page=1&query=^{_targetName}$ {fwVersion}";
// sanity check for target name matching requested
if(packageInfo.ElementAt(0).TargetName != _targetName)
{
targetNotFound = true;
}
}

response = await _cloudsmithClient.GetAsync(requestUri);
if(targetNotFound)
{
// can't find this target

responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("");

if (responseBody == "[]")
if (Verbosity >= VerbosityLevel.Normal)
{
// can't find this target
// output helpful message
Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine("");
Console.WriteLine("*************************** ERROR **************************");
Console.WriteLine("Couldn't find this target in our Cloudsmith repositories!");
Console.WriteLine("To list the available targets use this option --listtargets.");
Console.WriteLine("************************************************************");
Console.WriteLine("");

if (Verbosity >= VerbosityLevel.Normal)
{
// output helpful message
Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine("");
Console.WriteLine("*************************** ERROR **************************");
Console.WriteLine("Couldn't find this target in our Cloudsmith repositories!");
Console.WriteLine("To list the available targets use this option --listtargets.");
Console.WriteLine("************************************************************");
Console.WriteLine("");

Console.ForegroundColor = ConsoleColor.White;
}
return ExitCodes.E9005;
Console.ForegroundColor = ConsoleColor.White;
}

return ExitCodes.E9005;
}

if (Verbosity >= VerbosityLevel.Normal)
Expand All @@ -268,30 +290,6 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
Console.ForegroundColor = ConsoleColor.White;
}

// parse response
List<CloudsmithPackageInfo> packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);

// if no specific version was requested, use latest available
if (string.IsNullOrEmpty(_fwVersion))
{
_fwVersion = packageInfo.ElementAt(0).Version;
// grab download URL
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;
}
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)
{
return ExitCodes.E9005;
}
else
{
downloadUrl = packageInfo.Where(w => w.Version == _fwVersion).Select(s => s.DownloadUrl).FirstOrDefault();
}
}

// set exposed property
Version = _fwVersion;

Expand Down

0 comments on commit fc4138d

Please sign in to comment.