2018-05-25 20:33:22 +00:00
|
|
|
// Low level service that allows users to input paths to make requests to vault
|
|
|
|
// this service provides the UI synecdote to the cli commands read, write, delete, and list
|
2018-09-25 16:28:26 +00:00
|
|
|
import Service from '@ember/service';
|
2018-05-25 20:33:22 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
import { getOwner } from '@ember/application';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { shiftCommandIndex } from 'vault/lib/console-helpers';
|
2018-05-25 20:33:22 +00:00
|
|
|
|
|
|
|
export function sanitizePath(path) {
|
|
|
|
//remove whitespace + remove trailing and leading slashes
|
|
|
|
return path.trim().replace(/^\/+|\/+$/g, '');
|
|
|
|
}
|
|
|
|
export function ensureTrailingSlash(path) {
|
|
|
|
return path.replace(/(\w+[^/]$)/g, '$1/');
|
|
|
|
}
|
|
|
|
|
|
|
|
const VERBS = {
|
|
|
|
read: 'GET',
|
|
|
|
list: 'GET',
|
|
|
|
write: 'POST',
|
|
|
|
delete: 'DELETE',
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
isOpen: false,
|
|
|
|
|
|
|
|
adapter() {
|
|
|
|
return getOwner(this).lookup('adapter:console');
|
|
|
|
},
|
|
|
|
commandHistory: computed('log.[]', function() {
|
|
|
|
return this.get('log').filterBy('type', 'command');
|
|
|
|
}),
|
|
|
|
log: computed(function() {
|
|
|
|
return [];
|
|
|
|
}),
|
|
|
|
commandIndex: null,
|
|
|
|
|
|
|
|
shiftCommandIndex(keyCode, setCommandFn = () => {}) {
|
|
|
|
let [newIndex, newCommand] = shiftCommandIndex(
|
|
|
|
keyCode,
|
|
|
|
this.get('commandHistory'),
|
|
|
|
this.get('commandIndex')
|
|
|
|
);
|
|
|
|
if (newCommand !== undefined && newIndex !== undefined) {
|
|
|
|
this.set('commandIndex', newIndex);
|
|
|
|
setCommandFn(newCommand);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-29 14:14:31 +00:00
|
|
|
clearLog(clearAll = false) {
|
2018-05-25 20:33:22 +00:00
|
|
|
let log = this.get('log');
|
|
|
|
let history;
|
|
|
|
if (!clearAll) {
|
|
|
|
history = this.get('commandHistory').slice();
|
|
|
|
history.setEach('hidden', true);
|
|
|
|
}
|
|
|
|
log.clear();
|
|
|
|
if (history) {
|
|
|
|
log.addObjects(history);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
logAndOutput(command, logContent) {
|
|
|
|
let log = this.get('log');
|
2018-06-01 22:18:31 +00:00
|
|
|
if (command) {
|
|
|
|
log.pushObject({ type: 'command', content: command });
|
|
|
|
this.set('commandIndex', null);
|
|
|
|
}
|
2018-05-25 20:33:22 +00:00
|
|
|
if (logContent) {
|
|
|
|
log.pushObject(logContent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ajax(operation, path, options = {}) {
|
|
|
|
let verb = VERBS[operation];
|
|
|
|
let adapter = this.adapter();
|
|
|
|
let url = adapter.buildURL(path);
|
|
|
|
let { data, wrapTTL } = options;
|
|
|
|
return adapter.ajax(url, verb, {
|
|
|
|
data,
|
|
|
|
wrapTTL,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
read(path, data, wrapTTL) {
|
|
|
|
return this.ajax('read', sanitizePath(path), { wrapTTL });
|
|
|
|
},
|
|
|
|
|
|
|
|
write(path, data, wrapTTL) {
|
|
|
|
return this.ajax('write', sanitizePath(path), { data, wrapTTL });
|
|
|
|
},
|
|
|
|
|
|
|
|
delete(path) {
|
|
|
|
return this.ajax('delete', sanitizePath(path));
|
|
|
|
},
|
|
|
|
|
|
|
|
list(path, data, wrapTTL) {
|
|
|
|
let listPath = ensureTrailingSlash(sanitizePath(path));
|
|
|
|
return this.ajax('list', listPath, {
|
|
|
|
data: {
|
|
|
|
list: true,
|
|
|
|
},
|
|
|
|
wrapTTL,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|