This repository was archived by the owner on May 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
feat(devcontainers-cli): add devcontainers-cli module #425
Merged
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0c75144
add devcontainers-cli module
defelmnq 6f51315
small fixes on readme and test name
defelmnq cda311d
run prettier
defelmnq d1684e2
fmt
defelmnq f0b707a
lint
defelmnq 0c086c6
update-version
defelmnq 7d7ae8b
fix tests and add more package manager
defelmnq f269de5
add docker detection and lint
defelmnq 4f7cd1e
improve log and docker handling
defelmnq 47e28bc
fix missing tests
defelmnq 67dd5ea
Update devcontainers-cli/run.sh
defelmnq 8936b9a
Update devcontainers-cli/run.sh
defelmnq 89cca9c
fix tests due to log changes
defelmnq 0e188a0
Merge branch 'main' into vvi-16432
matifali e6b4ac7
update version
matifali 1928e96
Fix typo and clarify npm requirement wording
matifali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
display_name: devcontainers-cli | ||
description: devcontainers-cli module provides an easy way to install @devcontainers/cli into a workspace | ||
icon: ../.icons/devcontainers.svg | ||
verified: true | ||
tags: [devcontainers] | ||
--- | ||
|
||
# devcontainers-cli | ||
|
||
The devcontainers-cli module provides an easy way to install @devcontainers/cli into a workspace. It can be used within any workspace as it runs only if | ||
@devcontainers/cli is not installed yet. | ||
npm is required and should be installed in order for the module to work. | ||
|
||
|
||
## Examples | ||
|
||
```tf | ||
module "devcontainers-cli" { | ||
source = "registry.coder.com/modules/devcontainers-cli/coder" | ||
version = "1.0.0" | ||
agent_id = coder_agent.example.id | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { | ||
execContainer, | ||
executeScriptInContainer, | ||
findResourceInstance, | ||
runContainer, | ||
runTerraformApply, | ||
runTerraformInit, | ||
testRequiredVariables, | ||
type TerraformState, | ||
} from "../test"; | ||
|
||
const executeScriptInContainerWithNPM = async ( | ||
state: TerraformState, | ||
image: string, | ||
shell = "sh", | ||
): Promise<{ | ||
exitCode: number; | ||
stdout: string[]; | ||
stderr: string[]; | ||
}> => { | ||
const instance = findResourceInstance(state, "coder_script"); | ||
const id = await runContainer(image); | ||
const respPipx = await execContainer(id, [shell, "-c", "apk add nodejs npm"]); | ||
const resp = await execContainer(id, [shell, "-c", instance.script]); | ||
const stdout = resp.stdout.trim().split("\n"); | ||
const stderr = resp.stderr.trim().split("\n"); | ||
return { | ||
exitCode: resp.exitCode, | ||
stdout, | ||
stderr, | ||
}; | ||
}; | ||
|
||
describe("devcontainers-cli", async () => { | ||
await runTerraformInit(import.meta.dir); | ||
|
||
testRequiredVariables(import.meta.dir, { | ||
agent_id: "some-agent-id", | ||
}); | ||
|
||
it("misses npm", async () => { | ||
const state = await runTerraformApply(import.meta.dir, { | ||
agent_id: "some-agent-id", | ||
}); | ||
const output = await executeScriptInContainer(state, "alpine"); | ||
expect(output.exitCode).toBe(1); | ||
expect(output.stdout).toEqual([ | ||
"Installing @devcontainers/cli ...", | ||
"npm is not installed, please install npm first", | ||
]); | ||
}); | ||
|
||
it("installs devcontainers-cli", async () => { | ||
const state = await runTerraformApply(import.meta.dir, { | ||
agent_id: "some-agent-id", | ||
}); | ||
|
||
const output = await executeScriptInContainerWithNPM(state, "alpine"); | ||
expect(output.exitCode).toBe(0); | ||
|
||
expect(output.stdout[0]).toEqual("Installing @devcontainers/cli ..."); | ||
expect(output.stdout[1]).toEqual("Running npm install -g @devcontainers/cli ..."); | ||
expect(output.stdout[4]).toEqual("🥳 @devcontainers/cli has been installed !"); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
terraform { | ||
required_version = ">= 1.0" | ||
|
||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = ">= 0.17" | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
variable "agent_id" { | ||
type = string | ||
description = "The ID of a Coder agent." | ||
} | ||
|
||
resource "coder_script" "devcontainers-cli" { | ||
agent_id = var.agent_id | ||
display_name = "devcontainers-cli" | ||
icon = "/icon/devcontainers.svg" | ||
script = templatefile("${path.module}/run.sh", {}) | ||
run_on_start = true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env sh | ||
|
||
echo "Installing @devcontainers/cli ..." | ||
|
||
# If @devcontainers/cli is already installed, we can skip | ||
if command -v devcontainers > /dev/null 2>&1; then | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
echo "🥳 @devcontainers/cli is already installed" | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exit 1 | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fi | ||
|
||
# If npm is not installed, we should skip | ||
if ! command -v npm > /dev/null 2>&1; then | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
echo "npm is not installed, please install npm first" | ||
exit 1 | ||
fi | ||
|
||
# If @devcontainers/cli is not installed, we should install it | ||
echo "Running npm install -g @devcontainers/cli ..." | ||
npm install -g @devcontainers/cli \ | ||
&& echo "🥳 @devcontainers/cli has been installed !" | ||
defelmnq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
exit 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.