node_terminus
Version:
Pantheon Terminus cli wrapper and extensions
64 lines (62 loc) • 1.99 kB
JavaScript
const axios = require('axios')
const Docker = require('./docker')
const Base = require('./base')
const Multi = require('./multi.js')
class BaseCommand extends Multi.inherit(Base,Docker) {
constructor(name,executionArgs,executionOptions,next) {
super()
this.name = name
this.executionArgs = executionArgs || ''
this.executionOptions = executionOptions || ''
this.executionResults = {}
this.callbackUrl = ''
this.run = (executionArgs,executionOptions,callbackUrl) => {
try {
let exec = async () => {
let callbackFormat = ''
if(callbackUrl) {
callbackFormat = '--format=json'
}
if (typeof(executionArgs) === 'undefined') {
executionArgs = ""
}
if (typeof(executionOptions)==='undefined') {
executionOptions = ""
}
let runDockerCmd = async () =>{
let data = await this.cli.command(`${this.terminus_cmd} ${callbackFormat} ${executionArgs} ${executionOptions}`)
data.callbackUrl = callbackUrl;
if (this.name === 'auth:login'){
this.next = `auth:whoami`
}
if (this.name === 'auth:whoami'){
this.whoami = `${data.raw.trim()}`
}
this.executionResults = data
return this.executionResults
}
return runDockerCmd()
}
return exec()
.then(results => {
if (callbackUrl){
let url =`${results.callbackUrl}`
return axios.post(`${url}`, results.raw)
.then((res) => {
console.log(`POSTED results to ${url}`)
})
.catch((error) => {
Promise.reject(error)
})
} else {
return results.raw
}
})
}
catch(e) {
}
}
this.next = next
}
}
module.exports = BaseCommand