snips-sam
Version:
The Snips Assistant Manager
50 lines (44 loc) • 1.7 kB
text/typescript
import * as process from 'process';
import chalk from 'chalk';
import cli from '../cli';
import * as yargs from 'yargs';
import { Config, Credentials } from '../utils/config';
import { SSHService } from '../session/ssh';
exports.command = 'connect <hostname>';
exports.desc = 'Connect to a device with its <hostname> or ip.';
exports.handler = async (argv: yargs.Argv) => {
let hostname: string = argv['hostname'];
let credentials: Credentials | undefined;
let password: string | undefined;
try {
const response = await cli.prompt.promptQuestions([Config.usernameQuestion, Config.passwordQuestion]);
password = response['password'];
credentials = { hostname, username: response['username'] };
} catch (e) {
cli.stream.error('Error entering your credentials: ' + e.message);
process.exit();
}
if (password === undefined) {
cli.stream.error(`Password must be set`);
return process.exit();
}
if (credentials === undefined) {
cli.stream.hint(`Please first connect to your device with\n`
+ chalk.blue(`sam connect <hostname>`));
return process.exit();
}
const onConnected = () => {
cli.stream.success('Connection successful.');
};
const onSSHCopyId = () => {
cli.stream.success('SSH key generated & copied on the device.');
};
const onCredentialsSaved = () => {
cli.stream.success('Credentials saved.');
};
const ssh = new SSHService();
await ssh.newConnection(credentials, password, onConnected, onSSHCopyId, onCredentialsSaved)
.catch(e => cli.stream.error(e));
ssh.disconnect();
process.exit();
};