diff --git a/filebrowser/README.md b/filebrowser/README.md index 9a08398..134eb52 100644 --- a/filebrowser/README.md +++ b/filebrowser/README.md @@ -31,3 +31,13 @@ module "filebrowser" { folder = "/home/coder/project" } ``` + +### Specify location of `filebrowser.db` + +```hcl +module "filebrowser" { + source = "https://registry.coder.com/modules/filebrowser" + agent_id = coder_agent.example.id + database_path = ".config/filebrowser.db" +} +``` diff --git a/filebrowser/main.test.ts b/filebrowser/main.test.ts new file mode 100644 index 0000000..7541e65 --- /dev/null +++ b/filebrowser/main.test.ts @@ -0,0 +1,91 @@ +import { describe, expect, it } from "bun:test"; +import { + executeScriptInContainer, + runTerraformApply, + runTerraformInit, + testRequiredVariables, +} from "../test"; + +describe("filebrowser", async () => { + await runTerraformInit(import.meta.dir); + + testRequiredVariables(import.meta.dir, { + agent_id: "foo", + }); + + it("fails with wrong database_path", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + database_path: "nofb", + }).catch((e) => { + if (!e.message.startsWith("\nError: Invalid value for variable")) { + throw e; + } + }); + }); + + it("runs with default", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + }); + const output = await executeScriptInContainer(state, "alpine"); + expect(output.exitCode).toBe(0); + expect(output.stdout).toEqual([ + "\u001b[0;1mInstalling filebrowser ", + "", + "🥳 Installation comlete! ", + "", + "👷 Starting filebrowser in background... ", + "", + "📂 Serving /root at http://localhost:13339 ", + "", + "Running 'filebrowser --noauth --root /root --port 13339' ", + "", + "📝 Logs at /tmp/filebrowser.log", + ]); + }); + + it("runs with database_path var", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + database_path: ".config/filebrowser.db", + }); + const output = await executeScriptInContainer(state, "alpine"); + expect(output.exitCode).toBe(0); + expect(output.stdout).toEqual([ + "\u001b[0;1mInstalling filebrowser ", + "", + "🥳 Installation comlete! ", + "", + "👷 Starting filebrowser in background... ", + "", + "📂 Serving /root at http://localhost:13339 ", + "", + "Running 'filebrowser --noauth --root /root --port 13339 -d .config/filebrowser.db' ", + "", + "📝 Logs at /tmp/filebrowser.log", + ]); + }); + + it("runs with folder var", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + folder: "/home/coder/project", + }); + const output = await executeScriptInContainer(state, "alpine"); + expect(output.exitCode).toBe(0); + expect(output.stdout).toEqual([ + "\u001B[0;1mInstalling filebrowser ", + "", + "🥳 Installation comlete! ", + "", + "👷 Starting filebrowser in background... ", + "", + "📂 Serving /home/coder/project at http://localhost:13339 ", + "", + "Running 'filebrowser --noauth --root /home/coder/project --port 13339' ", + "", + "📝 Logs at /tmp/filebrowser.log", + ]); + }); +}); diff --git a/filebrowser/main.tf b/filebrowser/main.tf index e624004..f479488 100644 --- a/filebrowser/main.tf +++ b/filebrowser/main.tf @@ -9,12 +9,22 @@ terraform { } } -# Add required variables for your modules and remove any unneeded variables variable "agent_id" { type = string description = "The ID of a Coder agent." } +variable "database_path" { + type = string + description = "The path to the filebrowser database." + default = "filebrowser.db" + validation { + # Ensures path leads to */filebrowser.db + condition = can(regex(".*filebrowser\\.db$", var.database_path)) + error_message = "The database_path must end with 'filebrowser.db'." + } +} + variable "log_path" { type = string description = "The path to log filebrowser to." @@ -42,6 +52,7 @@ resource "coder_script" "filebrowser" { PORT : var.port, FOLDER : var.folder, LOG_PATH : var.log_path, + DB_PATH : var.database_path }) run_on_start = true } diff --git a/filebrowser/run.sh b/filebrowser/run.sh index 91bf368..ffbee0f 100644 --- a/filebrowser/run.sh +++ b/filebrowser/run.sh @@ -12,10 +12,15 @@ printf "👷 Starting filebrowser in background... \n\n" ROOT_DIR=${FOLDER} ROOT_DIR=$${ROOT_DIR/\~/$HOME} +DB_FLAG="" +if [ "${DB_PATH}" != "filebrowser.db" ]; then + DB_FLAG=" -d ${DB_PATH}" +fi + printf "📂 Serving $${ROOT_DIR} at http://localhost:${PORT} \n\n" -printf "Running 'filebrowser --noauth --root $ROOT_DIR --port ${PORT}' \n\n" +printf "Running 'filebrowser --noauth --root $ROOT_DIR --port ${PORT}$${DB_FLAG}' \n\n" -filebrowser --noauth --root $ROOT_DIR --port ${PORT} >${LOG_PATH} 2>&1 & +filebrowser --noauth --root $ROOT_DIR --port ${PORT}$${DB_FLAG} >${LOG_PATH} 2>&1 & printf "📝 Logs at ${LOG_PATH} \n\n"