diff --git a/cli.ts b/cli.ts new file mode 100644 index 0000000..663438b --- /dev/null +++ b/cli.ts @@ -0,0 +1,37 @@ +#!/usr/bin/env node + +import { open } from 'node:fs/promises'; +import { DataSource } from './src/data-source.js'; +import { ElfFile } from './src/elf.js'; + +const filePath = process.argv[2]; +if (!filePath) { + console.error('Usage: elfy '); + process.exit(1); +} + +const fileHandle = await open(filePath, 'r'); +try { + const dataSource: DataSource = { + async read(offset: number, length: number): Promise { + const buffer = new Uint8Array(length); + const { bytesRead } = await fileHandle.read(buffer, 0, length, offset); + return buffer.slice(0, bytesRead); + }, + }; + + const elf = new ElfFile(dataSource); + const { success, section } = await elf.tryReadSection('.note.gnu.build-id'); + if (!success || !section) { + console.error('Could not find .note.gnu.build-id section'); + process.exit(1); + } + + const uuid = Array.from(section.slice(16, 36)) + .map(b => b.toString(16).padStart(2, '0')) + .join(''); + + console.log(uuid); +} finally { + await fileHandle.close(); +} diff --git a/package.json b/package.json index ec24e02..9138fc2 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,9 @@ ], "author": "@bobbyg603", "license": "MIT", + "bin": { + "elfy": "dist/cli.js" + }, "files": [ "dist" ], diff --git a/tsconfig.json b/tsconfig.json index 57c1906..3c18c75 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,6 @@ "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, - "include": ["index.ts", "src/**/*.ts"], + "include": ["index.ts", "cli.ts", "src/**/*.ts"], "exclude": ["node_modules", "dist", "spec"] }