From b924c508581098010d1ca893a3c0f6310d39f2d6 Mon Sep 17 00:00:00 2001 From: cotore-game <102813037+cotore-game@users.noreply.github.com> Date: Fri, 7 Aug 2026 04:14:40 +0900 Subject: [PATCH] feat: add settings reset command --- .github/workflows/release.yml | 5 +-- .github/workflows/shell-tests.yml | 4 +-- install.sh | 1 + locales/en.json | 9 ++++- locales/ja.json | 9 ++++- mcserver-kit | 6 ++++ reset.sh | 58 +++++++++++++++++++++++++++++++ tests/run-tests.sh | 22 ++++++++++++ 8 files changed, 108 insertions(+), 6 deletions(-) create mode 100755 reset.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d5361cc..fbe9a83 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,10 +22,10 @@ jobs: - name: Run checks run: | - bash -n mcserver-kit new-minecraft-server.sh setup.sh install.sh uninstall.sh tests/run-tests.sh + bash -n mcserver-kit new-minecraft-server.sh setup.sh reset.sh install.sh uninstall.sh tests/run-tests.sh python3 -m py_compile scripts/detect-world-version.py scripts/validate-locales.py python3 scripts/validate-locales.py - shellcheck -x mcserver-kit new-minecraft-server.sh setup.sh install.sh uninstall.sh tests/run-tests.sh + shellcheck -x mcserver-kit new-minecraft-server.sh setup.sh reset.sh install.sh uninstall.sh tests/run-tests.sh bash tests/run-tests.sh - name: Build release archive @@ -35,6 +35,7 @@ jobs: new-minecraft-server.sh \ mcserver-kit \ setup.sh \ + reset.sh \ install.sh \ uninstall.sh \ config.example.yml \ diff --git a/.github/workflows/shell-tests.yml b/.github/workflows/shell-tests.yml index e626758..12d4e27 100644 --- a/.github/workflows/shell-tests.yml +++ b/.github/workflows/shell-tests.yml @@ -24,7 +24,7 @@ jobs: run: sudo apt-get update && sudo apt-get install --yes shellcheck zip unzip - name: Check Bash syntax - run: bash -n mcserver-kit new-minecraft-server.sh setup.sh install.sh uninstall.sh tests/run-tests.sh + run: bash -n mcserver-kit new-minecraft-server.sh setup.sh reset.sh install.sh uninstall.sh tests/run-tests.sh - name: Check Python syntax run: python3 -m py_compile scripts/detect-world-version.py scripts/validate-locales.py @@ -33,7 +33,7 @@ jobs: run: python3 scripts/validate-locales.py - name: Run ShellCheck - run: shellcheck -x mcserver-kit new-minecraft-server.sh setup.sh install.sh uninstall.sh tests/run-tests.sh + run: shellcheck -x mcserver-kit new-minecraft-server.sh setup.sh reset.sh install.sh uninstall.sh tests/run-tests.sh - name: Run specification tests run: bash tests/run-tests.sh diff --git a/install.sh b/install.sh index b568d0c..dd7529b 100755 --- a/install.sh +++ b/install.sh @@ -91,6 +91,7 @@ main() { install -m 755 "${source_dir}/new-minecraft-server.sh" "${INSTALL_DIR}/new-minecraft-server.sh" install -m 755 "${source_dir}/mcserver-kit" "${INSTALL_DIR}/mcserver-kit" install -m 755 "${source_dir}/setup.sh" "${INSTALL_DIR}/setup.sh" + install -m 755 "${source_dir}/reset.sh" "${INSTALL_DIR}/reset.sh" install -m 755 "${source_dir}/uninstall.sh" "${INSTALL_DIR}/uninstall.sh" install -m 644 "${source_dir}/config.example.yml" "${INSTALL_DIR}/config.example.yml" install -m 644 "${source_dir}/compose-example.yaml" "${INSTALL_DIR}/compose-example.yaml" diff --git a/locales/en.json b/locales/en.json index 169de59..7badfcd 100644 --- a/locales/en.json +++ b/locales/en.json @@ -4,6 +4,7 @@ "cli.commands": "Commands:", "cli.create": "Create a new server interactively (default command)", "cli.setup": "Configure Owner, EULA, MCID templates, Playit, and defaults", + "cli.reset": "Delete settings and MCID templates but keep servers and the program", "cli.uninstall": "Remove the program but keep settings and MCID templates", "cli.uninstall_purge": "Remove the program, settings, and MCID templates", "cli.help": "Show this help", @@ -31,5 +32,11 @@ "input.yes_no": "Please enter y or n.\n", "input.mcid": "MCID", "input.mcid_rules": "MCID must be 3–16 letters, numbers, or underscores.\n", - "input.one_per_line": "Enter one per line and press Enter on an empty line to finish.\n" + "input.one_per_line": "Enter one per line and press Enter on an empty line to finish.\n", + "reset.usage": "Usage: mcserver-kit reset [--yes]\n", + "reset.unsafe": "Refusing to reset an unsafe path.\n", + "reset.summary": "The following settings will be deleted:\n %s\n %s\nCreated servers, worlds, and the installed program will not be deleted.\n", + "reset.confirm": "Continue? [y/N]: ", + "reset.cancelled": "Reset cancelled.\n", + "reset.done": "Settings and MCID templates were deleted. Run mcserver-kit setup to configure them again.\n" } diff --git a/locales/ja.json b/locales/ja.json index 6cc4f23..0f161b9 100644 --- a/locales/ja.json +++ b/locales/ja.json @@ -4,6 +4,7 @@ "cli.commands": "コマンド:", "cli.create": "対話形式で新しいサーバーを作成します(省略時の動作)", "cli.setup": "Owner、EULA、MCIDテンプレート、Playit等を設定します", + "cli.reset": "設定とMCIDテンプレートだけを削除し、本体とサーバーは保持します", "cli.uninstall": "本体を削除し、設定とMCIDテンプレートは保持します", "cli.uninstall_purge": "本体、設定、MCIDテンプレートをすべて削除します", "cli.help": "このヘルプを表示します", @@ -31,5 +32,11 @@ "input.yes_no": "yまたはnを入力してください。\n", "input.mcid": "MCID", "input.mcid_rules": "MCIDは英数字と_の3~16文字です。\n", - "input.one_per_line": "1行に1つ入力し、空Enterで終了します。\n" + "input.one_per_line": "1行に1つ入力し、空Enterで終了します。\n", + "reset.usage": "使い方: mcserver-kit reset [--yes]\n", + "reset.unsafe": "危険なパスが指定されているためリセットを拒否しました。\n", + "reset.summary": "次の設定を削除します:\n %s\n %s\n作成済みサーバー、ワールド、インストール済み本体は削除しません。\n", + "reset.confirm": "続行しますか? [y/N]: ", + "reset.cancelled": "リセットをキャンセルしました。\n", + "reset.done": "設定とMCIDテンプレートを削除しました。mcserver-kit setupで再設定できます。\n" } diff --git a/mcserver-kit b/mcserver-kit index 2c0a7cb..97fbb59 100755 --- a/mcserver-kit +++ b/mcserver-kit @@ -27,6 +27,7 @@ show_help() { cat < [command] mcserver-kit --help @@ -36,6 +37,7 @@ HELP printf '\n' printf ' %-20s %s\n' create "$(tr cli.create)" printf ' %-20s %s\n' setup "$(tr cli.setup)" + printf ' %-20s %s\n' reset "$(tr cli.reset)" printf ' %-20s %s\n' uninstall "$(tr cli.uninstall)" printf ' %-20s %s\n' 'uninstall --purge' "$(tr cli.uninstall_purge)" printf ' %-20s %s\n' help "$(tr cli.help)" @@ -58,6 +60,10 @@ case "$command_name" in shift exec "${SCRIPT_DIR}/setup.sh" "$@" ;; + reset) + shift + exec "${SCRIPT_DIR}/reset.sh" "$@" + ;; uninstall) shift exec "${SCRIPT_DIR}/uninstall.sh" "$@" diff --git a/reset.sh b/reset.sh new file mode 100755 index 0000000..40dbd19 --- /dev/null +++ b/reset.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# `tr` is the repository translation helper, and load_messages takes no CLI args. +# shellcheck disable=SC2020,SC2119 +set -Eeuo pipefail + +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +CONFIG_FILE="${MCSERVER_KIT_CONFIG:-${HOME}/.config/mcserver-compose-kit/config.yml}" +TEMPLATE_DIR="${MCSERVER_KIT_MCID_TEMPLATE_DIR:-${HOME}/.config/mcserver-compose-kit/mcid-templates}" + +# shellcheck source=scripts/i18n.sh +source "${SCRIPT_DIR}/scripts/i18n.sh" +load_messages + +resolve_parent() { + local path="$1" + local parent + parent="$(dirname -- "$path")" + if [[ -d "$parent" ]]; then + (cd -- "$parent" && pwd) + else + printf '%s' "$parent" + fi +} + +confirm=false +case "${1-}" in + '') ;; + --yes | -y) confirm=true ;; + *) + tr reset.usage >&2 + exit 2 + ;; +esac + +config_parent="$(resolve_parent "$CONFIG_FILE")" +template_parent="$(resolve_parent "$TEMPLATE_DIR")" +for protected_path in '' / "$HOME"; do + if [[ "$config_parent" == "$protected_path" || "$template_parent" == "$protected_path" ]]; then + tr reset.unsafe >&2 + exit 1 + fi +done + +tr reset.summary "$CONFIG_FILE" "$TEMPLATE_DIR" +if [[ "$confirm" != 'true' ]]; then + read -r -p "$(tr reset.confirm)" answer + case "${answer,,}" in + y | yes) ;; + *) + tr reset.cancelled + exit 0 + ;; + esac +fi + +rm -f -- "$CONFIG_FILE" +rm -rf -- "$TEMPLATE_DIR" +tr reset.done diff --git a/tests/run-tests.sh b/tests/run-tests.sh index 551b1c2..125e11b 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -289,6 +289,27 @@ test_setup_command() { assert_equal 'absent' "$(! grep -q 'playit-secret-for-test' "$setup_log" && printf absent)" 'setup does not print the Playit secret' } +test_reset_command() { + local temp_dir="$1" + local config_dir="${temp_dir}/reset/config" + local config_file="${config_dir}/config.yml" + local template_dir="${config_dir}/mcid-templates" + local server_dir="${temp_dir}/reset/servers/keep-me" + + mkdir -p "$template_dir" "$server_dir" + : >"$config_file" + : >"${template_dir}/default.txt" + : >"${server_dir}/level.dat" + + MCSERVER_KIT_CONFIG="$config_file" \ + MCSERVER_KIT_MCID_TEMPLATE_DIR="$template_dir" \ + bash "${REPO_ROOT}/mcserver-kit" --lang en reset --yes >/dev/null + + assert_equal 'absent' "$([[ ! -f "$config_file" ]] && printf absent)" 'reset removes config.yml' + assert_equal 'absent' "$([[ ! -d "$template_dir" ]] && printf absent)" 'reset removes MCID templates' + assert_equal 'present' "$([[ -f "${server_dir}/level.dat" ]] && printf present)" 'reset preserves created servers and worlds' +} + main() { TEST_TEMP_DIR="$(mktemp -d)" trap cleanup EXIT @@ -302,6 +323,7 @@ main() { test_creation_flow "$TEST_TEMP_DIR" test_local_installation "$TEST_TEMP_DIR" test_setup_command "$TEST_TEMP_DIR" + test_reset_command "$TEST_TEMP_DIR" printf 'PASS: %d specification tests, %d skipped\n' "$tests_run" "$tests_skipped" }