{"action":"closed","issue":{"url":"https://api.github.com/repos/zevv/duc/issues/33","labels_url":"https://api.github.com/repos/zevv/duc/issues/33/labels{/name}","comments_url":"https://api.github.com/repos/zevv/duc/issues/33/comments","events_url":"https://api.github.com/repos/zevv/duc/issues/33/events","html_url":"https://github.com/zevv/duc/issues/33","id":39114713,"number":33,"title":"Query multiple databases which indexed a different directory without specifying -d databasefilename on the commandline","user":{"login":"ghuls","id":1299177,"avatar_url":"https://avatars.githubusercontent.com/u/1299177?v=3","gravatar_id":"","url":"https://api.github.com/users/ghuls","html_url":"https://github.com/ghuls","followers_url":"https://api.github.com/users/ghuls/followers","following_url":"https://api.github.com/users/ghuls/following{/other_user}","gists_url":"https://api.github.com/users/ghuls/gists{/gist_id}","starred_url":"https://api.github.com/users/ghuls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ghuls/subscriptions","organizations_url":"https://api.github.com/users/ghuls/orgs","repos_url":"https://api.github.com/users/ghuls/repos","events_url":"https://api.github.com/users/ghuls/events{/privacy}","received_events_url":"https://api.github.com/users/ghuls/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/zevv/duc/labels/duplicate","name":"duplicate","color":"cccccc"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2014-07-30T17:07:22Z","updated_at":"2015-04-23T12:24:48Z","closed_at":"2015-04-23T12:24:48Z","body":"I wrote a bash function to allow querying multiple databases (which indexed a different directory (actually fileserver)) to avoid the need to specify -d databasename on the command line.\r\n\r\nIt would be nice if duc would support something like this in the future by default.\r\n\r\n```\r\nUsage:\r\n  # This will use the '/media/srv-01/duc/db/srv-01.duc.db' database.\r\n  duce ls /media/srv-01\r\n\r\n  # This will use the '/media/srv-03/duc/db/srv-03.duc.db' database.\r\n  duce ls /media/srv-03/some/path\r\n\r\n  # This will use the '/media/srv-03/duc/db/srv-03-more-often.duc.db' database.\r\n  duce ls /media/srv-03/path/that/gets/indexed/more/often/something\r\n```\r\n\r\nAdd this script to your .bashrc:\r\n```bash\r\n# Enhanced duc function (a better name is welcome).\r\nduce () {\r\n    # Link indexed paths with database filenames.\r\n    local -A pathname_to_db_array;\r\n\r\n    # START: Adapt the paths to your configuration.\r\n    pathname_to_db_array['/media/srv-01']='/media/srv-01/duc/db/srv-01.duc.db';\r\n    pathname_to_db_array['/media/srv-02']='/media/srv-02/duc/db/srv-02.duc.db';\r\n    pathname_to_db_array['/media/srv-03']='/media/srv-03/duc/db/srv-03.duc.db';\r\n    pathname_to_db_array['/media/srv-03/path/that/gets/indexed/more/often']='/media/srv-03/duc/db/srv-03-more-often.duc.db';\r\n    # END: Adapt the paths to your configuration.\r\n\r\n    if [ ${#@} -ge 1 ] ; then\r\n        local command=\"${1}\";\r\n        \r\n        local -i parse_path=0;\r\n        \r\n        case \"${command}\" in\r\n            help)  duc ${@};\r\n                   return $?;;\r\n            info)  duc ${@};\r\n                   return $?;;\r\n            index) duc ${@};\r\n                   return $?;;\r\n            gui)   parse_path=1;;\r\n            ls)    parse_path=1;;\r\n            graph) parse_path=1;;\r\n            xml)   parse_path=1;;\r\n            *)     duc help;\r\n                   return $?;\r\n        esac\r\n        \r\n        if [ ${parse_path} -eq 1 ] ; then\r\n            local arg='';\r\n            local path='';\r\n            local absolute_path='';\r\n            local pathname_key='';\r\n            local suffix_path='';\r\n            local -i length_suffix_path=999999999;\r\n            local db_filename='';\r\n\r\n            for arg in ${@:2} ; do\r\n                # Last variable can be the path, so save it.\r\n                path=${arg};\r\n            done;\r\n            \r\n            if [ ! -e \"${path}\" ] ; then\r\n                # If the last variable is not a path, or does not exist,\r\n                # use the current working directory.\r\n                path=\"${PWD}\";\r\n            fi\r\n            \r\n            # Get absolute path.\r\n            absolute_path=$(realpath -s \"${path}\");\r\n            \r\n            # Check if the absolute path starts with one of the paths used as keys in the pathname_to_db_array.\r\n            for pathname_key in \"${!pathname_to_db_array[@]}\" ; do\r\n                suffix_path=\"${absolute_path#\"${pathname_key}\"}\";\r\n                \r\n                if [ \"${suffix_path}\" != \"${absolute_path}\" ] ; then\r\n                    # Take the key value of pathname_to_db array that has the longest match with the absolute path name.\r\n                    if [ ${length_suffix_path} -gt ${#suffix_path} ] ; then\r\n                        length_suffix_path=${#suffix_path};\r\n                        db_filename=\"${pathname_to_db_array[\"${pathname_key}\"]}\";\r\n                    fi\r\n                fi\r\n            done\r\n\r\n            if [ -z \"${db_filename}\" ] ; then\r\n                # If the database filename was empty, the absolute path did not start with any of the pathnames in pathname_to_db_array. \r\n                duc ${@};\r\n                return $?;\r\n            elif [ -f \"${db_filename}\" ] ; then\r\n                # The best database for the provided path was found.\r\n                duc ${command} -d \"${db_filename}\" \"${@:2}\";\r\n                return $?;\r\n            else\r\n                printf 'Error: database filename \"%s\" does not exist.\\n' \"${db_filename}\" > /dev/stderr;\r\n                return 1;\r\n            fi\r\n        fi\r\n    else\r\n        # Display help when no arguments are given.\r\n        duc help;\r\n        return $?;\r\n    fi\r\n}\r\n```"}}