From 236022f870f2ff3a847dd0e51d38adb0ec869ba4 Mon Sep 17 00:00:00 2001 From: megumin Date: Sun, 1 Sep 2024 12:16:05 +0100 Subject: [PATCH] feat(git-clone): custom destination folder name (#287) --- git-clone/README.md | 17 +++++++++++++++++ git-clone/main.test.ts | 16 ++++++++++++++++ git-clone/main.tf | 8 +++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/git-clone/README.md b/git-clone/README.md index 255b3f1..5efc50e 100644 --- a/git-clone/README.md +++ b/git-clone/README.md @@ -153,3 +153,20 @@ module "git-clone" { branch_name = "feat/example" } ``` + +## Git clone with different destination folder + +By default, the repository will be cloned into a folder matching the repository name. You can use the `folder_name` attribute to change the name of the destination folder to something else. + +For example, this will clone into the `~/projects/coder/coder-dev` folder: + +```tf +module "git-clone" { + source = "registry.coder.com/modules/git-clone/coder" + version = "1.0.12" + agent_id = coder_agent.example.id + url = "https://github.com/coder/coder" + folder_name = "coder-dev" + base_dir = "~/projects/coder" +} +``` diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index 87b0e4a..9fbd202 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -79,6 +79,22 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual(""); }); + it("repo_dir should match base_dir/folder_name", async () => { + const url = "git@github.com:coder/coder.git"; + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + base_dir: "/tmp", + folder_name: "foo", + url, + }); + expect(state.outputs.repo_dir.value).toEqual("/tmp/foo"); + expect(state.outputs.folder_name.value).toEqual("foo"); + expect(state.outputs.clone_url.value).toEqual(url); + const https_url = "https://github.com/coder/coder.git"; + expect(state.outputs.web_url.value).toEqual(https_url); + expect(state.outputs.branch_name.value).toEqual(""); + }); + it("branch_name should not include query string", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", diff --git a/git-clone/main.tf b/git-clone/main.tf index 4af5000..0295444 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -50,6 +50,12 @@ variable "branch_name" { default = "" } +variable "folder_name" { + description = "The destination folder to clone the repository into." + type = string + default = "" +} + locals { # Remove query parameters and fragments from the URL url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") @@ -64,7 +70,7 @@ locals { # Extract the branch name from the URL branch_name = var.branch_name == "" && local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : var.branch_name # Extract the folder name from the URL - folder_name = replace(basename(local.clone_url), ".git", "") + folder_name = var.folder_name == "" ? replace(basename(local.clone_url), ".git", "") : var.folder_name # Construct the path to clone the repository clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name]) # Construct the web URL