From 84bad159a3666f65fd030ba327f9945a20a6031a Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Mon, 25 Sep 2023 19:27:53 -0500 Subject: [PATCH] feat: add git clone module (#46) * feat: add `git-clone` module * Fix script expansion for personalize * Update README --- .icons/git.svg | 3 +++ git-clone/README.md | 38 ++++++++++++++++++++++++++++++++++++++ git-clone/main.tf | 37 +++++++++++++++++++++++++++++++++++++ git-clone/run.sh | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 .icons/git.svg create mode 100644 git-clone/README.md create mode 100644 git-clone/main.tf create mode 100755 git-clone/run.sh diff --git a/.icons/git.svg b/.icons/git.svg new file mode 100644 index 0000000..ceef116 --- /dev/null +++ b/.icons/git.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/git-clone/README.md b/git-clone/README.md new file mode 100644 index 0000000..b89c47e --- /dev/null +++ b/git-clone/README.md @@ -0,0 +1,38 @@ +--- +display_name: Git Clone +description: Clone a Git repository by URL and skip if it exists. +icon: ../.icons/git.svg +maintainer_github: coder +verified: true +tags: [git, helper] +--- +# Git Clone + +This module allows you to automatically clone a repository by URL and skip if it exists in the path provided. + +```hcl +module "git-clone" { + source = "https://registry.coder.com/modules/git-clone" + url = "https://github.com/coder/coder" +} +``` + +To use with [Git Authentication](https://coder.com/docs/v2/latest/admin/git-providers), add the provider by ID to your template: + +```hcl +data "coder_git_auth" "github" { + id = "github" +} +``` + +## Examples + +### Custom Path + +```hcl +module "git-clone" { + source = "https://registry.coder.com/modules/git-clone" + url = "https://github.com/coder/coder" + path = "~/projects/coder/coder" +} +``` diff --git a/git-clone/main.tf b/git-clone/main.tf new file mode 100644 index 0000000..e1e9be4 --- /dev/null +++ b/git-clone/main.tf @@ -0,0 +1,37 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + coder = { + source = "coder/coder" + version = ">= 0.11" + } + } +} + +variable "url" { + description = "The URL of the Git repository." + type = string +} + +variable "path" { + default = "" + description = "The path to clone the repository. Defaults to \"$HOME/\"." + type = string +} + +variable "agent_id" { + description = "The ID of a Coder agent." + type = string +} + +resource "coder_script" "git_clone" { + agent_id = var.agent_id + display_name = "Git Clone" + icon = "/icons/git.svg" + script = templatefile("${path.module}/run.sh", { + CLONE_PATH: var.path != "" ? var.path : join("/", ["~", basename(var.url)]), + REPO_URL: var.url, + }) + run_on_start = true +} diff --git a/git-clone/run.sh b/git-clone/run.sh new file mode 100755 index 0000000..1fee8da --- /dev/null +++ b/git-clone/run.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env sh + +REPO_URL="${REPO_URL}" +CLONE_PATH="${CLONE_PATH}" +# Expand home if it's specified! +CLONE_PATH="$${CLONE_PATH/#\~/$${HOME}}" + +# Check if the variable is empty... +if [ -z "$REPO_URL" ]; then + echo "No repository specified!" + exit 1 +fi + +# Check if the variable is empty... +if [ -z "$CLONE_PATH" ]; then + echo "No clone path specified!" + exit 1 +fi + +# Check if `git` is installed... +if ! command -v git >/dev/null; then + echo "Git is not installed!" + exit 1 +fi + +# Check if the directory exists... +if [ ! -d "$CLONE_PATH" ]; then + echo "Creating directory $CLONE_PATH..." + mkdir -p "$CLONE_PATH" +else + echo "$CLONE_PATH already exists, skipping clone!" + exit 0 +fi + +# Clone the repository... +echo "Cloning $REPO_URL to $CLONE_PATH..." +git clone "$REPO_URL" "$CLONE_PATH" +