Skip to content

Commit

Permalink
Implement termius init command for simple export.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxbey committed Mar 20, 2017
1 parent 36a3d12 commit 3a73966
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
5 changes: 4 additions & 1 deletion contrib/completion/bash/termius
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
13 changes: 12 additions & 1 deletion contrib/completion/zsh/_termius
Original file line number Diff line number Diff line change
Expand Up @@ -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.]" \
Expand Down Expand Up @@ -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]'
}
Expand Down Expand Up @@ -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]' \
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]


Expand Down
1 change: 1 addition & 0 deletions termius/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .tag import TagsCommand # noqa
from .info import InfoCommand # noqa
from .connect import ConnectCommand # noqa
from .init import InitCommand # noqa
81 changes: 81 additions & 0 deletions termius/handlers/init.py
Original file line number Diff line number Diff line change
@@ -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.')
38 changes: 38 additions & 0 deletions tests/integration/init.bats
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3a73966

Please sign in to comment.