From 282b0277ae8225acc1afa3ca7d86706ee3eaf7a3 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sat, 17 Dec 2022 01:29:22 +0000 Subject: [PATCH] settings --- src/isn/Program.cs | 8 ++------ src/isn/Settings.cs | 23 ++++++++++----------- src/isn/commands/PushCommand.cs | 4 ++-- src/isn/commands/set-api-key.cs | 8 ++++---- src/isn/commands/sources.set-default.cs | 2 +- src/isnd/Controllers/Packages/Catalog.cs | 26 +++++++++++++++++++----- src/isnd/Interfaces/IPackageManager.cs | 2 +- src/isnd/Services/PackageManager.cs | 2 +- src/isnd/Startup.cs | 9 ++++++-- src/isnd/isnd.csproj | 1 + 10 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/isn/Program.cs b/src/isn/Program.cs index 32004ea..858b775 100644 --- a/src/isn/Program.cs +++ b/src/isn/Program.cs @@ -17,7 +17,7 @@ namespace isn { var json = File.ReadAllText(cfgSettingIf.FullName); settings = JsonConvert.DeserializeObject(json); - currentSource = settings.DefaultSource; + currentSource = settings.DefaultSourceKey; } } static readonly OptionSet storeoptions = new OptionSet { @@ -58,13 +58,11 @@ namespace isn private static bool shouldShowPushHelp; private static string apiKey = null; private static string currentSource = null; - private static int pushKO = 0; private static bool storApiKey = false; public static IDataProtector Protector { get; set; } = new DefaultDataProtector(); static Settings settings = null; - public static Settings Settings { get @@ -170,7 +168,6 @@ namespace isn } List reports = PushPkg(pargs); Console.WriteLine(JsonConvert.SerializeObject(reports)); - pushKO = reports.Count(r => !r.OK && !r.AlreadyPresent); } }; @@ -212,8 +209,7 @@ namespace isn Console.WriteLine("isn version " + GitVersionInformation.AssemblySemFileVer); } - int runCode = commandSet.Run(args); - return (runCode == 0 && pushKO > 0) ? 500 : runCode; + return commandSet.Run(args); } } diff --git a/src/isn/Settings.cs b/src/isn/Settings.cs index bbad901..3f17fa2 100644 --- a/src/isn/Settings.cs +++ b/src/isn/Settings.cs @@ -17,26 +17,25 @@ namespace isn public bool AutoUpdateApiKey { get; set; } = false; - private string source; + private string defSourceKey; + /// - /// Default Url or alias for the source + /// Default source by its alias /// /// - public string DefaultSource + public string DefaultSourceKey { - get => source; + get => defSourceKey; set { - if (Sources.ContainsKey(value)) + if (!Sources.ContainsKey(value)) { - source = value; - return; + Sources[value]=new SourceSettings + { + Alias = defSourceKey + }; } - if (Sources.Values.Any(s => s.Alias == value)) - { - source = Sources[value].Alias; - } - throw new InvalidOperationException("Add it to Sources before, with an API key, as source or alias ."); + defSourceKey = value; } } } diff --git a/src/isn/commands/PushCommand.cs b/src/isn/commands/PushCommand.cs index 44ab15c..be07692 100644 --- a/src/isn/commands/PushCommand.cs +++ b/src/isn/commands/PushCommand.cs @@ -8,11 +8,11 @@ using Newtonsoft.Json; namespace isn { - public class PushCommand + public static class PushCommand { static public PushReport Run(string pkg, string source) { - if (source == null) source = Program.Settings.DefaultSource; + if (source == null) source = Program.Settings.DefaultSourceKey; if (source == null) throw new InvalidOperationException("source is null"); string apikey = Program.Protector.UnProtect(Program.Settings.Sources[source].ApiKey); var resources = SourceHelpers.GetServerResources(source); diff --git a/src/isn/commands/set-api-key.cs b/src/isn/commands/set-api-key.cs index aea8ab6..7181818 100644 --- a/src/isn/commands/set-api-key.cs +++ b/src/isn/commands/set-api-key.cs @@ -30,9 +30,9 @@ namespace isn { if (currentSource == null) { - if (Settings.DefaultSource == null) + if (Settings.DefaultSourceKey == null) return; - currentSource = Settings.DefaultSource; + currentSource = Settings.DefaultSourceKey; } if (Settings.Sources.ContainsKey(currentSource)) @@ -41,14 +41,14 @@ namespace isn { // Une suppression Settings.Sources.Remove(currentSource); - if (Settings.DefaultSource == currentSource) Settings.DefaultSource = null; + if (Settings.DefaultSourceKey == currentSource) Settings.DefaultSourceKey = null; } else { // Une mise À jour string ptd = Protector.Protect(apiKey); Settings.Sources[currentSource].ApiKey = ptd; - if (Settings.DefaultSource == null) Settings.DefaultSource = currentSource; + if (Settings.DefaultSourceKey == null) Settings.DefaultSourceKey = currentSource; } } else if (apiKey != null) diff --git a/src/isn/commands/sources.set-default.cs b/src/isn/commands/sources.set-default.cs index 3062ee9..8a691b3 100644 --- a/src/isn/commands/sources.set-default.cs +++ b/src/isn/commands/sources.set-default.cs @@ -13,7 +13,7 @@ namespace isn Settings.Sources[arg] : Settings.Sources.Values.FirstOrDefault((s)=> s.Alias == arg) ; if (settings==null) throw new InvalidOperationException(arg); - Settings.DefaultSource = arg; + Settings.DefaultSourceKey = arg; SaveConfig(); } diff --git a/src/isnd/Controllers/Packages/Catalog.cs b/src/isnd/Controllers/Packages/Catalog.cs index e735b2b..d795d04 100644 --- a/src/isnd/Controllers/Packages/Catalog.cs +++ b/src/isnd/Controllers/Packages/Catalog.cs @@ -44,13 +44,29 @@ namespace isnd.Controllers return Ok(leaf.First()); } - - [HttpGet(_pkgRootPrefix + ApiConfig.CatalogLeaf + "/{id}/{version}/{lower}/index.json")] - public IActionResult CatalogLeaf(string id, string pversion, string lower) + /// + /// Catalog Leaf, + /// Get info about given package id, and optional lower part . + /// + /// Given Package Id + /// lower part, a semantic version for the package, + /// and eventually followed by the "package type" + /// Info about concerned packages, in order to be able and download them + [HttpGet(_pkgRootPrefix + ApiConfig.CatalogLeaf + "/{id}/{lower?}/index.json")] + public IActionResult CatalogLeaf(string id, string lower = null) { - + Data.PackageVersion[] pkgvs = null; + if (lower == "{lower}") lower = null; bool askForindex = lower == null; - var pkgvs = this.packageManager.GetCatalogLeaf(id, pversion, lower).ToArray(); + if (lower != null && lower.IndexOf('/') > 0 ) + { + string version = lower.Substring(lower.IndexOf('/')); + pkgvs = this.packageManager.GetCatalogLeaf(id, version, lower).ToArray(); + } + else { + pkgvs = this.packageManager.GetCatalogLeaf(id, null, lower).ToArray(); + } + if (pkgvs.Count() == 0) return NotFound(); List types = pkgvs.Select( v => v.Type ?? "Dependency" diff --git a/src/isnd/Interfaces/IPackageManager.cs b/src/isnd/Interfaces/IPackageManager.cs index f2d2448..9a21474 100644 --- a/src/isnd/Interfaces/IPackageManager.cs +++ b/src/isnd/Interfaces/IPackageManager.cs @@ -24,7 +24,7 @@ namespace isnd.Interfaces Task DeletePackageAsync(string pkgid, string version, string type); Task UserAskForPackageDeletionAsync(string userid, string pkgId, string lower, string type); Task GetPackageAsync(string pkgid, string version, string type); - IEnumerable GetCatalogLeaf(string pkgId, string semver, string pkgType); + IEnumerable GetCatalogLeaf(string pkgId, string version, string pkgType); IEnumerable SearchById(string pkgId, string semver, string pkgType); RegistrationPageIndex GetCatalogIndex(); diff --git a/src/isnd/Services/PackageManager.cs b/src/isnd/Services/PackageManager.cs index e9f57de..06bf1c0 100644 --- a/src/isnd/Services/PackageManager.cs +++ b/src/isnd/Services/PackageManager.cs @@ -287,7 +287,7 @@ namespace isnd.Services ); } - public IEnumerable GetCatalogLeaf(string pkgId, string semver, string pkgType) + public IEnumerable GetCatalogLeaf(string pkgId, string semver=null, string pkgType=null) { return dbContext.PackageVersions .Include(v => v.Package) diff --git a/src/isnd/Startup.cs b/src/isnd/Startup.cs index eafa792..3f5c22d 100644 --- a/src/isnd/Startup.cs +++ b/src/isnd/Startup.cs @@ -19,6 +19,7 @@ using isnd.Helpers; using Microsoft.IdentityModel.Tokens; using System; using Microsoft.OpenApi.Models; +using System.IO; namespace isnd { @@ -96,7 +97,8 @@ namespace isnd }; }); services.AddSwaggerGen(options => - options.SwaggerDoc("v1", new OpenApiInfo + { + options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "ToDo API", @@ -112,7 +114,10 @@ namespace isnd Name = "Example License", Url = new Uri("https://isn.pschneider.fr/license") } - })); + }); + var xmlFilename = $"{typeof(Startup).Assembly.GetName().Name}.xml"; + options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/isnd/isnd.csproj b/src/isnd/isnd.csproj index 2aeb0d3..c2039f7 100644 --- a/src/isnd/isnd.csproj +++ b/src/isnd/isnd.csproj @@ -9,6 +9,7 @@ 1.0.5.0 1.0.5+Branch.main.Sha.14206ac477d0f07566d5e8125dc52cbd7f474ca2 1.0.5 + true