Skip to content

Commit

Permalink
Accept both -f and --force in the web terminal (#13683)
Browse files Browse the repository at this point in the history
* Accept both -f and --force in the web terminal

This aligns the behavior of the web terminal with the `vault write ...`
command to make it a bit more user friendly.

* Add changelog

* Use === instead of ==
  • Loading branch information
remilapeyre authored Jan 20, 2022
1 parent df217c6 commit 2818ae3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/13683.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:improvement
ui: The integrated web terminal now accepts both `-f` and `--force` as aliases
for `-force` for the `write` commmand.
```
2 changes: 1 addition & 1 deletion ui/app/components/console/ui-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default Component.extend({
let [method, flagArray, path, dataArray] = serviceArgs;

if (dataArray || flagArray) {
var { data, flags } = extractDataAndFlags(dataArray, flagArray);
var { data, flags } = extractDataAndFlags(method, dataArray, flagArray);
}

let inputError = logErrorFromInput(path, method, flags, dataArray);
Expand Down
6 changes: 5 additions & 1 deletion ui/app/lib/console-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parse } from 'shell-quote';
const supportedCommands = ['read', 'write', 'list', 'delete'];
const uiCommands = ['api', 'clearall', 'clear', 'fullscreen', 'refresh'];

export function extractDataAndFlags(data, flags) {
export function extractDataAndFlags(method, data, flags) {
return data.concat(flags).reduce(
(accumulator, val) => {
// will be "key=value" or "-flag=value" or "foo=bar=baz"
Expand All @@ -16,6 +16,10 @@ export function extractDataAndFlags(data, flags) {
let flagName = item.replace(/^-/, '');
if (flagName === 'wrap-ttl') {
flagName = 'wrapTTL';
} else if (method === 'write') {
if (flagName === 'f' || flagName === '-force') {
flagName = 'force';
}
}
accumulator.flags[flagName] = value || true;
return accumulator;
Expand Down
39 changes: 38 additions & 1 deletion ui/tests/unit/lib/console-helpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ module('Unit | Lib | console helpers', function () {

const testExtractCases = [
{
method: 'read',
name: 'data fields',
input: [
[
Expand All @@ -144,6 +145,7 @@ module('Unit | Lib | console helpers', function () {
},
},
{
method: 'read',
name: 'repeated data and a flag',
input: [['allowed_domains=example.com', 'allowed_domains=foo.example.com'], ['-wrap-ttl=2h']],
expected: {
Expand All @@ -156,6 +158,7 @@ module('Unit | Lib | console helpers', function () {
},
},
{
method: 'read',
name: 'data with more than one equals sign',
input: [['foo=bar=baz', 'foo=baz=bop', 'some=value=val'], []],
expected: {
Expand All @@ -167,6 +170,7 @@ module('Unit | Lib | console helpers', function () {
},
},
{
method: 'read',
name: 'data with empty values',
input: [[`foo=`, 'some=thing'], []],
expected: {
Expand All @@ -177,11 +181,44 @@ module('Unit | Lib | console helpers', function () {
flags: {},
},
},
{
method: 'write',
name: 'write with force flag',
input: [[], ['-force']],
expected: {
data: {},
flags: {
force: true,
},
},
},
{
method: 'write',
name: 'write with force short flag',
input: [[], ['-f']],
expected: {
data: {},
flags: {
force: true,
},
},
},
{
method: 'write',
name: 'write with GNU style force flag',
input: [[], ['--force']],
expected: {
data: {},
flags: {
force: true,
},
},
},
];

testExtractCases.forEach(function (testCase) {
test(`#extractDataAndFlags: ${testCase.name}`, function (assert) {
let { data, flags } = extractDataAndFlags(...testCase.input);
let { data, flags } = extractDataAndFlags(testCase.method, ...testCase.input);
assert.deepEqual(data, testCase.expected.data, 'has expected data');
assert.deepEqual(flags, testCase.expected.flags, 'has expected flags');
});
Expand Down

0 comments on commit 2818ae3

Please sign in to comment.