Skip to content

Commit

Permalink
create verify command to check if the data is updated or not
Browse files Browse the repository at this point in the history
  • Loading branch information
To-Ki-O committed Feb 25, 2023
1 parent d8c1956 commit 6eb66a0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
9 changes: 9 additions & 0 deletions dim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SearchAction,
UninstallAction,
UpdateAction,
VerifyAction,
} from "./libs/actions.ts";
import { NAME, VERSION } from "./libs/consts.ts";

Expand Down Expand Up @@ -129,6 +130,14 @@ await new Command()
.description("Search data from package_search CKAN API")
.action(new SearchAction().execute),
)
.command(
"verify",
new Command()
.description(
"Verify the data.\n",
)
.action(new VerifyAction().execute),
)
.command("help", new HelpCommand())
.command("complete", new CompletionsCommand())
.parse(Deno.args);
2 changes: 1 addition & 1 deletion libs/action_helper/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const executePostprocess = async (
}
};

const getIntegrity = function (
export const getIntegrity = function (
targetPath: string,
): string {
const byteArray = Deno.readFileSync(targetPath);
Expand Down
51 changes: 43 additions & 8 deletions libs/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Colors } from "../deps.ts";
import { DEFAULT_DATAFILES_PATH, DEFAULT_DIM_FILE_PATH } from "./consts.ts";
import { DimFileAccessor, DimLockFileAccessor } from "./accessor.ts";
import { ky, Sha1 } from "../deps.ts";
import { CkanApiClient } from "./ckan_api_client.ts";
import { createDataFilesDir, initDimFile, initDimLockFile } from "./action_helper/initializer.ts";
import { installFromDimFile, installFromURL, interactiveInstall, parseHeader } from "./action_helper/installer.ts";
Expand Down Expand Up @@ -39,8 +40,8 @@ export class InstallAction {
console.log(Colors.red("The -n option is not specified."));
Deno.exit(1);
}
const targetContent = new DimFileAccessor().getContents().find((c) => c.name === options.name);
if (targetContent !== undefined && !options.force) {
const targetLockContent = new DimFileAccessor().getContents().find((c) => c.name === options.name);
if (targetLockContent !== undefined && !options.force) {
console.log(Colors.red("The name already exists."));
console.log(
Colors.red(
Expand Down Expand Up @@ -105,7 +106,7 @@ export class UninstallAction {
);
}
const dimLockFileAccessor = new DimLockFileAccessor();
const targetContent = dimLockFileAccessor.getContents().find((c) => c.name === name);
const targetLockContent = dimLockFileAccessor.getContents().find((c) => c.name === name);
const isRemovedDimLockFile = await dimLockFileAccessor.removeContent(name);
if (isRemovedDimLockFile) {
console.log(
Expand All @@ -118,16 +119,16 @@ export class UninstallAction {
),
);
}
if (targetContent !== undefined) {
if (targetLockContent !== undefined) {
try {
Deno.statSync(targetContent.path);
await Deno.remove(targetContent.path);
Deno.statSync(targetLockContent.path);
await Deno.remove(targetLockContent.path);
console.log(
Colors.green(`Removed a file '${targetContent.path}'.`),
Colors.green(`Removed a file '${targetLockContent.path}'.`),
);
} catch {
console.log(
Colors.red(`Failed to remove a file '${targetContent.path}'.`),
Colors.red(`Failed to remove a file '${targetLockContent.path}'.`),
);
}
// TODO: Remove an empty direcotory
Expand Down Expand Up @@ -363,3 +364,37 @@ export class SearchAction {
);
}
}

export class VerifyAction {
async execute() {
const targetLockContents = new DimLockFileAccessor().getContents();
const targetContents = new DimLockFileAccessor().getContents();
let result = true;

if (targetContents.length !== targetLockContents.length) {
console.error(
Colors.red(`the numbers of dim.json and dim-lock.json don't match`),
);
Deno.exit(1);
}

for (const targetLockContent of targetLockContents) {
await ky.get(targetLockContent.url, targetLockContent.headers)
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => {
const integrity = new Sha1().update(arrayBuffer).toString();
if (integrity !== targetLockContent.integrity) {
result = false;
console.log(
Colors.red(`${targetLockContent.name} : verification failed`),
);
}
});
}
if (result) {
console.log(
Colors.green(`verification success`),
);
}
}
}

0 comments on commit 6eb66a0

Please sign in to comment.