From 770acd3ce26cd0fdd3978877254801e8e6e0c4b1 Mon Sep 17 00:00:00 2001 From: Helder Correia <174525+helderco@users.noreply.github.com> Date: Fri, 25 Mar 2022 11:50:47 -0100 Subject: [PATCH] Expand user home dir in client filesystem Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com> --- docs/drafts/plans/docker-cli-load/ssh.cue | 8 ++++---- docs/drafts/plans/docker-cli-run/ssh.cue | 8 ++++---- plan/task/clientfilesystemread.go | 6 +----- plan/task/clientfilesystemwrite.go | 3 +-- plan/task/util.go | 10 ++++++++++ 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/docs/drafts/plans/docker-cli-load/ssh.cue b/docs/drafts/plans/docker-cli-load/ssh.cue index e1423e1b..0fb00044 100644 --- a/docs/drafts/plans/docker-cli-load/ssh.cue +++ b/docs/drafts/plans/docker-cli-load/ssh.cue @@ -8,8 +8,8 @@ import ( dagger.#Plan & { client: filesystem: { - "/home/user/.ssh/id_rsa": read: contents: dagger.#Secret - "/home/user/.ssh/known_hosts": read: contents: dagger.#Secret + "~/.ssh/id_rsa": read: contents: dagger.#Secret + "~/.ssh/known_hosts": read: contents: dagger.#Secret } actions: { @@ -22,8 +22,8 @@ dagger.#Plan & { tag: "myimage:v2" host: "ssh://root@93.184.216.34" ssh: { - key: client.filesystem."/home/user/.ssh/id_rsa".read.contents - knownHosts: client.filesystem."/home/user/.ssh/known_hosts".read.contents + key: client.filesystem."~/.ssh/id_rsa".read.contents + knownHosts: client.filesystem."~/.ssh/known_hosts".read.contents } } } diff --git a/docs/drafts/plans/docker-cli-run/ssh.cue b/docs/drafts/plans/docker-cli-run/ssh.cue index 58e9861f..43c8083b 100644 --- a/docs/drafts/plans/docker-cli-run/ssh.cue +++ b/docs/drafts/plans/docker-cli-run/ssh.cue @@ -7,15 +7,15 @@ import ( dagger.#Plan & { client: filesystem: { - "/home/user/.ssh/id_rsa": read: contents: dagger.#Secret - "/home/user/.ssh/known_hosts": read: contents: dagger.#Secret + "~/.ssh/id_rsa": read: contents: dagger.#Secret + "~/.ssh/known_hosts": read: contents: dagger.#Secret } actions: run: cli.#Run & { host: "ssh://root@93.184.216.34" ssh: { - key: client.filesystem."/home/user/.ssh/id_rsa".read.contents - knownHosts: client.filesystem."/home/user/.ssh/known_hosts".read.contents + key: client.filesystem."~/.ssh/id_rsa".read.contents + knownHosts: client.filesystem."~/.ssh/known_hosts".read.contents } command: name: "info" } diff --git a/plan/task/clientfilesystemread.go b/plan/task/clientfilesystemread.go index 211b4572..d019be15 100644 --- a/plan/task/clientfilesystemread.go +++ b/plan/task/clientfilesystemread.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "os" - "path/filepath" "cuelang.org/go/cue" "github.com/moby/buildkit/client/llb" @@ -66,10 +65,7 @@ func (t clientFilesystemReadTask) parsePath(v *compiler.Value) (path string, err return } - path, err = filepath.Abs(path) - if err != nil { - return - } + path, err = clientFilePath(path) return } diff --git a/plan/task/clientfilesystemwrite.go b/plan/task/clientfilesystemwrite.go index 67f4c967..1ccbb325 100644 --- a/plan/task/clientfilesystemwrite.go +++ b/plan/task/clientfilesystemwrite.go @@ -5,7 +5,6 @@ import ( "fmt" "io/fs" "os" - "path/filepath" "cuelang.org/go/cue" bk "github.com/moby/buildkit/client" @@ -28,7 +27,7 @@ func (t clientFilesystemWriteTask) Run(ctx context.Context, pctx *plancontext.Co return nil, err } - path, err = filepath.Abs(path) + path, err = clientFilePath(path) if err != nil { return nil, err } diff --git a/plan/task/util.go b/plan/task/util.go index 19d1f312..504fb72e 100644 --- a/plan/task/util.go +++ b/plan/task/util.go @@ -2,7 +2,9 @@ package task import ( "fmt" + "path/filepath" + "github.com/mitchellh/go-homedir" "github.com/moby/buildkit/client/llb" "go.dagger.io/dagger/compiler" ) @@ -16,3 +18,11 @@ func vertexNamef(v *compiler.Value, format string, a ...interface{}) string { func withCustomName(v *compiler.Value, format string, a ...interface{}) llb.ConstraintsOpt { return llb.WithCustomName(vertexNamef(v, format, a...)) } + +func clientFilePath(path string) (string, error) { + expanded, err := homedir.Expand(path) + if err != nil { + return "", err + } + return filepath.Abs(expanded) +}