public CommandResult <string> NugetDownload( CommandEvaluationContext context, [Parameter(0, "package (.nuget) ID")] string id, [Parameter(1, "package version")] string ver, [Option("o", "output", "output path", true, true)] string @out = ".", [Option("u", "download-url", "nuget server api query service template url", true, true)] string url = DownloadUrl ) { id = id.Trim().ToLower(); ver = ver.ToLower(); var queryString = url .Replace("{LOWER_ID}", id) .Replace("{LOWER_VERSION}", ver); context.Out.Echo(context.ShellEnv.Colors.Log + $"GET {queryString} ... "); using var httpClient = new HttpClient(); using var request = new HttpRequestMessage(new HttpMethod("GET"), queryString); var tsk = httpClient.SendAsync(request); var result = tsk.Result; if (result.IsSuccessStatusCode) { var str = result.Content.ReadAsStreamAsync().Result; context.Out.Echoln(" Done(rdc)"); context.Out.Echo(ANSI.RSTXTA + ANSI.CPL(1) + ANSI.EL(ANSI.ELParameter.p2)); // TODO: add as ANSI combo if (str == null) { context.Warning("result is empty"); } else { var fn = $"{id}.{ver}.nupkg"; @out = Path.Combine(@out, fn); using var fstr = new FileStream(@out, FileMode.Create, FileAccess.Write); int b; while ((b = str.ReadByte()) != -1) { fstr.WriteByte((byte)b); } str.Close(); fstr.Close(); context.Out.Echoln($"package '{fn}' has been downloaded to: {new FilePath(@out)}"); return(new CommandResult <string>(@out)); } } else { context.Errorln($"can't get response content: {result.ReasonPhrase}"); return(new CommandResult <string>(ReturnCode.Error)); } return(new CommandResult <string>(ReturnCode.Error)); }
public CommandResult <PackageVersions> NugetVer( CommandEvaluationContext context, [Parameter(0, "package (.nuget) ID")] string id, [Option("q", "quiet", "mute output of result")] bool quiet = false, [Option("u", "get-url", "nuget server api query service template url", true, true)] string url = GetVerUrl ) { var queryString = url.Replace("{ID}", id.Trim()); context.Out.Echo(context.ShellEnv.Colors.Log + $"GET {queryString} ... "); using var httpClient = new HttpClient(); using var request = new HttpRequestMessage(new HttpMethod("GET"), queryString); var tsk = httpClient.SendAsync(request); var result = tsk.Result; if (result.IsSuccessStatusCode) { var res = result.Content.ReadAsStringAsync().Result; context.Out.Echoln(" Done(rdc)"); context.Out.Echo(ANSI.RSTXTA + ANSI.CPL(1) + ANSI.EL(ANSI.ELParameter.p2)); // TODO: add as ANSI combo if (res != null && !string.IsNullOrWhiteSpace(res)) { if (!quiet) { context.Out.Echoln(res); } var obj = JsonConvert.DeserializeObject <PackageVersions>(res); return(new CommandResult <PackageVersions>(obj)); } else { context.Warning("result is empty"); } } else { context.Errorln($"can't get response content: {result.ReasonPhrase}"); } return(new CommandResult <PackageVersions>(null)); }