#Git repository url to Directory in a function

1 messages · Page 1 of 1 (latest)

spark dust
#

Hey,

I would like to convert a repositoryUrl: string into a Directory within a function, how can I do that?

TS SDK

compact horizon
#

Hey @spark dust! What you are looking for is to clone the repository and obtain a Directory with the contents of the repo?

spark dust
#

Yes, I want the same behaviour as passing a repo url to a directory argument from the cli

hallow patrol
#

You can use:

dag.git("github.com/dagger/dagger").branch("main").tree()
#

I think an empty branch name should get you the default branch.

spark dust
#

Perfect, thank you!

compact horizon
# hallow patrol I think an empty branch name should get you the default branch.

It seems it does!

import { dag, Container, Directory, object, func } from "@dagger.io/dagger"

@object()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class TsExample {
  @func()
  clone(repositoryUrl: string): Promise<string> {
    let dir = dag.git(repositoryUrl).branch("").tree();
    return dag.container().from("alpine:latest").withDirectory("/dagger", dir).withExec(["ls", "-la", "/dagger"]).stdout()
  }
}
compact horizon
#

Hey @spark dust! I was just checking the docs because I remembered that we had some integration with automatically cloning repositories and giving directories to a function. In case this works for you, you can do the following:

  @func()
  contents(repository: Directory): Promise<string> {
    return dag.container().from("alpine:latest").withDirectory("/dagger", repository).withExec(["ls", "-la", "/dagger"]).stdout()
  }

And then call it like:

$ dagger call contents --repository=https://github.com/dagger/dagger#main
total 464
drwxr-xr-x   22 root     root          4096 Mar 19 13:14 .
drwxr-xr-x    1 root     root          4096 Mar 19 13:14 ..
drwxr-xr-x    3 root     root          4096 Mar 19 13:14 .changes
-rw-r--r--    2 root     root           962 Mar 19 13:14 .changie.yaml
-rw-r--r--    2 root     root            16 Mar 19 13:14 .dockerignore
drwxr-xr-x    8 root     root          4096 Mar 19 13:14 .git
drwxr-xr-x    3 root     root          4096 Mar 19 13:14 .github
-rw-r--r--    2 root     root           568 Mar 19 13:14 .gitignore
-rw-r--r--    2 root     root           776 Mar 19 13:14 .golangci.yml
-rw-r--r--    2 root     root           419 Mar 19 13:14 .goreleaser.common.yml
-rw-r--r--    2 root     root           620 Mar 19 13:14 .goreleaser.nightly.yml
-rw-r--r--    2 root     root          2822 Mar 19 13:14 .goreleaser.yml
-rw-r--r--    2 root     root           593 Mar 19 13:14 .markdownlint.yaml
-rw-r--r--    2 root     root           312 Mar 19 13:14 .readthedocs.yaml
-rw-r--r--    2 root     root         16214 Mar 19 13:14 CHANGELOG.md
-rw-r--r--    2 root     root          5161 Mar 19 13:14 CODE_OF_CONDUCT.md
-rw-r--r--    2 root     root         11869 Mar 19 13:14 CONTRIBUTING.md
-rw-r--r--    2 root     root         10758 Mar 19 13:14 LICENSE
-rw-r--r--    2 root     root           517 Mar 19 13:14 NOTICE
-rw-r--r--    2 root     root          2456 Mar 19 13:14 README.md
-rw-r--r--    2 root     root         20730 Mar 19 13:14 RELEASING.md
drwxr-xr-x    2 root     root          4096 Mar 19 13:14 analytics
drwxr-xr-x    2 root     root          4096 Mar 19 13:14 auth
drwxr-xr-x    2 root     root          4096 Mar 19 13:14 ci
drwxr-xr-x   13 root     root          4096 Mar 19 13:14 cmd
drwxr-xr-x    8 root     root          4096 Mar 19 13:14 core
drwxr-xr-x    9 root     root          4096 Mar 19 13:14 dagql
drwxr-xr-x    8 root     root          4096 Mar 19 13:14 docs
drwxr-xr-x    8 root     root          4096 Mar 19 13:14 engine
drwxr-xr-x    4 root     root          4096 Mar 19 13:14 examples
-rw-r--r--    2 root     root         14074 Mar 19 13:14 go.mod
-rw-r--r--    2 root     root        236369 Mar 19 13:14 go.sum
drwxr-xr-x    2 root     root          4096 Mar 19 13:14 hack
drwxr-xr-x    3 root     root          4096 Mar 19 13:14 helm
-rwxr-xr-x    2 root     root          8674 Mar 19 13:14 install.sh
drwxr-xr-x    7 root     root          4096 Mar 19 13:14 internal
drwxr-xr-x    3 root     root          4096 Mar 19 13:14 linters
drwxr-xr-x    3 root     root          4096 Mar 19 13:14 network
drwxr-xr-x   11 root     root          4096 Mar 19 13:14 sdk
drwxr-xr-x    2 root     root          4096 Mar 19 13:14 telemetry
drwxr-xr-x    2 root     root          4096 Mar 19 13:14 tracing

You could also send a subpath of the repository. Checkout the docs here for more details: https://docs.dagger.io/manuals/developer/typescript/292372/functions

Dagger Functions are regular code, written in a supported programming language, and running in containers. Dagger Functions let you encapsulate common operations or workflows into discrete units with clear inputs and outputs.