-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
not extremely useful but a nice to have. precursor to updating region select for app env creation
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module.exports = async function (params) { | ||
const { | ||
args: { _: commands }, | ||
} = params | ||
const action = commands[1] | ||
|
||
if (HELP[action]) return HELP[action] | ||
|
||
return { | ||
en: { | ||
usage: [ 'regions <action> <parameters>', '[options]' ], | ||
description: 'Begin regions', | ||
contents: { | ||
header: 'Regions actions', | ||
items: [ | ||
{ name: 'list', description: 'List Begin regions' }, | ||
], | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
const HELP = { | ||
list: { | ||
en: { | ||
usage: [ 'regions list', '[options]' ], | ||
description: 'List Begin regions', | ||
contents: { | ||
header: 'List Begin regions', | ||
example: 'begin regions list', | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const names = { en: [ 'regions' ] } | ||
const subcommands = [ 'list' ] | ||
const aliases = { ls: 'list' } | ||
const defaultCommand = 'list' | ||
const help = require('./help').bind({}) | ||
|
||
async function action (params) { | ||
const { args } = params | ||
const { _ } = args | ||
let subcommand = _[1] || defaultCommand | ||
const alias = Object.keys(aliases).includes(subcommand) && aliases[subcommand] | ||
subcommand = alias || subcommand | ||
|
||
if (subcommands.includes(subcommand)) { | ||
const lib = require('../../lib') | ||
const { getConfig } = lib | ||
const { action } = require(`./${subcommand}`) | ||
|
||
const config = getConfig(params) | ||
return action({ | ||
config, | ||
...params | ||
}) | ||
} | ||
else { | ||
return help(params) | ||
} | ||
} | ||
|
||
module.exports = { | ||
names, | ||
action, | ||
help, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
async function action (params) { | ||
let client = require('@begin/api') | ||
let f = require('../../lib/format')() | ||
let collumns = require('../../lib/columns') | ||
let { config } = params | ||
let { access_token: token, stagingAPI: _staging } = config | ||
|
||
let regions = await client.regions({ token, _staging }) | ||
|
||
if (!regions) return Error('No regions found!') | ||
|
||
return [ | ||
f.bold('Available regions:'), | ||
collumns(Object.entries(regions).map(([ k, v ]) => [ v, k ]), 2) | ||
].join('\n') | ||
} | ||
|
||
module.exports = { | ||
name: 'list', | ||
description: 'List Begin regions', | ||
action, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* | ||
* @param {Array<Array<string>>} arrays | ||
* @param {Number} padding | ||
* @returns {string} mluti-line string | ||
*/ | ||
function columns (arrays, padding = 1) { | ||
const colLengths = [] | ||
arrays.forEach(row => { | ||
row.forEach((item, colI) => { | ||
colLengths[colI] = Math.max(colLengths[colI] || 0, item.length) | ||
}) | ||
}) | ||
|
||
const lines = [] | ||
arrays.forEach(row => { | ||
let line = row.map((item, colI) => { | ||
return item.padEnd(colLengths[colI] + padding, ' ') | ||
}).join('').trim() | ||
|
||
lines.push(line) | ||
}) | ||
|
||
return lines.join('\n') | ||
} | ||
|
||
module.exports = columns |