Skip to content

Commit ae0df99

Browse files
committed
Read files from the volume
Signed-off-by: David Gageot <[email protected]>
1 parent 7fa27dc commit ae0df99

File tree

7 files changed

+131
-15
lines changed

7 files changed

+131
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package commands
5+
6+
import (
7+
"context"
8+
"net"
9+
10+
"github.com/docker/labs-ai-tools-for-devs/pkg/paths"
11+
)
12+
13+
func dialVolumeContents(ctx context.Context) (net.Conn, error) {
14+
path, err := paths.GetVolumeContentsSocketPath()
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
dialer := net.Dialer{}
20+
return dialer.DialContext(ctx, "unix", path)
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package commands
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/Microsoft/go-winio"
8+
"github.com/docker/labs-ai-tools-for-devs/pkg/paths"
9+
)
10+
11+
func dialVolumeContents(ctx context.Context) (net.Conn, error) {
12+
path, err := paths.GetVolumeContentsSocketPath()
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
return winio.DialPipeContext(ctx, path)
18+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package commands
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net"
9+
"net/http"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func ReadFromVolume(ctx context.Context) *cobra.Command {
15+
return &cobra.Command{
16+
Use: "read-from-volume",
17+
Short: "Read a file from the extension's volume",
18+
Args: cobra.ExactArgs(1),
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
content, err := readConfig(ctx, args[0])
21+
if err != nil {
22+
return err
23+
}
24+
fmt.Print(content)
25+
return nil
26+
},
27+
}
28+
}
29+
30+
func readConfig(ctx context.Context, filename string) (string, error) {
31+
httpClient := &http.Client{
32+
Transport: &http.Transport{
33+
DialContext: func(ctx context.Context, _, _ string) (conn net.Conn, err error) {
34+
return dialVolumeContents(ctx)
35+
},
36+
},
37+
}
38+
39+
var content struct {
40+
Contents string `json:"contents"`
41+
}
42+
if err := query(ctx, httpClient, "GET", "/volume-file-content?volumeId=docker-prompts&targetPath="+filename, &content); err != nil {
43+
return "", err
44+
}
45+
46+
return content.Contents, nil
47+
}
48+
49+
func query(ctx context.Context, httpClient *http.Client, method string, endpoint string, v any) error {
50+
req, err := http.NewRequestWithContext(ctx, method, "http://localhost"+endpoint, nil)
51+
if err != nil {
52+
return err
53+
}
54+
req.Header.Set("X-DockerDesktop-Host", "vm.docker.internal")
55+
56+
response, err := httpClient.Do(req)
57+
if err != nil {
58+
return err
59+
}
60+
defer response.Body.Close()
61+
62+
buf, err := io.ReadAll(response.Body)
63+
if err != nil {
64+
return err
65+
}
66+
67+
if err := json.Unmarshal(buf, &v); err != nil {
68+
return err
69+
}
70+
return nil
71+
}

src/extension/host-binary/cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func main() {
2828
cmd.AddCommand(ListOAuthApps(ctx))
2929
cmd.AddCommand(DeriveSecret(ctx))
3030
cmd.AddCommand(commands.CurrentUser(ctx))
31+
cmd.AddCommand(commands.ReadFromVolume(ctx))
3132
if err := cmd.Execute(); err != nil {
3233
fmt.Println(err)
3334
os.Exit(1)

src/extension/host-binary/pkg/paths/paths_unix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ func GetToolsApiSocketPath() (string, error) {
1919
}
2020
return filepath.Join(dir, "tools.sock"), nil
2121
}
22+
23+
func GetVolumeContentsSocketPath() (string, error) {
24+
dir, err := dockerDesktopSocketDir()
25+
if err != nil {
26+
return "", err
27+
}
28+
return filepath.Join(dir, "volume-contents.sock"), nil
29+
}

src/extension/host-binary/pkg/paths/paths_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ func GetSecretsApiSocketPath() (string, error) {
99
func GetToolsApiSocketPath() (string, error) {
1010
return `//./pipe/dockerTools`, nil
1111
}
12+
13+
func GetVolumeContentsSocketPath() (string, error) {
14+
return `//./pipe/dockerVolumeContents`, nil
15+
}

src/extension/ui/src/utils/Files.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,14 @@ export const readFileInPromptsVolume = async (
5555
client: v1.DockerDesktopClient,
5656
path: string
5757
) => {
58-
return tryRunImageSync(
59-
client,
60-
[
61-
"--rm",
62-
"-v",
63-
"docker-prompts:/docker-prompts:ro",
64-
"--network=none",
65-
"-w",
66-
"/docker-prompts",
67-
BUSYBOX,
68-
"/bin/cat",
69-
`${path}`,
70-
],
71-
true
72-
);
58+
try {
59+
const result = await client.extension.host?.cli.exec("host-binary", ["read-from-volume", path]);
60+
if (result) {
61+
return result.stdout;
62+
}
63+
} catch { }
64+
65+
return ""
7366
};
7467

7568
export const writeToPromptsVolume = async (

0 commit comments

Comments
 (0)