keepassxreboot / keepassxc

KeePassXC is a cross-platform community-driven port of the Windows application “Keepass Password Safe”.
https://keepassxc.org/
Other
20.69k stars 1.43k forks source link

CLI - Setting Default Dataset Path or Using Environment Variable #10593

Open sadid opened 4 months ago

sadid commented 4 months ago

Instead of keepassxc-cli <sub-command> [options] database <arg>, have dataset set either by using a default path or an environment variable so it become keepassxc-cli <subcommand> [options] <arg>.

I am using this bash script to achieve this:

#!/bin/bash

kp() {
    DATABASE="/path/to/database"

    subcommand="$1"
    shift

    options=()
    while [[ $# -gt 1 ]]; do
        options+=("$1")
        shift
    done

    term="$1"

    keepassxc-cli "$subcommand" "${options[@]}" "$DATABASE" "$term"
}

kp "$@"

This script is simple, but it's not complete. For example, it doesn't handle all corner cases like keepassxc-cli ls -h properly. Does keepassxc-cli have a mechanism to basically not give the dataset and fall back to a default one or an environment variable? I did search the manual but couldn't find a relevant piece.

droidmonkey commented 4 months ago

See here for a previous discussion: https://github.com/keepassxreboot/keepassxc/issues/6225

I am not against querying an env var if the database is not supplied on the command line itself. Probably something like KPXC_CLI_DBFILE

sadid commented 4 months ago

First of all, Thanks for the awesome app!

Yes I think env var helps a lot. Alternatively, checking for a default path (like ~/.DB.kdbx) when the databse have not provided in the command line might help.

Just for sake of compelteness, I improved my wrapper and now it cover all my use cases and I think more versatile than the #6225

#!/bin/bash

function kp_wrapper() {
    sub_command="$1"
    shift

    local DB="$HOME/.DB.kdbx"

    if [[ $# -le 1 ]]; then
      echo "keepassxc-cli $sub_command $@ $DB"
      keepassxc-cli "$sub_command" "$@" "$DB"
      return
    fi

    local last_arg="${@: -1}"
    local args=("${@: 1:$(($#-1))}")

    if [[ $last_arg == -* ]]; then
        args+=("$DB")
    else
        args=("${args[@]}" "$DB" "$last_arg")
    fi

    echo "keepassxc-cli $sub_command ${args[@]}"
    keepassxc-cli "$sub_command" "${args[@]}"
}

kp_wrapper "$@"