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

Add Pretty Table Flag (--pretty, -pr) #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions bin/nc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ program
.option("-h, --id <type>", "Id")
.option("-m, --image <type>,", "Image")
.option("-n, --instance <type>,", "Instance")
.option("-pr, --pretty,", "Display JSON output in a 'pretty' table structure")
.parse(process.argv);

Services(program, spinner);
63 changes: 60 additions & 3 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,58 @@ const fs = require("fs");
const figlet = require("figlet");
const chalk = require("chalk");
const path = "./.nc.config.js";
const Table = require("cli-table");

/**
* Display logger output as table (--pretty)
* @param {*} data
*/
let createTable = data => {
let tableStyle = {
chars: {
top: "═",
"top-mid": "╤",
"top-left": "╔",
"top-right": "╗",
bottom: "═",
"bottom-mid": "╧",
"bottom-left": "╚",
"bottom-right": "╝",
left: "║",
"left-mid": "╟",
mid: "─",
"mid-mid": "┼",
right: "║",
"right-mid": "╢",
middle: "│"
}
};

try {
let table = new Table(tableStyle);
let header = [];
let body = [];

// Creates header
for (column in data) {
header.push(column);
}
table.push(header);

// Creates body
for (const [key, value] of Object.entries(data)) {
body.push(value);
}
table.push(body);

return table.toString();
} catch {
throw new Error(
"This data cannot be displayed in a pretty table format:",
data
);
}
};

/**
* Display text as CLI logo
Expand Down Expand Up @@ -35,11 +87,16 @@ let logofied = text => {
* @param {*} data
* @param {*} error
*/
let logger = (data, error) => {
let logger = (data, error, prettyEnabled) => {
let parsedData = JSON.parse(JSON.stringify(data));
let output = prettyEnabled
? createTable(parsedData)
: JSON.stringify(parsedData);

if (error) {
console.log(chalk.red(`${JSON.stringify(data, null, 2)}`));
console.log(chalk.red(output));
} else {
console.log(chalk.green(`${JSON.stringify(data, null, 2)}`));
console.log(chalk.green(output));
}
};

Expand Down
106 changes: 53 additions & 53 deletions lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
case "list":
compute.listInstances({}, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "stop":
Expand All @@ -67,9 +67,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -80,9 +80,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -93,9 +93,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -106,9 +106,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -129,24 +129,24 @@ function Services(program, spinner) {
ACL: "public-read"
},
(error, data) => {
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
case "list":
storage.listStorage({}, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "upload":
storage.uploadToStorage({}, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "delete":
Expand All @@ -156,9 +156,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -175,33 +175,33 @@ function Services(program, spinner) {
case "create":
database.createNewItem(option, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "query":
database.query(option, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "delete":
database.delete(option, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "update":
database.updateItem(option, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;

Expand All @@ -221,9 +221,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -234,9 +234,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -248,9 +248,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -262,9 +262,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand Down Expand Up @@ -294,18 +294,18 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
case "list":
network.list({}, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "delete":
Expand All @@ -315,9 +315,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -334,9 +334,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -352,9 +352,9 @@ function Services(program, spinner) {
},
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
Expand All @@ -363,37 +363,37 @@ function Services(program, spinner) {
{ CallerReference: program.cr, Name: program.name },
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
case "listz":
network.listZones({}, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
case "deletez":
network.deleteZone(
{ Id: `/hostedzone/${program.id}` },
(error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
}
);
break;
case "record":
network.changeRecordSets({}, (error, data) => {
if (error) {
logger(error, true);
logger(error, true, program.pretty);
}
logger(data, null);
logger(data, null, program.pretty);
});
break;
default:
Expand Down
Loading