Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix post read #41

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 76 additions & 56 deletions src/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,49 +95,54 @@ async function addToSummaryWithSizeCheck(
}
}

function stopKnoxctlScan(): void {
async function stopKnoxctlScan(): Promise<void> {
const pidFile = getPidFilePath();
if (fs.existsSync(pidFile)) {
const pid = fs.readFileSync(pidFile, ENCODING).trim();
log(`Attempting to stop knoxctl scan process with PID: ${pid}`);
try {
process.kill(Number(pid), "SIGINT");
await exec.exec("sudo", ["kill", "-SIGINT", pid]);
log("Sent SIGINT signal to knoxctl scan process");
setTimeout(async () => {
try {
process.kill(Number(pid), 0);
log("Process is still running. Attempting to force kill...");
process.kill(Number(pid), "SIGKILL");
} catch (error) {
log("knoxctl scan process has been terminated");
}
fs.unlinkSync(pidFile);
log("Removed PID file");

// Change permissions of output files
const outputDir = getOutputDir();
log(`Attempting to change permissions of files in: ${outputDir}`);
try {
await exec.exec("sudo", ["chmod", "-R", "644", outputDir]);
log("Changed permissions of output files");

// List directory contents to verify
const { stdout, stderr } = await exec.getExecOutput("ls", [
"-l",
outputDir,
]);
log("Directory contents after permission change:");
log(stdout);
if (stderr) {
log(`stderr: ${stderr}`, "warning");
}
} catch (error) {
log(
`Failed to change permissions of output files: ${error instanceof Error ? error.message : String(error)}`,
"error",
);
await new Promise((resolve) => setTimeout(resolve, 5000));
try {
await exec.exec("sudo", ["kill", "-0", pid]);
log("Process is still running. Attempting to force kill...");
await exec.exec("sudo", ["kill", "-SIGKILL", pid]);
} catch (error) {
log("knoxctl scan process has been terminated");
}
await exec.exec("sudo", ["rm", pidFile]);
log("Removed PID file");

// Change permissions of output files
const outputDir = getOutputDir();
log(`Attempting to change permissions of files in: ${outputDir}`);
try {
await exec.exec("sudo", ["chmod", "-R", "644", outputDir]);
await exec.exec("sudo", [
"chown",
"-R",
`${process.env.USER}:${process.env.USER}`,
outputDir,
]);
log("Changed permissions and ownership of output files");

// List directory contents to verify
const { stdout, stderr } = await exec.getExecOutput("ls", [
"-l",
outputDir,
]);
log("Directory contents after permission change:");
log(stdout);
if (stderr) {
log(`stderr: ${stderr}`, "warning");
}
}, 5000);
} catch (error) {
log(
`Failed to change permissions of output files: ${error instanceof Error ? error.message : String(error)}`,
"error",
);
}
} catch (error) {
log(
`Failed to stop knoxctl scan process: ${error instanceof Error ? error.message : String(error)}`,
Expand All @@ -151,27 +156,32 @@ function stopKnoxctlScan(): void {
}
}

function getLatestFile(directory: string, prefix: string): string | null {
async function getLatestFile(
directory: string,
prefix: string,
): Promise<string | null> {
log(`Searching for files with prefix "${prefix}" in directory: ${directory}`);

if (!fs.existsSync(directory)) {
log(`Directory does not exist: ${directory}`, "error");
try {
const { stdout } = await exec.getExecOutput("sudo", [
"ls",
"-t",
directory,
]);
const files = stdout.split("\n").filter(Boolean);
log(`Files in directory: ${files.join(", ")}`);

const matchingFile = files.find(
(file) => file.startsWith(prefix) && file.endsWith(".md"),
);
return matchingFile || null;
} catch (error) {
log(
`Error listing directory contents: ${error instanceof Error ? error.message : String(error)}`,
"error",
);
return null;
}

const files = fs.readdirSync(directory);
log(`Files in directory: ${files.join(", ")}`);

const matchingFiles = files
.filter((file) => file.startsWith(prefix) && file.endsWith(".md"))
.map((file) => ({
name: file,
time: fs.statSync(path.join(directory, file)).mtime.getTime(),
}))
.sort((a, b) => b.time - a.time);

log(`Matching files: ${matchingFiles.map((f) => f.name).join(", ")}`);
return matchingFiles.length > 0 ? matchingFiles[0].name : null;
}

async function processResultFile(
Expand All @@ -180,12 +190,22 @@ async function processResultFile(
title: string,
emoji: string,
): Promise<void> {
const file = getLatestFile(outputDir, prefix);
const file = await getLatestFile(outputDir, prefix);
if (file) {
const filePath = path.join(outputDir, file);
log(`Processing ${title} file: ${filePath}`);
const content = fs.readFileSync(filePath, ENCODING);
await addToSummaryWithSizeCheck(`${emoji} ${title}\n\n${content}`, title);
try {
const { stdout: content } = await exec.getExecOutput("sudo", [
"cat",
filePath,
]);
await addToSummaryWithSizeCheck(`${emoji} ${title}\n\n${content}`, title);
} catch (error) {
log(
`Error reading file ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
"error",
);
}
} else {
log(
`No ${title.toLowerCase()} file found with prefix ${prefix} in ${outputDir}`,
Expand Down
Loading