Created
June 12, 2025 05:31
-
-
Save westonplatter/2df32c9522045bb958f5b4ea45cd155a to your computer and use it in GitHub Desktop.
Dagger - list files in container passed as arg
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
from typing import Annotated | |
import dagger | |
from dagger import Doc, dag, field, function, object_type | |
@object_type | |
class Workspace: | |
"""A module for editing code""" | |
source: Annotated[dagger.Directory, Doc("the workspace source code")] = field() | |
@function | |
async def build_container(self) -> dagger.Container: | |
"""Build the Docker container from Dockerfile with aqua, trunk, and audit tools""" | |
return dag.container().build(self.source) | |
@function | |
async def clone_repo(self, | |
repo_url: str, | |
github_token: dagger.Secret | None = None, | |
use_ssh: bool = False, | |
) -> dagger.Directory: | |
"""Clone a git repository (supports private repos with GitHub token or SSH)""" | |
if use_ssh: | |
# Use SSH authentication - requires SSH keys to be available | |
container = ( | |
dag.container() | |
.from_("alpine/git:latest") | |
.with_exec(["apk", "add", "--no-cache", "openssh-client"]) | |
.with_exec(["mkdir", "-p", "/root/.ssh"]) | |
.with_exec(["ssh-keyscan", "-H", "github.com", ">>", "/root/.ssh/known_hosts"]) | |
.with_mounted_file("/root/.ssh/id_rsa", dag.host().file(f"{dag.host().env_variable('HOME')}/.ssh/id_rsa")) | |
.with_exec(["chmod", "600", "/root/.ssh/id_rsa"]) | |
.with_exec(["git", "clone", repo_url, "/repo"]) | |
) | |
return container.directory("/repo") | |
else: | |
# Use HTTPS with token | |
git_ref = dag.git(repo_url) | |
if github_token: | |
git_ref = git_ref.with_auth_token(github_token) | |
return git_ref.head().tree() | |
@function | |
async def build_env(self, repo_url: str, github_token: dagger.Secret | None = None, use_ssh: bool = False) -> dagger.Container: | |
"""Build the dev environment: build the container, clone the repo, and mount it for development.""" | |
repo_dir = await self.clone_repo(repo_url, github_token, use_ssh) | |
container = await self.build_container() | |
# Add a custom function to the container for listing files | |
enhanced_container = ( | |
container | |
.with_directory("/root/workspace/target-repo", repo_dir) | |
.with_workdir("/root/workspace/target-repo") | |
) | |
return enhanced_container | |
@function | |
async def list_files_in_container(self, container: dagger.Container) -> str: | |
"""List files in a container using ls command""" | |
return await container.with_exec(["ls", "-la", "/root/workspace/target-repo"]).stdout() | |
@function | |
async def build_env_and_list_files(self, repo_url: str, github_token: dagger.Secret | None = None, use_ssh: bool = False) -> str: | |
"""Build the dev environment and list files in one step""" | |
container = await self.build_env(repo_url, github_token, use_ssh) | |
return await container.with_exec(["ls", "-la", "/root/workspace/target-repo"]).stdout() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment