Skip to content

Commit a14a21e

Browse files
author
Frank Schmid
committed
Added models show command
1 parent 244519f commit a14a21e

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,8 @@ The plugin also adds some new commands to Serverless: `sls models XXXXXX`
105105
#### list
106106
Lists the defined model names
107107

108+
#### show
109+
Shows specified model definitions as JSON or YAML.
110+
Usage: `sls models show <model names> [--format json|yaml]`
111+
108112
#### more to come

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports = function(S) {
2121
ModelsCreate = new ModelsCreate();
2222
let ModelsList = require('./lib/actions/ModelsList')(S);
2323
ModelsList = new ModelsList();
24+
let ModelsShow = require('./lib/actions/ModelsShow')(S);
25+
ModelsShow = new ModelsShow();
2426
let ModelsRemove = require('./lib/actions/ModelsRemove')(S);
2527
ModelsRemove = new ModelsRemove();
2628
let ModelsImport = require('./lib/actions/ModelsImport')(S);
@@ -62,6 +64,7 @@ module.exports = function(S) {
6264
return BbPromise.join(
6365
ModelsCreate.registerActions(),
6466
ModelsList.registerActions(),
67+
ModelsShow.registerActions(),
6568
ModelsImport.registerActions(),
6669
ModelsRemove.registerActions()
6770
);

lib/Models.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module.exports = function(S) {
9191
return BbPromise.resolve(models[0]);
9292
}
9393

94-
return BbPromise.resolve([]);
94+
return BbPromise.resolve({});
9595
});
9696
}
9797

lib/actions/ModelsShow.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
'use strict';
2+
/**
3+
* List APIG models
4+
*/
5+
6+
module.exports = function(S) {
7+
8+
const path = require('path'),
9+
SUtils = S.utils,
10+
SError = require(S.getServerlessPath('Error')),
11+
SCli = require(S.getServerlessPath('utils/cli')),
12+
BbPromise = require('bluebird'),
13+
_ = require('lodash'),
14+
yaml = require('js-yaml');
15+
16+
class ModelsShow extends S.classes.Plugin {
17+
18+
static getName() {
19+
return 'serverless.core.' + this.name;
20+
}
21+
22+
registerActions() {
23+
S.addAction(this.modelsShow.bind(this), {
24+
handler: 'modelsShow',
25+
description: `Show definition of an APIG model.
26+
usage: serverless models show <model name>`,
27+
context: 'models',
28+
contextAction: 'show',
29+
options: [
30+
{
31+
option: 'format',
32+
shortcut: 'f',
33+
description: 'Output format used - json or yaml. Defaults to json'
34+
}
35+
],
36+
parameters: [
37+
{
38+
parameter: 'names',
39+
description: 'One or multiple model names',
40+
position: '0->'
41+
}
42+
]
43+
});
44+
45+
return BbPromise.resolve();
46+
}
47+
48+
modelsShow(evt) {
49+
let _this = this;
50+
_this.evt = evt;
51+
_this.project = S.getProject();
52+
53+
return _this._prompt()
54+
.bind(_this)
55+
.then(_this._validateAndPrepare)
56+
.then(_this._showModel)
57+
.then(function() {
58+
return _this.evt;
59+
});
60+
}
61+
62+
_prompt() {
63+
return BbPromise.resolve();
64+
}
65+
66+
_validateAndPrepare() {
67+
let _this = this;
68+
69+
// Validate output format
70+
if (!_this.evt.options.format) {
71+
_this.evt.options.format = 'json';
72+
}
73+
if (!_this.evt.options.names) {
74+
return BbPromise.reject(new SError('No model(s) specified.'));
75+
}
76+
77+
switch (_.toLower(_this.evt.options.format)) {
78+
case 'json':
79+
_this._formatter = JSON.stringify;
80+
break;
81+
case 'yaml':
82+
_this._formatter = _this._yamlStringify;
83+
break;
84+
default:
85+
return BbPromise.reject(new SError(`Unknown output format: ${_this.evt.options.format}`));
86+
}
87+
88+
return S.classes.Models.loadFromProject(_this.project)
89+
.then(function (models) {
90+
// Validate requested models
91+
let modelNames = _.keys(models);
92+
if (_.size(models) === 0 || !_this.evt.options.names.every(function (modelName) {
93+
return _.indexOf(modelNames, modelName) !== -1;
94+
})) {
95+
return BbPromise.reject(new SError(`Unknown model specified`));
96+
}
97+
98+
return BbPromise.resolve(models);
99+
});
100+
}
101+
102+
_showModel(models) {
103+
let _this = this;
104+
105+
let count = _this.evt.options.names.length;
106+
for (let n=0; n<count; n++) {
107+
let modelName = _this.evt.options.names[n];
108+
let output = {};
109+
output[modelName] = models[modelName];
110+
SCli.log('\n' + _this._formatter(output, null, 2));
111+
}
112+
113+
return BbPromise.resolve();
114+
}
115+
116+
_yamlStringify(obj, parser, indent) {
117+
return yaml.safeDump(obj);
118+
}
119+
120+
}
121+
122+
return ModelsShow;
123+
};

0 commit comments

Comments
 (0)