mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-19 05:37:51 +00:00
Merge pull request #1613 from balena-io/ts-lib-actions-keys
Convert lib/actions/keys to typescript
This commit is contained in:
commit
173a48eede
@ -1,123 +0,0 @@
|
||||
###
|
||||
Copyright 2016-2017 Balena
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
###
|
||||
|
||||
commandOptions = require('./command-options')
|
||||
|
||||
exports.list =
|
||||
signature: 'keys'
|
||||
description: 'list all ssh keys'
|
||||
help: '''
|
||||
Use this command to list all your SSH keys.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena keys
|
||||
'''
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
balena = require('balena-sdk').fromSharedOptions()
|
||||
visuals = require('resin-cli-visuals')
|
||||
|
||||
balena.models.key.getAll().then (keys) ->
|
||||
console.log visuals.table.horizontal keys, [
|
||||
'id'
|
||||
'title'
|
||||
]
|
||||
.nodeify(done)
|
||||
|
||||
exports.info =
|
||||
signature: 'key <id>'
|
||||
description: 'list a single ssh key'
|
||||
help: '''
|
||||
Use this command to show information about a single SSH key.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena key 17
|
||||
'''
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
balena = require('balena-sdk').fromSharedOptions()
|
||||
visuals = require('resin-cli-visuals')
|
||||
|
||||
balena.models.key.get(params.id).then (key) ->
|
||||
console.log visuals.table.vertical key, [
|
||||
'id'
|
||||
'title'
|
||||
]
|
||||
|
||||
# Since the public key string is long, it might
|
||||
# wrap to lines below, causing the table layout to break.
|
||||
# See https://github.com/balena-io/balena-cli/issues/151
|
||||
console.log('\n' + key.public_key)
|
||||
.nodeify(done)
|
||||
|
||||
exports.remove =
|
||||
signature: 'key rm <id>'
|
||||
description: 'remove a ssh key'
|
||||
help: '''
|
||||
Use this command to remove a SSH key from balena.
|
||||
|
||||
Notice this command asks for confirmation interactively.
|
||||
You can avoid this by passing the `--yes` boolean option.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena key rm 17
|
||||
$ balena key rm 17 --yes
|
||||
'''
|
||||
options: [ commandOptions.yes ]
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
balena = require('balena-sdk').fromSharedOptions()
|
||||
patterns = require('../utils/patterns')
|
||||
|
||||
patterns.confirm(options.yes, 'Are you sure you want to delete the key?').then ->
|
||||
balena.models.key.remove(params.id)
|
||||
.nodeify(done)
|
||||
|
||||
exports.add =
|
||||
signature: 'key add <name> [path]'
|
||||
description: 'add a SSH key to balena'
|
||||
help: '''
|
||||
Use this command to associate a new SSH key with your account.
|
||||
|
||||
If `path` is omitted, the command will attempt
|
||||
to read the SSH key from stdin.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena key add Main ~/.ssh/id_rsa.pub
|
||||
$ cat ~/.ssh/id_rsa.pub | balena key add Main
|
||||
'''
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
_ = require('lodash')
|
||||
Promise = require('bluebird')
|
||||
readFileAsync = Promise.promisify(require('fs').readFile)
|
||||
capitano = require('capitano')
|
||||
balena = require('balena-sdk').fromSharedOptions()
|
||||
|
||||
Promise.try ->
|
||||
return readFileAsync(params.path, encoding: 'utf8') if params.path?
|
||||
|
||||
# TODO: should this be promisified for consistency?
|
||||
Promise.fromNode (callback) ->
|
||||
capitano.utils.getStdin (data) ->
|
||||
return callback(null, data)
|
||||
|
||||
.then(_.partial(balena.models.key.create, params.name))
|
||||
.nodeify(done)
|
128
lib/actions/keys.ts
Normal file
128
lib/actions/keys.ts
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
Copyright 2016-2017 Balena
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { CommandDefinition } from 'capitano';
|
||||
import * as commandOptions from './command-options';
|
||||
|
||||
export const list: CommandDefinition = {
|
||||
signature: 'keys',
|
||||
description: 'list all ssh keys',
|
||||
help: `\
|
||||
Use this command to list all your SSH keys.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena keys\
|
||||
`,
|
||||
permission: 'user',
|
||||
async action() {
|
||||
const balena = (await import('balena-sdk')).fromSharedOptions();
|
||||
|
||||
const keys = await balena.models.key.getAll();
|
||||
|
||||
const visuals = await import('resin-cli-visuals');
|
||||
console.log(visuals.table.horizontal(keys, ['id', 'title']));
|
||||
},
|
||||
};
|
||||
|
||||
export const info: CommandDefinition<{ id: string }> = {
|
||||
signature: 'key <id>',
|
||||
description: 'list a single ssh key',
|
||||
help: `\
|
||||
Use this command to show information about a single SSH key.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena key 17\
|
||||
`,
|
||||
permission: 'user',
|
||||
async action(params) {
|
||||
const balena = (await import('balena-sdk')).fromSharedOptions();
|
||||
|
||||
const key = await balena.models.key.get(params.id);
|
||||
|
||||
const visuals = await import('resin-cli-visuals');
|
||||
console.log(visuals.table.vertical(key, ['id', 'title']));
|
||||
|
||||
// Since the public key string is long, it might
|
||||
// wrap to lines below, causing the table layout to break.
|
||||
// See https://github.com/balena-io/balena-cli/issues/151
|
||||
console.log('\n' + key.public_key);
|
||||
},
|
||||
};
|
||||
|
||||
export const remove: CommandDefinition<
|
||||
{ id: string },
|
||||
commandOptions.YesOption
|
||||
> = {
|
||||
signature: 'key rm <id>',
|
||||
description: 'remove a ssh key',
|
||||
help: `\
|
||||
Use this command to remove a SSH key from balena.
|
||||
|
||||
Notice this command asks for confirmation interactively.
|
||||
You can avoid this by passing the \`--yes\` boolean option.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena key rm 17
|
||||
$ balena key rm 17 --yes\
|
||||
`,
|
||||
options: [commandOptions.yes],
|
||||
permission: 'user',
|
||||
async action(params, options) {
|
||||
const patterns = await import('../utils/patterns');
|
||||
|
||||
await patterns.confirm(
|
||||
options.yes ?? false,
|
||||
'Are you sure you want to delete the key?',
|
||||
);
|
||||
|
||||
const balena = (await import('balena-sdk')).fromSharedOptions();
|
||||
await balena.models.key.remove(params.id);
|
||||
},
|
||||
};
|
||||
|
||||
export const add: CommandDefinition<{ name: string; path: string }> = {
|
||||
signature: 'key add <name> [path]',
|
||||
description: 'add a SSH key to balena',
|
||||
help: `\
|
||||
Use this command to associate a new SSH key with your account.
|
||||
|
||||
If \`path\` is omitted, the command will attempt
|
||||
to read the SSH key from stdin.
|
||||
|
||||
Examples:
|
||||
|
||||
$ balena key add Main ~/.ssh/id_rsa.pub
|
||||
$ cat ~/.ssh/id_rsa.pub | balena key add Main\
|
||||
`,
|
||||
permission: 'user',
|
||||
async action(params) {
|
||||
let key: string;
|
||||
if (params.path != null) {
|
||||
const { promisify } = await import('util');
|
||||
const readFileAsync = promisify((await import('fs')).readFile);
|
||||
key = await readFileAsync(params.path, 'utf8');
|
||||
} else {
|
||||
const getStdin = await import('get-stdin');
|
||||
key = await getStdin();
|
||||
}
|
||||
|
||||
const balena = (await import('balena-sdk')).fromSharedOptions();
|
||||
await balena.models.key.create(params.name, key);
|
||||
},
|
||||
};
|
33
npm-shrinkwrap.json
generated
33
npm-shrinkwrap.json
generated
@ -2688,6 +2688,11 @@
|
||||
"resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
|
||||
"integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo="
|
||||
},
|
||||
"get-stdin": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-4.0.1.tgz",
|
||||
"integrity": "sha1-uWjGsKBDhDJJAui/Gl3zJXmkUP4="
|
||||
},
|
||||
"is-elevated": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-elevated/-/is-elevated-1.0.0.tgz",
|
||||
@ -6318,9 +6323,9 @@
|
||||
"integrity": "sha1-3Xzn3hh8Bsi/NTeWrHHgmfCYDrw="
|
||||
},
|
||||
"get-stdin": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-4.0.1.tgz",
|
||||
"integrity": "sha1-uWjGsKBDhDJJAui/Gl3zJXmkUP4="
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-7.0.0.tgz",
|
||||
"integrity": "sha512-zRKcywvrXlXsA0v0i9Io4KDRaAw7+a1ZpjRwl9Wox8PFlVCCHra7E9c4kqXCoCM9nR5tBkaTTZRBoCm60bFqTQ=="
|
||||
},
|
||||
"get-stream": {
|
||||
"version": "4.1.0",
|
||||
@ -14460,6 +14465,14 @@
|
||||
"requires": {
|
||||
"get-stdin": "^4.0.1",
|
||||
"meow": "^3.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"get-stdin": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-4.0.1.tgz",
|
||||
"integrity": "sha1-uWjGsKBDhDJJAui/Gl3zJXmkUP4=",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"progress-stream": {
|
||||
@ -17101,6 +17114,13 @@
|
||||
"integrity": "sha1-DHlipq3vp7vUrDZkYKY4VSrhoKI=",
|
||||
"requires": {
|
||||
"get-stdin": "^4.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"get-stdin": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-4.0.1.tgz",
|
||||
"integrity": "sha1-uWjGsKBDhDJJAui/Gl3zJXmkUP4="
|
||||
}
|
||||
}
|
||||
},
|
||||
"strip-json-comments": {
|
||||
@ -17204,6 +17224,13 @@
|
||||
"requires": {
|
||||
"get-stdin": "^4.0.1",
|
||||
"minimist": "^1.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"get-stdin": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-4.0.1.tgz",
|
||||
"integrity": "sha1-uWjGsKBDhDJJAui/Gl3zJXmkUP4="
|
||||
}
|
||||
}
|
||||
},
|
||||
"tar": {
|
||||
|
@ -191,6 +191,7 @@
|
||||
"event-stream": "3.3.4",
|
||||
"express": "^4.13.3",
|
||||
"fast-boot2": "^1.0.9",
|
||||
"get-stdin": "^7.0.0",
|
||||
"global-agent": "^2.1.7",
|
||||
"global-tunnel-ng": "^2.1.1",
|
||||
"humanize": "0.0.9",
|
||||
|
Loading…
Reference in New Issue
Block a user