#How to delete a file or directory on the host filesystem?

1 messages · Page 1 of 1 (latest)

vale trellis
#

I am playing a bit with Dagger to understand how it works. The following three functions respectively create, modify and delete a file called message.txt:

func (m *DaggerDraft) Create(ctx context.Context, projectDir *Directory) *Container {
    return dag.Container().
        From("alpine:3.19").
        WithDirectory("/project", projectDir).
        WithWorkdir("/project").
        WithExec([]string{"/bin/sh", "-c", "echo hello > message.txt"})
}

func (m *DaggerDraft) Modify(ctx context.Context, projectDir *Directory) *Container {
    return dag.Container().
        From("alpine:3.19").
        WithDirectory("/project", projectDir).
        WithWorkdir("/project").
        WithExec([]string{"/bin/sh", "-c", "echo HELLO > message.txt"})
}

func (m *DaggerDraft) Delete(ctx context.Context, projectDir *Directory) *Container {
    return dag.Container().
        From("alpine:3.19").
        WithDirectory("/project", projectDir).
        WithWorkdir("/project").
        WithExec([]string{"/bin/sh", "-c", "rm message.txt"})
}

If I call Dagger like below, it will create message.txt on the host, modify it on the host but not delete it on the host. Why is that?

# dagger version: dagger v0.10.2 (registry.dagger.io/engine) darwin/amd64
dagger call create --project-dir . directory --path . export --path .
dagger call modify --project-dir . directory --path . export --path .
dagger call delete --project-dir . directory --path . export --path .

Ultimately, I want to be able to run a container that removes and regenerates a directory on the host filesystem.

shadow aspen
#

Yeah that's just the way export works currently, it will export the files that exist in the directory to the host but it won't touch any files that already exist. I think that's the right default behavior (fairly often you just want to merge in new/changed files) but definitely see how it would be useful to have an option to replace all contents of the host directory such that it exactly matches the directory being exported.

I think we could add a flag for this pretty easily like dagger call delete --project-dir . directory --path . export --path . --merge=false

#

This may be so trivial that I can just open a PR quick, but otherwise will put an issue in our backlog

shadow aspen
vale trellis
#

One use case I have in mind is to remove and recreate a sub directory of a project. That could for example be the manifests directory in the kube-prometheus project. Today, that is done by running the script https://github.com/prometheus-operator/kube-prometheus/blob/main/build.sh which relies on CLI tools (jsonnet, jsonnet-bundler and gojsontoyaml) that may as well be in a container.

GitHub

Use Prometheus to monitor Kubernetes and applications running on Kubernetes - prometheus-operator/kube-prometheus

shadow aspen
#

Just FYI this was fixed in the latest v0.10.3 release, syntax we settled on is dagger call ... export --path . --wipe=true to include deletions

cursive raptor
#

Oh I didn't know we had that! Probably something to add to the user manual cc @waxen notch