clistyle: 'triton network ...'

This commit is contained in:
Trent Mick 2016-01-04 21:53:52 -08:00
parent d2c20a5161
commit 13b525cca7
7 changed files with 202 additions and 110 deletions

View File

@ -2,6 +2,8 @@
## 4.0.0 (not yet released) ## 4.0.0 (not yet released)
- XXX changes in `triton instance,key,network`
- Add the ability to create a profile copying from an existing profile, - Add the ability to create a profile copying from an existing profile,
via `triton profile create --copy NAME`. via `triton profile create --copy NAME`.

View File

@ -5,18 +5,18 @@
*/ */
/* /*
* Copyright 2015 Joyent, Inc. * Copyright 2016 Joyent, Inc.
* *
* `triton network ...` * `triton network get ...`
*/ */
var format = require('util').format; var format = require('util').format;
var common = require('./common'); var common = require('../common');
var errors = require('./errors'); var errors = require('../errors');
function do_network(subcmd, opts, args, cb) { function do_get(subcmd, opts, args, cb) {
if (opts.help) { if (opts.help) {
this.do_help('help', {}, [subcmd], cb); this.do_help('help', {}, [subcmd], cb);
return; return;
@ -25,7 +25,7 @@ function do_network(subcmd, opts, args, cb) {
'incorrect number of args (%d)', args.length))); 'incorrect number of args (%d)', args.length)));
} }
this.tritonapi.getNetwork(args[0], function (err, net) { this.top.tritonapi.getNetwork(args[0], function (err, net) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
@ -39,7 +39,7 @@ function do_network(subcmd, opts, args, cb) {
}); });
} }
do_network.options = [ do_get.options = [
{ {
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
@ -51,13 +51,13 @@ do_network.options = [
help: 'JSON output.' help: 'JSON output.'
} }
]; ];
do_network.help = ( do_get.help = (
'Show a network.\n' 'Show a network.\n'
+ '\n' + '\n'
+ 'Usage:\n' + 'Usage:\n'
+ ' {{name}} network <id>\n' + ' {{name}} get <id|name>\n'
+ '\n' + '\n'
+ '{{options}}' + '{{options}}'
); );
module.exports = do_network; module.exports = do_get;

103
lib/do_network/do_list.js Normal file
View File

@ -0,0 +1,103 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2016 Joyent, Inc.
*
* `triton network list ...`
*/
var tabula = require('tabula');
var common = require('../common');
// to be passed as query string args to /my/networks
var validFilters = [
'id',
'name',
'public',
'description'
];
// columns default without -o
var columnsDefault = 'shortid,name,subnet,gateway,fabric,vlan,public';
// columns default with -l
var columnsDefaultLong = 'id,name,subnet,gateway,fabric,vlan,public';
// sort default with -s
var sortDefault = 'name';
function do_list(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
}
var columns = columnsDefault;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = columnsDefaultLong;
}
columns = columns.split(',');
var sort = opts.s.split(',');
this.top.tritonapi.cloudapi.listNetworks(function (err, networks) {
if (err) {
callback(err);
return;
}
if (opts.json) {
common.jsonStream(networks);
} else {
for (var i = 0; i < networks.length; i++) {
var net = networks[i];
net.shortid = net.id.split('-', 1)[0];
net.vlan = net.vlan_id;
}
tabula(networks, {
skipHeader: opts.H,
columns: columns,
sort: sort
});
}
callback();
});
}
do_list.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: sortDefault
}));
do_list.help = [
'List available networks.',
'',
'Usage:',
' {{name}} list',
'',
'Fields (most are self explanatory, the client adds some for convenience):',
' vlan A shorter alias for "vlan_id".',
' shortid A short ID prefix.',
'',
'{{options}}'
].join('\n');
do_list.aliases = ['ls'];
module.exports = do_list;

50
lib/do_network/index.js Normal file
View File

@ -0,0 +1,50 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2016 Joyent, Inc.
*
* `triton network ...`
*/
var Cmdln = require('cmdln').Cmdln;
var util = require('util');
// ---- CLI class
function NetworkCLI(top) {
this.top = top;
Cmdln.call(this, {
name: top.name + ' network',
/* BEGIN JSSTYLED */
desc: [
'List, get, create and update Triton networks.'
].join('\n'),
/* END JSSTYLED */
helpOpts: {
minHelpCol: 24 /* line up with option help */
},
helpSubcmds: [
'help',
'list',
'get'
]
});
}
util.inherits(NetworkCLI, Cmdln);
NetworkCLI.prototype.init = function init(opts, args, cb) {
this.log = this.top.log;
Cmdln.prototype.init.apply(this, arguments);
};
NetworkCLI.prototype.do_list = require('./do_list');
NetworkCLI.prototype.do_get = require('./do_get');
module.exports = NetworkCLI;

View File

@ -5,97 +5,21 @@
*/ */
/* /*
* Copyright 2015 Joyent, Inc. * Copyright 2016 Joyent, Inc.
* *
* `triton networks ...` * `triton networks ...` bwcompat shortcut for `triton network list ...`.
*/ */
var tabula = require('tabula');
var common = require('./common');
// to be passed as query string args to /my/networks
var validFilters = [
'id',
'name',
'public',
'description'
];
// columns default without -o
var columnsDefault = 'shortid,name,subnet,gateway,fabric,vlan,public';
// columns default with -l
var columnsDefaultLong = 'id,name,subnet,gateway,fabric,vlan,public';
// sort default with -s
var sortDefault = 'name';
function do_networks(subcmd, opts, args, callback) { function do_networks(subcmd, opts, args, callback) {
if (opts.help) { var subcmdArgv = ['node', 'triton', 'network', 'list'].concat(args);
this.do_help('help', {}, [subcmd], callback); this.dispatch('network', subcmdArgv, callback);
return;
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
}
var columns = columnsDefault;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = columnsDefaultLong;
}
columns = columns.split(',');
var sort = opts.s.split(',');
this.tritonapi.cloudapi.listNetworks(function (err, networks) {
if (err) {
callback(err);
return;
}
if (opts.json) {
common.jsonStream(networks);
} else {
for (var i = 0; i < networks.length; i++) {
var net = networks[i];
net.shortid = net.id.split('-', 1)[0];
net.vlan = net.vlan_id;
}
tabula(networks, {
skipHeader: opts.H,
columns: columns,
sort: sort
});
}
callback();
});
} }
do_networks.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: sortDefault
}));
do_networks.help = [ do_networks.help = [
'List available networks.', 'A shortcut for "triton network list".',
'', '',
'Usage:', 'Usage:',
' {{name}} networks', ' {{name}} networks ...'
'',
'Fields (most are self explanatory, the client adds some for convenience):',
' vlan A shorter alias for "vlan_id".',
' shortid A short ID prefix.',
'',
'{{options}}'
].join('\n'); ].join('\n');
module.exports = do_networks; module.exports = do_networks;

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2015, Joyent, Inc. * Copyright (c) 2016, Joyent, Inc.
*/ */
/* /*
@ -27,11 +27,11 @@ var networks;
test('triton networks', function (tt) { test('triton networks', function (tt) {
tt.test(' triton networks -h', function (t) { tt.test(' triton network list -h', function (t) {
h.triton('networks -h', function (err, stdout, stderr) { h.triton('networks -h', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
t.ok(/Usage:\s+triton networks/.test(stdout)); t.ok(/Usage:\s+triton network list/.test(stdout));
t.end(); t.end();
}); });
}); });
@ -55,6 +55,16 @@ test('triton networks', function (tt) {
}); });
}); });
tt.test(' triton network list', function (t) {
h.triton('network list', function (err, stdout, stderr) {
if (h.ifErr(t, err))
return t.end();
t.ok(/^SHORTID\b/.test(stdout));
t.ok(/\bFABRIC\b/.test(stdout));
t.end();
});
});
tt.test(' triton networks -l', function (t) { tt.test(' triton networks -l', function (t) {
h.triton('networks -l', function (err, stdout, stderr) { h.triton('networks -l', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
@ -84,10 +94,10 @@ test('triton networks', function (tt) {
}); });
test('triton network', function (tt) { test('triton network get', function (tt) {
tt.test(' triton network -h', function (t) { tt.test(' triton network get -h', function (t) {
h.triton('network -h', function (err, stdout, stderr) { h.triton('network get -h', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
t.ok(/Usage:\s+triton network\b/.test(stdout)); t.ok(/Usage:\s+triton network\b/.test(stdout));
@ -95,25 +105,26 @@ test('triton network', function (tt) {
}); });
}); });
tt.test(' triton help network', function (t) { tt.test(' triton network help get', function (t) {
h.triton('help network', function (err, stdout, stderr) { h.triton('network help get', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
t.ok(/Usage:\s+triton network\b/.test(stdout)); t.ok(/Usage:\s+triton network get\b/.test(stdout));
t.end(); t.end();
}); });
}); });
tt.test(' triton network', function (t) { tt.test(' triton network get', function (t) {
h.triton('network', function (err, stdout, stderr) { h.triton('network get', function (err, stdout, stderr) {
t.ok(err); t.ok(err);
t.ok(/error \(Usage\)/.test(stderr)); t.ok(/error \(Usage\)/.test(stderr));
t.end(); t.end();
}); });
}); });
tt.test(' triton network ID', function (t) { tt.test(' triton network get ID', function (t) {
h.triton('network ' + networks[0].id, function (err, stdout, stderr) { h.triton('network get ' + networks[0].id,
function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
var network = JSON.parse(stdout); var network = JSON.parse(stdout);
@ -122,9 +133,9 @@ test('triton network', function (tt) {
}); });
}); });
tt.test(' triton network SHORTID', function (t) { tt.test(' triton network get SHORTID', function (t) {
var shortid = networks[0].id.split('-')[0]; var shortid = networks[0].id.split('-')[0];
h.triton('network ' + shortid, function (err, stdout, stderr) { h.triton('network get ' + shortid, function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
var network = JSON.parse(stdout); var network = JSON.parse(stdout);
@ -133,8 +144,9 @@ test('triton network', function (tt) {
}); });
}); });
tt.test(' triton network NAME', function (t) { tt.test(' triton network get NAME', function (t) {
h.triton('network ' + networks[0].name, function (err, stdout, stderr) { h.triton('network get ' + networks[0].name,
function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))
return t.end(); return t.end();
var network = JSON.parse(stdout); var network = JSON.parse(stdout);

View File

@ -42,8 +42,9 @@ var subs = [
['instance wait'], ['instance wait'],
['instance audit'], ['instance audit'],
['ssh'], ['ssh'],
['networks'],
['network'], ['network'],
['network list', 'networks'],
['network get'],
['key'], ['key'],
['key add'], ['key add'],
['key list', 'key ls', 'keys'], ['key list', 'key ls', 'keys'],