Skip to content

Commit

Permalink
Fix bug in migration of config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopaganini committed Aug 26, 2024
1 parent 1345db7 commit d184d2d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions snip
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ readonly VERSION="0.0.3"

# Default variables
readonly ARGV0="$0"
readonly SHORT_HOSTNAME="${HOSTNAME#.*}"
readonly PROGRAM="${0##*/}"
readonly XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
readonly CONFIG_DIR="${XDG_CONFIG_HOME}/snip"
readonly CONFIG_FILENAME="config.${HOSTNAME?}"
readonly CONFIG_FILE="${CONFIG_DIR}/${CONFIG_FILENAME}"
readonly DB_PREFIX="${CONFIG_DIR}/db"
readonly DB_FILE="${DB_PREFIX}.${SHORT_HOSTNAME?}"
readonly CONFIG_FILE="${CONFIG_DIR}/config.${SHORT_HOSTNAME?}"
readonly DB_FILE="${DB_PREFIX}.${HOSTNAME?}"

# Base column command. Behavior of the last column depends on the command
# (list/find).
Expand Down Expand Up @@ -625,12 +625,12 @@ function repo_sync() {
}

# migrate_from_v0_0_2 - Migrate the config file and database from v0.0.2.
# - Config file: Rename bare config name to config.SHORT_HOSTNAME form.
# - Config file: Rename bare config name to config.HOSTNAME form.
# - Database: Add host as the first column, if not present.
function migrate_from_v0_0_2() {
# Old config file == new config file without the .SHORT_HOSTNAME suffix
local old_config="${CONFIG_FILE%.*}"
if [[ -s "${CONFIG_FILE%.*}" ]]; then
# Old config file == new config file without the .HOSTNAME suffix
local old_config="${CONFIG_DIR}/${CONFIG_FILENAME%%.*}"
if [[ -s "${old_config}" ]]; then
mv "${old_config}" "${CONFIG_FILE}"
print_ok "Configuration migrated from v0.0.2"
fi
Expand All @@ -639,7 +639,7 @@ function migrate_from_v0_0_2() {
# we assume it to be using v0.0.2. Keep the regexp as simple as possible to
# avoid compatibility issues.
if head -1 "${DB_FILE}" | grep -q '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'; then
sed -i "s/^/${SHORT_HOSTNAME}|/" "${DB_FILE}"
sed -i "s/^/${HOSTNAME}|/" "${DB_FILE}"
print_ok "Database migrated from v0.0.2"
fi
}
Expand Down

0 comments on commit d184d2d

Please sign in to comment.