dshackle cli tool
This commit is contained in:
57
dshackle-cli/src/cli.js
Normal file
57
dshackle-cli/src/cli.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import {describe} from "./grpc-clent";
|
||||
import arg from 'arg';
|
||||
import clc from "cli-color";
|
||||
import util from "util";
|
||||
|
||||
export function cli(args) {
|
||||
let opts = parseArgumentsIntoOptions(args);
|
||||
if (!opts.url) {
|
||||
console.log("Err: URL not specified!")
|
||||
return
|
||||
}
|
||||
describe(opts.url, (error, response) => {
|
||||
if (error) {
|
||||
console.error(clc.red('Connection to ' + opts.url + ' failed! [' + error.message + ']'));
|
||||
} else {
|
||||
console.error(clc.green('Connected to ', opts.url));
|
||||
if (opts.print) {
|
||||
console.log(util.inspect(response, false, null, true /* enable colors */));
|
||||
} else {
|
||||
response.chains.forEach(function (item) {
|
||||
var state = item.status.availability;
|
||||
|
||||
switch (state) {
|
||||
case 'AVAIL_OK':
|
||||
state = clc.green(state);
|
||||
break
|
||||
case 'AVAIL_UNKNOWN':
|
||||
case 'AVAIL_UNAVAILABLE':
|
||||
state = clc.red(state);
|
||||
break
|
||||
default:
|
||||
state = clc.yellow(state);
|
||||
}
|
||||
console.log(item.status.chain + ' -> ' + clc.bold(state))
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function parseArgumentsIntoOptions(rawArgs) {
|
||||
const args = arg(
|
||||
{
|
||||
'--print': Boolean,
|
||||
'-p': '--print'
|
||||
},
|
||||
{
|
||||
argv: rawArgs.slice(2),
|
||||
}
|
||||
);
|
||||
return {
|
||||
print: args['--print'] || false,
|
||||
url: args._[0]
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
25
dshackle-cli/src/grpc-clent.js
Normal file
25
dshackle-cli/src/grpc-clent.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const grpc = require("@grpc/grpc-js");
|
||||
const path = require('path')
|
||||
const protoLoader = require("@grpc/proto-loader");
|
||||
|
||||
const PROTO_PATH = path.join(__dirname, "../proto/blockchain.proto");
|
||||
|
||||
const options = {
|
||||
keepCase: true,
|
||||
longs: String,
|
||||
enums: String,
|
||||
defaults: true,
|
||||
oneofs: true,
|
||||
};
|
||||
|
||||
const packageDefinition = protoLoader.loadSync(PROTO_PATH, options);
|
||||
const emerald = grpc.loadPackageDefinition(packageDefinition).emerald
|
||||
|
||||
export function describe(url, handler) {
|
||||
const client = new emerald.Blockchain(
|
||||
url,
|
||||
grpc.credentials.createInsecure()
|
||||
);
|
||||
console.log('Connecting to: ' + url + '...')
|
||||
client.Describe({}, handler);
|
||||
}
|
||||
Reference in New Issue
Block a user