Skip to content

Commit

Permalink
Merge pull request #16 from Megapixel99/add-backups
Browse files Browse the repository at this point in the history
Add backups
  • Loading branch information
Megapixel99 authored Apr 6, 2020
2 parents 3c82c8a + ca47b15 commit 3cbbd3f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ manager such as [n](https://www.npmjs.com/package/n) or [nvm](https://github.com

##### To Do:
- Modify build script for Windows and Linux
- Add CLI option to view current alias
- Add CLI option to create backups of SSH keys (In progress)
- Fix issue in the use of `sudo` in the build script with zshell.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ To change the current alias:
```bash
$ gam change-alias
```
To view the email of the current alias:
```bash
$ gam current-alias-email
```
To delete a public and private key for an alias
```bash
$ gam delete-alias
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "GAM is a NodeJS application for managing multiple Git accounts (aliases).",
"main": "index.js",
"scripts": {
"prestart": "./node_modules/.bin/eslint . --fix",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./src/manager.js"
},
Expand Down
7 changes: 2 additions & 5 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ if (args[0] === 'create-alias') {
+ ' ' + 'Local:' + '\n'
+ ' ' + 'Email:' + ` ${emails.localEmail}`);
} else if (args[0] === 'backup') {
methods.backup(dir).then((res) => {
console.log(res);
}).catch((err) => {
throw (err);
});
methods.backup(dir);
} else if (args[0] === 'h' || args[0] === 'help' || args.length === 0) {
console.log('Usage: gam [command] [options]' + '\n\n'
+ 'Available Commands:' + '\n'
+ ' ' + 'backup: ' + ' ' + 'Creates a backup of all of the public and private keys' + '\n'
+ ' ' + 'create-alias: ' + ' ' + 'Creates a public and private key for a new alias' + '\n'
+ ' ' + 'change-alias: ' + ' ' + 'Changes the current public and private key to the specified alias' + '\n'
+ ' ' + 'current-alias-email:' + ' ' + 'Retrives the email and name assosiated with the current alias' + '\n'
Expand Down
45 changes: 45 additions & 0 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,50 @@ function getFormattedDate(date) {
return `${month}-${day}-${year}`;
}

function copyFileSync(source, target) {
let targetFile = target;

if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}

fs.writeFileSync(targetFile, fs.readFileSync(source));
}

function copyFolderRecursiveSync(source, target) {
let files = [];

const targetFolder = target;
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}

if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach((file) => {
const curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory() && file.match(/backup-\d{2}-\d{2}-\d{4}/g) !== null) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}

function backup(dir) {
const folderName = `backup-${getFormattedDate(new Date())}`;
try {
copyFolderRecursiveSync(dir, path.join(dir, `/${folderName}`));
console.log(`Created Backup folder named: ${folderName}` + ' successfully.');
} catch (e) {
console.log('An error occured when creating the backup folder');
throw e;
}
}

function chooseAlias(dir, customStr) {
const aliasList = [];
fs.readdirSync(dir).forEach((file) => {
Expand Down Expand Up @@ -278,6 +322,7 @@ function deleteAlias(alias, dir) {
}

module.exports = {
backup,
chooseAlias,
createAlias,
changeAlias,
Expand Down

0 comments on commit 3cbbd3f

Please sign in to comment.