krz/yoshi-cli

A password manager for the command line.

clone: git clone https://gitbay.org/krz/yoshi-cli.git

main: yoshi/cli.py · raw

  1"""
  2This script uses argparse to parse command line arguments.
  3
  4It imports the required modules and sets up a parser with basic options for demonstration purposes.
  5"""
  6
  7import argparse
  8from yoshi import crypto, database, process
  9
 10
 11def yoshi():
 12    """
 13    CLI entry point logic, used to parse user inputs.
 14    """
 15    parser = argparse.ArgumentParser(
 16        description="Manage your username and passwords via a convenient CLI vault."
 17    )
 18
 19    # Top-level arguments
 20    group_one = parser.add_mutually_exclusive_group()
 21    group_one.add_argument(
 22        "-n", "--new", help="Create a new account.", action="store_true"
 23    )
 24    group_one.add_argument(
 25        "-l", "--list", help="List all saved accounts.", action="store_true"
 26    )
 27    group_one.add_argument(
 28        "-e", "--edit", help="Edit a saved account.", action="store_true"
 29    )
 30    group_one.add_argument(
 31        "-d", "--delete", help="Delete a saved account.", action="store_true"
 32    )
 33    group_one.add_argument(
 34        "--purge",
 35        help=(
 36            "Purge all accounts and delete the vault. "
 37            "(Caution: this will irreversibly destroy your data.)"
 38        ),
 39        action="store_true",
 40    )
 41    group_one.add_argument("--encrypt", help="Encrypt the vault.", action="store_true")
 42    group_one.add_argument("--decrypt", help="Decrypt the vault.", action="store_true")
 43
 44    # Encryption flags
 45    group_two = parser.add_mutually_exclusive_group()
 46    group_two.add_argument(
 47        "-g",
 48        "--generate",
 49        help=("When using the --encrypt option, generate a new encryption key."),
 50        action="store_true",
 51    )
 52    group_two.add_argument(
 53        "-k",
 54        "--keyfile",
 55        help="Path to existing key file.",
 56        action="store",
 57        nargs=1,
 58        type=str,
 59    )
 60
 61    # Edit flags
 62    group_three = parser.add_argument_group()
 63    group_three.add_argument(
 64        "-u",
 65        "--uuid",
 66        help=("When using the --edit or --delete options, provide the account UUID."),
 67        action="store",
 68        nargs=1,
 69        type=str,
 70    )
 71    group_three.add_argument(
 72        "-f",
 73        "--field",
 74        help=(
 75            "When using the --edit option, specify the field to edit (integer index)."
 76        ),
 77        action="store",
 78        nargs=1,
 79        type=int,
 80    )
 81
 82    args = parser.parse_args()
 83
 84    if args.decrypt:
 85        if args.keyfile:
 86            key = crypto.load_key(args.keyfile[0])
 87        else:
 88            key = input("Please enter your decryption key: ")
 89        crypto.decrypt(key)
 90    elif args.encrypt:
 91        if args.generate:
 92            key = crypto.generate_key()
 93            print(
 94                "WRITE THIS KEY DOWN SOMEWHERE SAFE. YOU WILL NOT BE ABLE TO DECRYPT "
 95                "YOUR DATA WITHOUT IT!"
 96            )
 97            print(key.decode())
 98            print("\n")
 99        else:
100            if args.keyfile:
101                key = crypto.load_key(args.keyfile[0])
102            else:
103                key = input("Please enter your encryption key: ")
104        crypto.encrypt(key)
105    elif database.check_table():
106        if args.new:
107            process.create_account()
108        elif args.list:
109            process.list_accounts()
110        elif args.edit:
111            process.edit_account(args.uuid[0], args.field[0])
112        elif args.delete:
113            process.delete_account(args.uuid[0])
114        elif args.purge:
115            process.purge_accounts()
116        else:
117            raise TypeError(
118                "Please specify a command or use the --help flag for more information."
119            )
120
121
122if __name__ == "__main__":
123    yoshi()