Skip to content

Commit

Permalink
Merged in MOPLAT-168-cname-support (pull request #15)
Browse files Browse the repository at this point in the history
build: add cname support

Approved-by: Deepak Thukral <iapain@yahoo.com>
  • Loading branch information
Rafał Wrzochol authored and iapain committed Dec 4, 2019
2 parents 46141f3 + 67308c9 commit 2f8cac1
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/@mo-platform/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ API Spec
6. version
7. help
8. login
9. cname

`login`

Expand Down Expand Up @@ -72,3 +73,11 @@ Login into MoJS SDK
```
moapp login
```

`cname`

Manage CNAME

```
moapp cname <list|create|delete>
```
13 changes: 13 additions & 0 deletions packages/@mo-platform/cli/config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ nock('http://localhost:3000')
.reply(200, [{"_id":"55fbf2fc0b74415f0cf8e9b1","_vs":"000000001","name":"matt","version":"0.0.1","data":{"name":"matt","version":"0.0.1","description":"","main":"index.html","module":"globals","license":"MIT","ignore":["**/.*","node_modules","bower_components","test","tests"]},"__v":0,"url":"https://matt.app.fagbokforlaget.no","created_at":"2015-09-18T11:18:20.401Z","previousVersions":[]}])


nock('http://localhost:3000')
.get('/api/cnames/test-app')
.reply(200,[{"cnames":["test-app.com"]}])


nock('http://localhost:3000')
.post('/api/cnames/test-app')
.reply(200,{status: "nginx conf file and ssl cert created"})

nock('http://localhost:3000')
.delete('/api/cnames/test-app')
.reply(200,{status: "file deleted"})


module.exports = {
moServer: {
Expand Down
4 changes: 4 additions & 0 deletions packages/@mo-platform/cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ exports.run = function() {
return tasks.migrate(options);
}

if (options.argv.remain.length && options.argv.remain[0] === "cname") {
return tasks.cname(options);
}

console.error('Invalid command: '+ options.argv.remain[0]);
return tasks.help();
}
60 changes: 60 additions & 0 deletions packages/@mo-platform/cli/lib/helpers/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,63 @@ exports.search = (params, options) => {
})
})
}

exports.cnameList = (name, options) => {
if(options == undefined) options = {}
const moServer = config.moServer[options.env || 'dev']

return moConfigFile.read(options).then((authData) => {
return new Promise((resolve, reject) => {
request.get(`${moServer}/api/cnames/${name}`)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.send({token: authData.token})
.then(res => {
return resolve(res.body)
})
.catch(err => {
return reject(`${err} (${err.status})`)
})
})
})
}

exports.cnameCreate = (name, cname, options) => {
if(options == undefined) options = {}
const moServer = config.moServer[options.env || 'dev']

return moConfigFile.read(options).then((authData) => {
return new Promise((resolve, reject) => {
request.post(`${moServer}/api/cnames/${name}`)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.send({"cname": cname, token: authData.token})
.then(res => {
return resolve(res.body)
})
.catch(err => {
return reject(`${err} (${err.status})`)
})
})
})
}

exports.cnameDelete = (name, cname, options) => {
if(options == undefined) options = {}
const moServer = config.moServer[options.env || 'dev']

return moConfigFile.read(options).then((authData) => {
return new Promise((resolve, reject) => {
request.delete(`${moServer}/api/cnames/${name}`)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.send({"cname": cname, token: authData.token})
.then(res => {
return resolve(res.body)
})
.catch(err => {
return reject(`${err} (${err.status})`)
})
})
})
}
30 changes: 30 additions & 0 deletions packages/@mo-platform/cli/lib/tasks/cname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const fs = require('fs-extra')
const requests = require('../helpers/requests')
const path = require('path')
const chalk = require('chalk')


module.exports = async (options) => {
const packageFile = path.resolve(options.file || 'mo-app.json')
const json = await fs.readJSON(packageFile)
const appName = options.name || json.name
const cmd = options.argv.remain[1]
const cname = options.argv.remain[2]
let response = {}
switch(cmd) {
case 'list':
response = await requests.cnameList(appName, options)
break;
case 'create':
response = await requests.cnameCreate(appName, cname, options)
break;
case 'delete':
response = await requests.cnameDelete(appName, cname, options)
break;
default:
response = chalk.red(`Invalid command '${cmd}'`)
}
console.log(response)
}
4 changes: 4 additions & 0 deletions packages/@mo-platform/cli/lib/tasks/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function() {
{green deploy} Deploys app to mo cloud
{green delete} Deletes app from mo cloud
{green rollback} Rollbacks to a specific version on mo cloud
{green cname [list|create|delete]} CNAME management
{bold Advance options}
--name app name, it will override name from package.json
Expand All @@ -28,5 +29,8 @@ module.exports = function() {
moapp deploy --name myapp
moapp delete --name myapp --force
moapp rollback 0.0.1 --env=dev
moapp cname list --name myapp
moapp cname create myapp.com --name myapp
moapp cname delete myapp.com --name myapp
`)
}
1 change: 1 addition & 0 deletions packages/@mo-platform/cli/lib/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ exports.search = require('./search');
exports.login = require('./login');
exports.rollback = require('./rollback');
exports.migrate = require('./migrate');
exports.cname = require('./cname');
46 changes: 46 additions & 0 deletions packages/@mo-platform/cli/test/cname_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const expect = require('chai').expect;
const nixt = require('nixt'),
childProcess = require('child_process');

describe('MoApp', function() {
describe('cname', function () {

describe('list', function() {
it('should list cnames for given app', function (done) {
nixt()
.run('moapp cname list --name test-app --file=test/fixtures/app/test-package.json --configFile=test/fixtures/tmp_config_file.json')
.stdout(/cnames/gi)
.end(done)
})
})

describe('create', function() {
it('should create cname for given app', function (done) {
nixt()
.run('moapp cname create test-app.com --name test-app --file=test/fixtures/app/test-package.json --configFile=test/fixtures/tmp_config_file.json')
.stdout(/created/gi)
.end(done)
})
})

describe('delete', function() {
it('should delete cname for given app', function (done) {
nixt()
.run('moapp cname delete test-app.com --name test-app --file=test/fixtures/app/test-package.json --configFile=test/fixtures/tmp_config_file.json')
.stdout(/deleted/gi)
.end(done)
})
})

describe('invalid', function() {
it('should raise error', function (done) {
nixt()
.run('moapp cname invalid test-app.com --name test-app --file=test/fixtures/app/test-package.json --configFile=test/fixtures/tmp_config_file.json')
.stdout(/invalid/gi)
.end(done)
})
})

});
});

0 comments on commit 2f8cac1

Please sign in to comment.