|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Net; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Inedo.UPack; |
| 8 | +using Inedo.UPack.Packaging; |
| 9 | + |
| 10 | +namespace Inedo.ProGet.UPack |
| 11 | +{ |
| 12 | + [DisplayName("verify")] |
| 13 | + [Description("Verifies that a specified package hash matches the hash stored in a ProGet Universal feed.")] |
| 14 | + public sealed class Verify : Command |
| 15 | + { |
| 16 | + [DisplayName("package")] |
| 17 | + [Description("Path of a valid .upack file.")] |
| 18 | + [PositionalArgument(0)] |
| 19 | + [ExpandPath] |
| 20 | + public string PackagePath { get; set; } |
| 21 | + |
| 22 | + [DisplayName("source")] |
| 23 | + [Description("URL of a upack API endpoint.")] |
| 24 | + [PositionalArgument(1)] |
| 25 | + public string SourceEndpoint { get; set; } |
| 26 | + |
| 27 | + [DisplayName("user")] |
| 28 | + [Description("User name and password to use for servers that require authentication. Example: username:password")] |
| 29 | + [ExtraArgument] |
| 30 | + public NetworkCredential Authentication { get; set; } |
| 31 | + |
| 32 | + public override async Task<int> RunAsync(CancellationToken cancellationToken) |
| 33 | + { |
| 34 | + var metadata = GetPackageMetadata(); |
| 35 | + var packageId = new UniversalPackageId(metadata.Group, metadata.Name); |
| 36 | + var client = CreateClient(this.SourceEndpoint, this.Authentication); |
| 37 | + var remoteVersion = await client.GetPackageVersionAsync(packageId, metadata.Version, false, cancellationToken); |
| 38 | + |
| 39 | + if (remoteVersion == null) |
| 40 | + throw new UpackException($"Package {packageId} was not found in feed."); |
| 41 | + |
| 42 | + var sha1 = GetSHA1(this.PackagePath); |
| 43 | + |
| 44 | + if (sha1 != remoteVersion.SHA1) |
| 45 | + throw new UpackException($"Package SHA1 value {sha1} did not match remote SHA1 value {remoteVersion.SHA1}"); |
| 46 | + |
| 47 | + Console.WriteLine("Hashes for local and remote package match: " + sha1); |
| 48 | + |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + private UniversalPackageMetadata GetPackageMetadata() |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + using (var package = new UniversalPackage(this.PackagePath)) |
| 57 | + { |
| 58 | + return package.GetFullMetadata().Clone(); |
| 59 | + } |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + throw new UpackException($"The source package file '{this.PackagePath}' does not exist or could not be opened.", ex); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments