diff --git a/contrib/completion/bash/termius b/contrib/completion/bash/termius index 6f49f9b..6ad4d26 100644 --- a/contrib/completion/bash/termius +++ b/contrib/completion/bash/termius @@ -198,7 +198,7 @@ _termius() _get_comp_words_by_ref -n : cur prev words # Command data: - cmds='complete connect fullclean group groups help host hosts identities identity info key keys login logout pfrule pfrules pull push snippet snippets sync tags settings' + cmds='complete connect fullclean group groups help host hosts identities identity info init key keys login logout pfrule pfrules pull push snippet snippets sync tags settings' cmds_complete='-h --help --name --shell' cmds_complete_arg_options='--name --shell' cmds_settings='-h --help --synchronize-key --agent-forwarding --log-file' @@ -232,6 +232,9 @@ _termius() cmds_info='-h --help --log-file -G --group -H --host -M --no-merge -f --format -c --column --prefix --noindent --address --max-width' cmds_info_arg_options='--log-file -f --format -c --column --prefix --address' cmds_info_file_options='--log-file' + cmds_init='-h --help --log-file -u --username -p --password' + cmds_init_arg_options='--log-file -u --username -p --password' + cmds_init_file_options='--log-file' cmds_key='-h --help --log-file -i --identity-file -d --delete -L --label' cmds_key_arg_options='--log-file -i --identity-file -L --label' cmds_key_file_options='--log-file -i --identity-file' diff --git a/contrib/completion/zsh/_termius b/contrib/completion/zsh/_termius index f980ddc..6c63b9e 100644 --- a/contrib/completion/zsh/_termius +++ b/contrib/completion/zsh/_termius @@ -24,6 +24,7 @@ _termius() { "identities[List ssh identities.]" \ "identity[identity operations.]" \ "info[Display host or group connection arguments.]" \ + "info[Initialize termius cli.]" \ "key[Ssh key operations.]" \ "keys[List ssh keys.]" \ "login[Authenticate on termius.com.]" \ @@ -125,7 +126,7 @@ _termius_groups() { } _termius_help() { _arguments \ - '1:command:(complete connect fullclean group groups help host hosts identities identity info key keys login logout pfrule pfrules pull push snippet snippets sync tags)' \ + '1:command:(complete connect fullclean group groups help host hosts identities identity info init key keys login logout pfrule pfrules pull push snippet snippets sync tags)' \ '-h[Display help]' \ '--help[Display help]' } @@ -320,6 +321,16 @@ _termius_push() { '-p[Password]:isnotsecure:()' \ '--password[Password]:isnotsecure:()' } +_termius_init() { + _arguments \ + '-h[Display help]' \ + '--help[Display help]' \ + '--log-file[Log to this file]:filename:_files' \ + '-u[Username]:username:()' \ + '--username[Username]:username:()' \ + '-p[Password]:isnotsecure:()' \ + '--password[Password]:isnotsecure:()' +} _termius_snippet() { _arguments \ '-h[Display help]' \ diff --git a/setup.py b/setup.py index ccbf2c5..6398358 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,8 @@ 'tags = termius.handlers:TagsCommand', 'info = termius.handlers:InfoCommand', 'connect = termius.handlers:ConnectCommand', - 'crypto = termius.cloud.commands:CryptoCommand' + 'crypto = termius.cloud.commands:CryptoCommand', + 'init = termius.handlers:InitCommand' ] diff --git a/termius/handlers/__init__.py b/termius/handlers/__init__.py index f11cfb1..de52e01 100644 --- a/termius/handlers/__init__.py +++ b/termius/handlers/__init__.py @@ -9,3 +9,4 @@ from .tag import TagsCommand # noqa from .info import InfoCommand # noqa from .connect import ConnectCommand # noqa +from .init import InitCommand # noqa diff --git a/termius/handlers/init.py b/termius/handlers/init.py new file mode 100644 index 0000000..f6d0437 --- /dev/null +++ b/termius/handlers/init.py @@ -0,0 +1,81 @@ +"""Module with init command.""" + +from argparse import Namespace + +import six + +from termius.account.commands import LoginCommand +from termius.cloud.commands import PullCommand, PushCommand +from termius.core.commands import AbstractCommand +from termius.sync.commands import SyncCommand + + +class InitCommand(AbstractCommand): + """Initialize termius cli.""" + + # pylint: disable=no-self-use + def prompt_username(self): + """Ask username prompt.""" + return six.moves.input("Termius's username: ") + + # pylint: disable=no-self-use + def prompt_authy_token(self): + """Ask authy token prompt.""" + return six.moves.input('Authy token: ') + + def extend_parser(self, parser): + """Add more arguments to parser.""" + parser.add_argument('-u', '--username', metavar='USERNAME') + parser.add_argument('-p', '--password', metavar='PASSWORD') + return parser + + def init_namespace(self, parsed_args, username, password): + """Make authenticated Namespace instance.""" + return Namespace( + log_file=parsed_args.log_file, + username=username, + password=password, + service='ssh', + credentials=None + ) + + def login(self, parsed_args): + """Wrapper for login command.""" + command = LoginCommand(self.app, self.app_args, self.cmd_name) + command.take_action(parsed_args) + + def pull(self, parsed_args): + """Wrapper for pull command.""" + command = PullCommand(self.app, self.app_args, self.cmd_name) + command.take_action(parsed_args) + + def sync_ssh(self, parsed_args): + """Wrapper for sync command.""" + command = SyncCommand(self.app, self.app_args, self.cmd_name) + command.take_action(parsed_args) + + def push(self, parsed_args): + """Wrapper for push command.""" + command = PushCommand(self.app, self.app_args, self.cmd_name) + command.take_action(parsed_args) + + def take_action(self, parsed_args): + """Process command call.""" + self.log.info('Initialize Termius CLI...\n') + + username = parsed_args.username or self.prompt_username() + password = parsed_args.password or self.prompt_password() + + namespace = self.init_namespace( + parsed_args, username, password + ) + + self.login(namespace) + self.log.info('\nPull your data from termius cloud...') + self.pull(namespace) + self.log.info('\nSync local storage with your ~/.ssh/config...') + self.sync_ssh(namespace) + self.log.info('\nPush local data to termius cloud...') + self.push(namespace) + + self.log.info('\nTermius CLI successfully initialized.') diff --git a/tests/integration/init.bats b/tests/integration/init.bats new file mode 100644 index 0000000..879251e --- /dev/null +++ b/tests/integration/init.bats @@ -0,0 +1,38 @@ +#!/usr/bin/env bats +load test_helper + +@test "init help by arg" { + run termius init --help + [ "$status" -eq 0 ] +} + +@test "init help command" { + run termius help init + [ "$status" -eq 0 ] +} + +@test "log into tester account" { + if [ "$TERMIUS_USERNAME" == '' ] || [ "$TERMIUS_PASSWORD" == '' ];then + skip '$TERMIUS_USERNAME or $TERMIUS_PASSWORD are not set!' + fi + rm ~/.termius/config || true + + run termius init --username $TERMIUS_USERNAME -p $TERMIUS_PASSWORD + [ "$status" -eq 0 ] + ! [ -z "$(cat ~/.termius/config)" ] +} + +@test "Change tester account" { + if [ "$TERMIUS_USERNAME" == '' ] || [ "$TERMIUS_PASSWORD" == '' ] || + [ "$TERMIUS_USERNAME_2" == '' ] || [ "$TERMIUS_PASSWORD_2" == '' ] ;then + skip '$TERMIUS_USERNAME or $TERMIUS_PASSWORD or $TERMIUS_USERNAME_2 or $TERMIUS_PASSWORD_2 are not set!' + fi + rm ~/.termius/config || true + + termius init --username $TERMIUS_USERNAME -p $TERMIUS_PASSWORD + populate_storage + run termius init --username $TERMIUS_USERNAME_2 -p $TERMIUS_PASSWORD_2 + [ "$status" -eq 0 ] + ! [ -z "$(cat ~/.termius/config)" ] + assert_clear_storage +}