Skip to content

Commit

Permalink
Add --overwrite flag to staticrypt
Browse files Browse the repository at this point in the history
Fixes robinmoisson#196

Add support for the `--overwrite` flag to overwrite files in-place.

* **cli/helpers.js**
  - Add `overwrite` parameter to `getFullOutputPath` and `recursivelyApplyCallbackToHtmlFiles` functions.
  - Update `parseCommandLineArguments` to include the `--overwrite` option.

* **cli/index.js**
  - Update `runStatiCrypt` function to handle the `--overwrite` flag.
  - Update `encodeAndGenerateFile` function to handle the `--overwrite` flag.

* **README.md**
  - Add documentation for the `--overwrite` flag in the CLI section.
  - Add an example of using the `--overwrite` flag.
  • Loading branch information
vuyp committed Sep 3, 2024
1 parent 5f362e3 commit 56c4d9c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ staticrypt dir_to_encrypt/* -r
staticrypt dir_to_encrypt/* -r -d dir_to_encrypt
```

#### Overwrite files in-place

```bash
# Use the --overwrite flag to overwrite the original files in-place
staticrypt dir_to_encrypt/* -r --overwrite
```

#### Get a shareable auto-decrypt link

The link contains the hashed password, that will auto-decrypt the file - you can include your file URL or leave blank. (⚠️ you should keep your `.staticrypt.json` so the salt is the same each time you encrypt, or re-encrypting will [invalidate the link](#why-does-staticrypt-create-a-config-file)):
Expand Down Expand Up @@ -153,6 +160,8 @@ The password argument is optional if `STATICRYPT_PASSWORD` is set in the environ
[string] [default: "encrypted"]
--decrypt Include this flag to decrypt files instead of
encrypt. [boolean] [default: false]
--overwrite Overwrite the original files in-place.
[boolean] [default: false]
-p, --password The password to encrypt your file with. Leave
empty to be prompted for it. If
STATICRYPT_PASSWORD is set in the env, we'll
Expand Down
18 changes: 14 additions & 4 deletions cli/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ exports.genFile = genFile;
* @param {string} path
* @param {string} fullRootDirectory
* @param {string} outputDirectory
* @param {boolean} overwrite
* @returns {string}
*/
function getFullOutputPath(path, fullRootDirectory, outputDirectory) {
function getFullOutputPath(path, fullRootDirectory, outputDirectory, overwrite) {
if (overwrite) {
return path;
}
const relativePath = pathModule.relative(fullRootDirectory, path);
return outputDirectory + "/" + relativePath;
}
Expand Down Expand Up @@ -339,17 +343,18 @@ exports.isCustomPasswordTemplateDefault = isCustomPasswordTemplateDefault;
* @param {string} path
* @param {string} outputDirectory
* @param {string} rootDirectory
* @param {boolean} overwrite
* @param {(fullPath: string, rootDirectoryFromArgument: string) => void} callback
*/
function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, rootDirectory = "") {
function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, rootDirectory = "", overwrite = false) {
const fullPath = pathModule.resolve(path);
const fullRootDirectory = rootDirectory || pathModule.dirname(fullPath);

if (fs.statSync(fullPath).isDirectory()) {
fs.readdirSync(fullPath).forEach((filePath) => {
const fullFilePath = `${fullPath}/${filePath}`;

recursivelyApplyCallbackToHtmlFiles(callback, fullFilePath, outputDirectory, fullRootDirectory);
recursivelyApplyCallbackToHtmlFiles(callback, fullFilePath, outputDirectory, fullRootDirectory, overwrite);
});
return;
}
Expand All @@ -360,7 +365,7 @@ function recursivelyApplyCallbackToHtmlFiles(callback, path, outputDirectory, ro
}
// else just copy the file as is
else {
const fullOutputPath = getFullOutputPath(fullPath, fullRootDirectory, outputDirectory);
const fullOutputPath = getFullOutputPath(fullPath, fullRootDirectory, outputDirectory, overwrite);
copyFile(fullPath, fullOutputPath);
}
}
Expand Down Expand Up @@ -388,6 +393,11 @@ function parseCommandLineArguments() {
describe: "Include this flag to decrypt files instead of encrypt.",
default: false,
})
.option("overwrite", {
type: "boolean",
describe: "Overwrite the original files in-place.",
default: false,
})
.option("p", {
alias: "password",
type: "string",
Expand Down
9 changes: 5 additions & 4 deletions cli/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ async function runStatiCrypt() {
);
},
path,
namedArgs.directory
namedArgs.directory,
"",
namedArgs.overwrite
);
});
}
Expand Down Expand Up @@ -223,10 +225,9 @@ async function encodeAndGenerateFile(
};

// remove the base path so that the actual output path is relative to the base path
const relativePath = pathModule.relative(rootDirectoryFromArguments, path);
const outputFilepath = namedArgs.directory + "/" + relativePath;
const outputFilepath = getFullOutputPath(path, rootDirectoryFromArguments, namedArgs.directory, namedArgs.overwrite);

genFile(templateData, outputFilepath, namedArgs.template);
}

runStatiCrypt();
runStatiCrypt();

0 comments on commit 56c4d9c

Please sign in to comment.