Skip to content

Commit

Permalink
fix: correctly handle file reading and updates
Browse files Browse the repository at this point in the history
This commit refactors the handling of file read/write operations
to use synchronous methods for simplicity.
  • Loading branch information
therealdwright committed Jan 29, 2024
1 parent d5fb3f1 commit c7ab52c
Show file tree
Hide file tree
Showing 40 changed files with 5,381 additions and 1,886 deletions.
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const axios = require("axios");
const core = require("@actions/core");
const exec = require("@actions/exec");
const fs = require("fs").promises;
const fs = require("fs");
const tar = require("tar");
const { Octokit } = require("@octokit/rest");

Expand All @@ -24,9 +24,17 @@ async function downloadFile(url, outputPath) {
async function checkForSecrets() {
let secretsDetected = false;

const data = await fs.readFile(secretsFilePath, "utf8");
if (!data || data.trim().length === 0) {
console.log("No data or empty file found, skipping processing...");
let data;
try {
data = fs.readFileSync(secretsFilePath, "utf8");
} catch (err) {
console.error(`Error reading file: ${err}`);
throw err;
}

// If the file is empty or only contains an empty array, assume no secrets found
if (!data || data.trim() === "[]") {
console.log("No secrets found, skipping processing...");
return secretsDetected;
}

Expand Down Expand Up @@ -95,6 +103,11 @@ function getRepoData(repoUrl) {
}

async function run() {
// Ensure secrets.json exists at the start
if (!fs.existsSync(secretsFilePath)) {
fs.writeFileSync(secretsFilePath, "[]"); // Create an empty JSON array
}

try {
const tarballPath = "./trufflehog.tar.gz";
await downloadFile(
Expand All @@ -103,7 +116,6 @@ async function run() {
);
await tar.x({ file: tarballPath });

let output = "";
const options = {
listeners: {
stdout: (data) => {
Expand Down Expand Up @@ -132,9 +144,9 @@ async function run() {
],
options
);
fs.writeFileSync(secretsFilePath, output);
} catch (error) {
console.error(`Error executing trufflehog: ${error}`);
throw error;
}

const secretsFound = await checkForSecrets();
Expand Down
Loading

0 comments on commit c7ab52c

Please sign in to comment.