Skip to content

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

@sadid

Description

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.

Activity

droidmonkey

droidmonkey commented on Apr 17, 2024

@droidmonkey
Member

See here for a previous discussion: #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

added this to the v2.8.0 milestone on Apr 17, 2024
sadid

sadid commented on Apr 17, 2024

@sadid
Author

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 "$@"
salazarp

salazarp commented on Nov 3, 2024

@salazarp

Would it make sense to give the database location its own option (e.g. "-d") rather than make it positional?

I could then do a quick alias: function kp(){ keepassxc-cli -d "${KDBX}" "$@"; }
...and I could happily do kp ls or kp show -s mygroup, or anything else.

As it stands currently, I can naively try this: function kp(){ keepassxc-cli "$@" "$KDBX"; }
...which will work well for kp -l but not for kp -l $KBDX mygroup.

The problem is the alias/function/script needs to parse how many arguments are there before the database path in every command, and it all gets cumbersome quickly.

Like all the others, awed and ever grateful for the work. 🫶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      CLI - Setting Default Dataset Path or Using Environment Variable · Issue #10593 · keepassxreboot/keepassxc