Skip to content

Commit

Permalink
add lib/util.js #13
Browse files Browse the repository at this point in the history
  • Loading branch information
katsube committed Mar 22, 2020
1 parent 649b032 commit c13cf0e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 128 deletions.
70 changes: 6 additions & 64 deletions bin/gpwd-h.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//--------------------------------------
const MIN_ITEM = 1;
const MAX_ITEM = 65534;
const CONFIG_FILENAME = '.gpwd.json';
const DEFAULT_OPT = {
length: 8,
item: 1,
Expand All @@ -24,14 +23,13 @@ const DEFAULT_OPT = {
//--------------------------------------
// Module
//--------------------------------------
const fs = require("fs");
const path = require("path");
const program = require("commander");
const util = require("../lib/util.js");
const genPassword = require("../index.js").human;
const passwd = new genPassword();

// Config file
const config = getConfig();
const config = util.getConfig(true);

//--------------------------------------
// commander
Expand Down Expand Up @@ -65,19 +63,19 @@ if( program.secure === undefined ){
//--------------------------------------
// --length (is number)
if( ! Number.isInteger( Number(program.length) ) ){
error("-l, --length option is only integer.");
util.error("-l, --length option is only integer.");
}
// --length (between min to max)
if( ! (genPassword.MIN_LENGTH <= Number(program.length) && Number(program.length) <= genPassword.MAX_LENGTH) ){
error(`-l, --length option is need between ${genPassword.MIN_LENGTH} to ${genPassword.MAX_LENGTH}`);
util.error(`-l, --length option is need between ${genPassword.MIN_LENGTH} to ${genPassword.MAX_LENGTH}`);
}
// --item (is number)
if( ! Number.isInteger( Number(program.item) ) ){
error("-i, --item option is only integer.");
util.error("-i, --item option is only integer.");
}
// --item (between min to max)
if( ! (MIN_ITEM <= Number(program.item) && Number(program.item) <= MAX_ITEM) ){
error(`-i, --item option is need between ${MIN_ITEM} to ${MAX_ITEM}`);
util.error(`-i, --item option is need between ${MIN_ITEM} to ${MAX_ITEM}`);
}

//--------------------------------------
Expand All @@ -94,59 +92,3 @@ for(let i=0; i<program.item; i++){
.gen()
.echo();
}


/**
* Return configuration
*
* @return {Object|boolean}
*/
function getConfig(){
// Get Home directory path
const home = getHomeDirevtory();
if( home === false){
return({});
}

// Get configuration
const file = path.join(home, CONFIG_FILENAME);
if( fs.existsSync(file) ){
try{
const config = require(file);
if( "human" in config ){
return(config.human);
}
}
catch(err){
error(err);
}
}

return({});
}

/**
* Return HOME Directory path
*
* @return {string|boolean}
*/
function getHomeDirevtory(){
let path = process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"];
if( fs.existsSync(path) ){
return(path);
}
else{
return(false);
}
}

/**
* Display Error and exit
*
* @param {string} str error message
* @returns {void}
*/
function error(str){
console.error("[Error] " + str);
process.exit(1);
}
73 changes: 9 additions & 64 deletions bin/gpwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const MIN_ITEM = 1;
const MAX_ITEM = 65534;
const MIN_BASE_LEN = 2;
const MAX_BASE_LEN = 64;
const CONFIG_FILENAME = '.gpwd.json';
const DEFAULT_OPT = {
length: 8,
item: 1,
Expand All @@ -27,14 +26,13 @@ const DEFAULT_OPT = {
//--------------------------------------
// Module
//--------------------------------------
const fs = require("fs");
const path = require("path");
const program = require("commander");
const util = require("../lib/util.js");
const genPassword = require("../index.js");
const passwd = new genPassword();

// Config file
const config = getConfig();
const config = util.getConfig();

//--------------------------------------
// commander
Expand Down Expand Up @@ -72,31 +70,31 @@ if( program.secure === undefined ){
//--------------------------------------
// --length (is number)
if( ! Number.isInteger( Number(program.length) ) ){
error("-l, --length option is only integer.");
util.error("-l, --length option is only integer.");
}
// --length (between min to max)
if( ! (genPassword.MIN_LENGTH <= Number(program.length) && Number(program.length) <= genPassword.MAX_LENGTH) ){
error(`-l, --length option is need between ${genPassword.MIN_LENGTH} to ${genPassword.MAX_LENGTH}`);
util.error(`-l, --length option is need between ${genPassword.MIN_LENGTH} to ${genPassword.MAX_LENGTH}`);
}
// --strength
if( ! passwd.existsStrength(program.strength) ){
error("-s, --strength option is [god|strong|normal|weak] and more.");
util.error("-s, --strength option is [god|strong|normal|weak] and more.");
}
// --item (is number)
if( ! Number.isInteger( Number(program.item) ) ){
error("-i, --item option is only integer.");
util.error("-i, --item option is only integer.");
}
// --item (between min to max)
if( ! (MIN_ITEM <= Number(program.item) && Number(program.item) <= MAX_ITEM) ){
error(`-i, --item option is need between ${MIN_ITEM} to ${MAX_ITEM}`);
util.error(`-i, --item option is need between ${MIN_ITEM} to ${MAX_ITEM}`);
}
// --base (charactor type)
if( (program.base !== undefined) && ( ! program.base.match(/^[a-zA-Z0-9\.\-_\+/!\"#\$%&'\(\)\*,;<=>?@\[\]\^`{\|}~]*$/) )){
error("-b, --base option is only use [a-zA-Z0-9.-_+/!\"#$%&'()*,;<=>?@[]^`{|}~]");
util.error("-b, --base option is only use [a-zA-Z0-9.-_+/!\"#$%&'()*,;<=>?@[]^`{|}~]");
}
// --base (string length)
if( (program.base !== undefined) && !(MIN_BASE_LEN <= program.base.length && program.base.length <= MAX_BASE_LEN) ){
error(`-b, --base option is need between ${MIN_BASE_LEN} to ${MAX_BASE_LEN} string length`);
util.error(`-b, --base option is need between ${MIN_BASE_LEN} to ${MAX_BASE_LEN} string length`);
}

//--------------------------------------
Expand All @@ -114,56 +112,3 @@ for(let i=0; i<program.item; i++){
.gen()
.echo();
}


/**
* Return configuration
*
* @return {Object|boolean}
*/
function getConfig(){
// Get Home directory path
const home = getHomeDirevtory();
if( home === false){
return({});
}

// Get configuration
const file = path.join(home, CONFIG_FILENAME);
if( fs.existsSync(file) ){
try{
return( require(file) );
}
catch(err){
error(err);
}
}

return({});
}

/**
* Return HOME Directory path
*
* @return {string|boolean}
*/
function getHomeDirevtory(){
let path = process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"];
if( fs.existsSync(path) ){
return(path);
}
else{
return(false);
}
}

/**
* Display Error and exit
*
* @param {string} str error message
* @returns {void}
*/
function error(str){
console.error("[Error] " + str);
process.exit(1);
}
77 changes: 77 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//--------------------------------------
// Constant
//--------------------------------------
const CONFIG_FILENAME = '.gpwd.json';

//--------------------------------------
// Module
//--------------------------------------
const fs = require("fs");
const path = require("path");

/**
* Return HOME Directory path
*
* @return {string|boolean}
*/
function getHomeDirectory(){
let path = process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"];
if( fs.existsSync(path) ){
return(path);
}
else{
return(false);
}
}

/**
* Return configuration
*
* @param {boolean} [is_human=false]
* @return {Object}
*/
function getConfig(is_human=false){
// Get Home directory path
const home = getHomeDirectory();
if( home === false){
return({});
}

// Get configuration
const file = path.join(home, CONFIG_FILENAME);
if( fs.existsSync(file) ){
try{
const config = require(file);
if( is_human === false ){
return(config);
}
else if( "human" in config ){
return(config.human);
}
}
catch(err){
error(err);
}
}

return({});
}

/**
* Display Error and exit
*
* @param {string} str error message
* @returns {void}
*/
function error(str){
console.error("[Error] " + str);
process.exit(1);
}


//--------------------------------------
// exports
//--------------------------------------
module.exports.getConfig = getConfig;
module.exports.getHomeDirectory = getHomeDirectory;
module.exports.error = error;

0 comments on commit c13cf0e

Please sign in to comment.