Skip to content

Commit

Permalink
Fix cli tool and tests to rreflect domain and folder structure changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Feb 13, 2024
1 parent d2db865 commit 0cec145
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 30 deletions.
8 changes: 7 additions & 1 deletion src/ARCValidationPackages/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type ValidationPackageIndex =

static member getSemanticVersionString(i: ValidationPackageIndex) = $"{i.Metadata.MajorVersion}.{i.Metadata.MinorVersion}.{i.Metadata.PatchVersion}";

member this.PrettyPrint() =
$" {this.Metadata.Name} @ version {this.Metadata.MajorVersion}.{this.Metadata.MinorVersion}.{this.Metadata.PatchVersion}{System.Environment.NewLine}{_.Metadata.Description}{System.Environment.NewLine}Last Updated: {this.LastUpdated}{System.Environment.NewLine}"

/// <summary>
/// represents the locally installed version of a validation package, e.g. the path to the local file and the date it was cached.
/// </summary>
Expand Down Expand Up @@ -138,4 +141,7 @@ type ARCValidationPackage =
static member updateCacheDate (date: System.DateTimeOffset) (package: ARCValidationPackage) =
{package with CacheDate = date}

static member getSemanticVersionString(vp: ARCValidationPackage) = $"{vp.Metadata.MajorVersion}.{vp.Metadata.MinorVersion}.{vp.Metadata.PatchVersion}";
static member getSemanticVersionString(vp: ARCValidationPackage) = $"{vp.Metadata.MajorVersion}.{vp.Metadata.MinorVersion}.{vp.Metadata.PatchVersion}";

member this.PrettyPrint() =
$" {this.Metadata.Name} @ version {this.Metadata.MajorVersion}.{this.Metadata.MinorVersion}.{this.Metadata.PatchVersion}{System.Environment.NewLine}{this.Metadata.Description}{System.Environment.NewLine}CacheDate: {this.CacheDate}{System.Environment.NewLine}Installed at: {this.LocalPath}{System.Environment.NewLine}"
18 changes: 17 additions & 1 deletion src/ARCValidationPackages/PackageCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ type PackageCache =
static member getPackage (name: string) (semVerString: string) (cache: PackageCache) =
cache[name][semVerString]

static member getLatestPackage (name: string) (cache: PackageCache) =
cache[name]
|> Seq.maxBy (fun (kv:KeyValuePair<string,ARCValidationPackage>) -> kv.Key)
|> fun x -> x.Value

static member getPackages (name: string) (cache: PackageCache) =
cache[name]

static member getAllPackages (cache: PackageCache) =
cache.Values |> Seq.toList
cache.Values
|> Seq.toList
|> Seq.map (fun x -> x.Values)
|> Seq.concat

static member tryGetPackage (name: string) (semVerString: string) (cache: PackageCache) =
if cache.ContainsKey(name) then
Expand All @@ -80,6 +88,14 @@ type PackageCache =
else
None

static member tryGetLatestPackage (name: string) (cache: PackageCache) =
if cache.ContainsKey(name) then
cache
|> PackageCache.getLatestPackage name
|> Some
else
None

static member tryGetPackages (name: string) (cache: PackageCache) =
if cache.ContainsKey(name) then
cache
Expand Down
4 changes: 2 additions & 2 deletions src/ARCValidationPackages/TopLevelAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ type API =
static member UninstallPackage(
cache: PackageCache,
packageName: string,
?Version: string,
?SemVer: string,
?Verbose: bool
) =
let verbose = defaultArg Verbose false

match Version with
match SemVer with
| None ->

if verbose then printfn $"uninstalling all package versions of {packageName}..."
Expand Down
25 changes: 19 additions & 6 deletions src/arc-validate/APIs/PackageAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ type PackageAPI =
match e with
| PackageNotFound p -> printfn $"Package {p} not found. Your package index might be out of date. Consider updating the index via arc-validate package update-index."
| DownloadError (p, msg) -> printfn $"Error downloading package {p}: {msg}."
| PackageVersionNotFound (p, v) -> printfn $"Package {p} version {v} not found."
| PackageInstallError.APIError e ->
match e with
| RateLimitExceeded msg -> printfn $"Rate limit exceeded: {msg}"
| SerializationError msg -> printfn $"Serialization error: {msg}"
| NotFoundError msg -> printfn $"Not found: {msg}"

static member printGetSyncedConfigAndCacheError (e: GetSyncedConfigAndCacheError) =
match e with
| SyncError msg -> printfn $"Error syncing config and cache: {msg}"
| GetSyncedConfigAndCacheError.APIError e ->
match e with
| RateLimitExceeded msg -> printfn $"Rate limit exceeded: {msg}"
| SerializationError msg -> printfn $"Serialization error: {msg}"
| NotFoundError msg -> printfn $"Not found: {msg}"

static member Install(
args: ParseResults<PackageInstallArgs>,
Expand All @@ -32,8 +43,9 @@ type PackageAPI =
| Ok (config, cache) ->

let packageName = args.TryGetResult(PackageInstallArgs.Package).Value
let version = args.TryGetResult(PackageInstallArgs.PackageVersion)

match (API.InstallPackage(config, cache, packageName, ?Verbose = Verbose, ?Token = Token)) with
match (API.InstallPackage(config, cache, packageName, ?SemVer = version, ?Verbose = Verbose, ?Token = Token)) with
| Ok msg ->
printfn $"{msg}"
ExitCode.Success
Expand All @@ -56,8 +68,9 @@ type PackageAPI =
| Ok (config, cache) ->
let verbose = defaultArg Verbose false
let packageName = args.TryGetResult(PackageUninstallArgs.Package).Value
let version = args.TryGetResult(PackageUninstallArgs.PackageVersion)

match (API.UninstallPackage(cache, packageName, verbose)) with
match (API.UninstallPackage(cache, packageName, ?SemVer = version, Verbose = verbose)) with
| Ok msg ->
printfn $"{msg}"
ExitCode.Success
Expand Down Expand Up @@ -85,21 +98,21 @@ type PackageAPI =
| Ok (config, cache) ->
let verbose = defaultArg Verbose false

let printCachedPackageList (packages: ARCValidationPackage list) =
let printCachedPackageList (packages: seq<ARCValidationPackage>) =
packages
|> fun p ->
if p.Length = 0 then
if Seq.length p = 0 then
printfn $"No validation packages installed."
else
p |> List.iteri (fun i p -> printfn $"{System.Environment.NewLine}[{i}]:\tName: {p.Name}{System.Environment.NewLine}\tInstalled on: {p.CacheDate}{System.Environment.NewLine}\tat: {p.LocalPath}")
p |> Seq.iteri (fun i p -> printfn $"""{System.Environment.NewLine}[{i}]: {p.PrettyPrint()}""")

let printIndexedPackageList (packages: ValidationPackageIndex list) =
packages
|> fun p ->
if p.Length = 0 then
printfn $"No validation packages indexed."
else
p |> List.iteri (fun i p -> printfn $"{System.Environment.NewLine}[{i}]:\tName: {p.FileName}{System.Environment.NewLine}\tLast updated: {p.LastUpdated}")
p |> List.iteri (fun i p -> printfn $"{System.Environment.NewLine}[{i}]: {p.PrettyPrint()}")

match (args.TryGetResult(Installed), args.TryGetResult(Indexed)) with
| None, None | Some Installed, None | None, Some Installed->
Expand Down
16 changes: 12 additions & 4 deletions src/arc-validate/APIs/ValidateAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module ValidateAPI =
let package =
args.TryGetResult(Package)

let version =
args.TryGetResult(PackageVersion)

match package with

| None -> // Default call means validate schema conformity
Expand Down Expand Up @@ -66,13 +69,18 @@ module ValidateAPI =
exitCode <- ExitCode.InternalError

| Ok (config, cache) ->
let package =
match version with
| Some semver -> PackageCache.tryGetPackage packageName semver cache
| None -> PackageCache.tryGetLatestPackage packageName cache

match PackageCache.tryGetPackage packageName cache with
match package with
| Some validationPackage ->
if verbose then
AnsiConsole.MarkupLine($"LOG: [green]validation package [bold underline]{packageName}[/] is installed locally.[/]");
AnsiConsole.MarkupLine($"LOG: running validation against [bold underline green]{packageName}[/].");
AnsiConsole.MarkupLine($"LOG: Output path is:");
AnsiConsole.MarkupLine($"LOG: [green]validation package [bold underline]{packageName}[/] is installed locally:[/]")
AnsiConsole.MarkupLine($"LOG: {validationPackage.PrettyPrint()}")
AnsiConsole.MarkupLine($"LOG: running validation against [bold underline green]{packageName}[/].")
AnsiConsole.MarkupLine($"LOG: Output path is:")
AnsiConsole.Write(TextPath(Path.GetFullPath(outPath)))
AnsiConsole.MarkupLine("")

Expand Down
8 changes: 6 additions & 2 deletions src/arc-validate/CLIArgs/PackageArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
open Argu

type PackageInstallArgs =
| [<Last; ExactlyOnce; MainCommand>] Package of package_name:string
| [<ExactlyOnce; MainCommand>] Package of package_name:string
| [<Unique; AltCommandLine("-pv")>] PackageVersion of package_version:string

interface IArgParserTemplate with
member s.Usage =
match s with
| Package _ -> "name of the validation package to install"
| PackageVersion _ -> "version of the validation package to install. If no version is specified, the latest version will be installed."

type PackageUninstallArgs =
| [<Last; ExactlyOnce; MainCommand>] Package of package_name:string
| [<ExactlyOnce; MainCommand>] Package of package_name:string
| [<Unique; AltCommandLine("-pv")>] PackageVersion of package_version:string

interface IArgParserTemplate with
member s.Usage =
match s with
| Package _ -> "name of the validation package to uninstall"
| PackageVersion _ -> "version of the validation package to uninstall. If no version is specified, all versions will be uninstalled."

type PackageListArgs =
| [<AltCommandLine("-i"); Unique>] Installed
Expand Down
4 changes: 3 additions & 1 deletion src/arc-validate/CLIArgs/ValidateArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ type ValidateArgs =
| [<AltCommandLine("-i")>] ARC_Directory of path:string
| [<AltCommandLine("-o")>] Out_Directory of path:string
| [<AltCommandLine("-p")>] Package of package_name:string
| [<AltCommandLine("-pv")>] PackageVersion of package_version:string

interface IArgParserTemplate with
member s.Usage =
match s with
| Out_Directory _ -> "Optional. Specify a output directory for the test results file (arc-validate-results.xml). Default: file gets written to the arc root folder."
| ARC_Directory _ -> "Optional. Specify a directory that contains the arc to convert. Default: content of the ARC_PATH environment variable. If ARC_PATH is not set: current directory."
| Package _ -> "Optional. Specify a validation package to use on top of the default validation for invenio export. Default: no package is used, only structural validation for invenio export."
| Package _ -> "Optional. Specify a validation package to use on top of the default validation for invenio export. Default: no package is used, only structural validation for invenio export."
| PackageVersion _ -> "Optional. Specify a version of the validation package to use. If no version is specified, the latest version will be used."
Loading

0 comments on commit 0cec145

Please sign in to comment.