Skip to content

Commit fc4138d

Browse files
authored
Improve sanity checks (#100)
1 parent 482384c commit fc4138d

File tree

3 files changed

+52
-51
lines changed

3 files changed

+52
-51
lines changed

nanoFirmwareFlasher/CloudsmithPackageInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ internal class CloudsmithPackageInfo
1616

1717
[JsonProperty("cdn_url")]
1818
public string DownloadUrl { get; set; }
19+
20+
[JsonProperty("name")]
21+
public string TargetName { get; set; }
1922
}
2023
}

nanoFirmwareFlasher/Esp32Operations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ internal static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync
167167

168168
if (fitCheck)
169169
{
170-
if (targetName.Contains("ESP32_WROOM_32_V3") &&
170+
if (targetName.EndsWith("REV3") &&
171171
(esp32Device.ChipName.Contains("revision 0") ||
172172
esp32Device.ChipName.Contains("revision 1") ||
173173
esp32Device.ChipName.Contains("revision 2")))

nanoFirmwareFlasher/FirmwarePackage.cs

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -219,46 +219,68 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
219219

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

222+
bool targetNotFound = false;
223+
222224
// check for empty array
223225
if (responseBody == "[]")
224226
{
225-
if (Verbosity >= VerbosityLevel.Normal)
227+
targetNotFound = true;
228+
}
229+
else
230+
{
231+
// parse response
232+
List<CloudsmithPackageInfo> packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);
233+
234+
// if no specific version was requested, use latest available
235+
if (string.IsNullOrEmpty(_fwVersion))
226236
{
227-
Console.ForegroundColor = ConsoleColor.White;
228-
Console.WriteLine("");
229-
Console.Write($"Trying to find {_targetName} in community targets repository...");
237+
_fwVersion = packageInfo.ElementAt(0).Version;
238+
// grab download URL
239+
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;
240+
}
241+
else
242+
{
243+
//get the download Url from the Cloudsmith Package info
244+
// addition check if the cloudsmith json return empty json
245+
if(packageInfo is null || packageInfo.Count == 0)
246+
{
247+
return ExitCodes.E9005;
248+
}
249+
else
250+
{
251+
downloadUrl = packageInfo.Where(w => w.Version == _fwVersion).Select(s => s.DownloadUrl).FirstOrDefault();
252+
}
230253
}
231254

232-
// try with community targets
233-
234-
requestUri = $"{_cloudsmithPackages}/{_communityTargetsRepo}/?page=1&query=^{_targetName}$ {fwVersion}";
255+
// sanity check for target name matching requested
256+
if(packageInfo.ElementAt(0).TargetName != _targetName)
257+
{
258+
targetNotFound = true;
259+
}
260+
}
235261

236-
response = await _cloudsmithClient.GetAsync(requestUri);
262+
if(targetNotFound)
263+
{
264+
// can't find this target
237265

238-
responseBody = await response.Content.ReadAsStringAsync();
266+
Console.WriteLine("");
239267

240-
if (responseBody == "[]")
268+
if (Verbosity >= VerbosityLevel.Normal)
241269
{
242-
// can't find this target
270+
// output helpful message
271+
Console.ForegroundColor = ConsoleColor.Red;
243272

273+
Console.WriteLine("");
274+
Console.WriteLine("*************************** ERROR **************************");
275+
Console.WriteLine("Couldn't find this target in our Cloudsmith repositories!");
276+
Console.WriteLine("To list the available targets use this option --listtargets.");
277+
Console.WriteLine("************************************************************");
244278
Console.WriteLine("");
245279

246-
if (Verbosity >= VerbosityLevel.Normal)
247-
{
248-
// output helpful message
249-
Console.ForegroundColor = ConsoleColor.Yellow;
250-
251-
Console.WriteLine("");
252-
Console.WriteLine("*************************** ERROR **************************");
253-
Console.WriteLine("Couldn't find this target in our Cloudsmith repositories!");
254-
Console.WriteLine("To list the available targets use this option --listtargets.");
255-
Console.WriteLine("************************************************************");
256-
Console.WriteLine("");
257-
258-
Console.ForegroundColor = ConsoleColor.White;
259-
}
260-
return ExitCodes.E9005;
280+
Console.ForegroundColor = ConsoleColor.White;
261281
}
282+
283+
return ExitCodes.E9005;
262284
}
263285

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

271-
// parse response
272-
List<CloudsmithPackageInfo> packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);
273-
274-
// if no specific version was requested, use latest available
275-
if (string.IsNullOrEmpty(_fwVersion))
276-
{
277-
_fwVersion = packageInfo.ElementAt(0).Version;
278-
// grab download URL
279-
downloadUrl = packageInfo.ElementAt(0).DownloadUrl;
280-
}
281-
else
282-
{
283-
//get the download Url from the Cloudsmith Package info
284-
// addition check if the cloudsmith json return empty json
285-
if(packageInfo is null || packageInfo.Count == 0)
286-
{
287-
return ExitCodes.E9005;
288-
}
289-
else
290-
{
291-
downloadUrl = packageInfo.Where(w => w.Version == _fwVersion).Select(s => s.DownloadUrl).FirstOrDefault();
292-
}
293-
}
294-
295293
// set exposed property
296294
Version = _fwVersion;
297295

0 commit comments

Comments
 (0)