-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.py
49 lines (33 loc) · 954 Bytes
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import click
from featuren.settings import db
from featuren.models.user import User
@click.group()
def cli():
pass
def validate_user(ctx, param, value):
"""
User validation.
"""
user = User.where("username", value).first()
if user:
click.secho(f"Username: {value} already exists, please try other one.", **ERROR)
raise click.Abort
return value
@cli.command()
@click.option("--username", callback=validate_user, prompt=True)
@click.password_option()
def add_user(username, password):
"""
Command to add new user.
"""
user_data = {"username": username, "password": password}
try:
user = User()
user.create(user_data)
click.secho(f"User: {username} create successfully!", **SUCCESS)
except Exception:
click.secho(f"Please try again", **ERROR)
# custom styles #
SUCCESS = dict(bg="green", fg="white")
ERROR = dict(bg="red", fg="white")
cli()