-
-
Save mhutch/3ca189807c95167856638b35621cc715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
namespace MacPushTestApp | |
{ | |
class Program | |
{ | |
static int Main(string[] args) | |
{ | |
if (args.Length != 2 || !Guid.TryParse(args[0], out Guid _guid) || !File.Exists(args[1])) | |
{ | |
Console.WriteLine("Usage: push-package APIKEY PACKAGEFILE"); | |
return 1; | |
} | |
string source = "https://www.nuget.org/api/v2/package"; | |
string apiKey = args[0]; | |
string packagePath = args[1]; | |
if (PushPackage(source, apiKey, packagePath).Result) | |
{ | |
return 0; | |
} | |
return 2; | |
} | |
static async Task<bool> PushPackage(string source, string apiKey, string packagePath) | |
{ | |
var request = WebRequest.CreateHttp(source); | |
request.Method = "PUT"; | |
request.SendChunked = true; | |
request.Headers.Add("X-NuGet-ApiKey", apiKey); | |
request.Headers.Add("X-NuGet-Client-Version", "4.3.0"); | |
var content = new MultipartFormDataContent(); | |
var fileStream = new FileStream(packagePath, FileMode.Open, FileAccess.Read, FileShare.Read); | |
var packageContent = new StreamContent(fileStream); | |
packageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream"); | |
//"package" and "package.nupkg" are random names for content deserializing | |
//not tied to actual package name. | |
content.Add(packageContent, "package", "package.nupkg"); | |
request.ContentType = content.Headers.ContentType.ToString(); | |
request.ContentLength = content.Headers.ContentLength.Value; | |
Stream requestStream = request.GetRequestStream(); | |
var data = await content.ReadAsByteArrayAsync(); | |
await requestStream.WriteAsync(data, 0, data.Length); | |
var response = (HttpWebResponse)await request.GetResponseAsync(); | |
if (response.StatusCode != HttpStatusCode.OK) | |
{ | |
Console.WriteLine($"Failed with status code {response.StatusCode}"); | |
return false; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment