1
1
package commands
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "encoding/base64"
5
7
"encoding/json"
6
8
"fmt"
7
9
"io"
@@ -11,43 +13,66 @@ import (
11
13
"github.com/spf13/cobra"
12
14
)
13
15
16
+ type FileContent struct {
17
+ VolumeId string `json:"volumeId"`
18
+ TargetPath string `json:"targetPath"`
19
+ Contents string `json:"contents"`
20
+ }
21
+
14
22
func ReadFromVolume (ctx context.Context ) * cobra.Command {
15
23
return & cobra.Command {
16
24
Use : "read-from-volume" ,
17
25
Short : "Read a file from the extension's volume" ,
18
26
Args : cobra .ExactArgs (1 ),
19
27
RunE : func (cmd * cobra.Command , args []string ) error {
20
- content , err := readConfig (ctx , args [0 ])
21
- if err != nil {
28
+ filename := args [0 ]
29
+
30
+ var content FileContent
31
+ if err := get (ctx , httpClient (), "/volume-file-content?volumeId=docker-prompts&targetPath=" + filename , & content ); err != nil {
22
32
return err
23
33
}
24
- fmt .Print (content )
34
+
35
+ fmt .Print (content .Contents )
25
36
return nil
26
37
},
27
38
}
28
39
}
29
40
30
- func readConfig (ctx context.Context , filename string ) (string , error ) {
31
- httpClient := & http.Client {
41
+ func WriteToVolume (ctx context.Context ) * cobra.Command {
42
+ return & cobra.Command {
43
+ Use : "write-to-volume" ,
44
+ Short : "Write some base64 encoded content to a file on the extension's volume" ,
45
+ Args : cobra .ExactArgs (2 ),
46
+ RunE : func (cmd * cobra.Command , args []string ) error {
47
+ filename := args [0 ]
48
+ contentBase64 := args [1 ]
49
+
50
+ content , err := base64 .StdEncoding .DecodeString (contentBase64 )
51
+ if err != nil {
52
+ return err
53
+ }
54
+
55
+ return post (ctx , httpClient (), "/volume-file-content" , FileContent {
56
+ VolumeId : "docker-prompts" ,
57
+ TargetPath : filename ,
58
+ Contents : string (content ),
59
+ })
60
+ },
61
+ }
62
+ }
63
+
64
+ func httpClient () * http.Client {
65
+ return & http.Client {
32
66
Transport : & http.Transport {
33
67
DialContext : func (ctx context.Context , _ , _ string ) (conn net.Conn , err error ) {
34
68
return dialVolumeContents (ctx )
35
69
},
36
70
},
37
71
}
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
72
}
48
73
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 )
74
+ func get (ctx context.Context , httpClient * http.Client , endpoint string , v any ) error {
75
+ req , err := http .NewRequestWithContext (ctx , http . MethodGet , "http://localhost" + endpoint , nil )
51
76
if err != nil {
52
77
return err
53
78
}
@@ -67,5 +92,33 @@ func query(ctx context.Context, httpClient *http.Client, method string, endpoint
67
92
if err := json .Unmarshal (buf , & v ); err != nil {
68
93
return err
69
94
}
95
+
96
+ return nil
97
+ }
98
+
99
+ func post (ctx context.Context , httpClient * http.Client , endpoint string , v any ) error {
100
+ payload , err := json .Marshal (v )
101
+ if err != nil {
102
+ return err
103
+ }
104
+
105
+ req , err := http .NewRequestWithContext (ctx , http .MethodPost , "http://localhost" + endpoint , bytes .NewReader (payload ))
106
+ if err != nil {
107
+ return err
108
+ }
109
+ req .Header .Set ("X-DockerDesktop-Host" , "vm.docker.internal" )
110
+ req .Header .Set ("Content-Type" , "application/json" )
111
+
112
+ response , err := httpClient .Do (req )
113
+ if err != nil {
114
+ return err
115
+ }
116
+ defer response .Body .Close ()
117
+
118
+ _ , err = io .ReadAll (response .Body )
119
+ if err != nil {
120
+ return err
121
+ }
122
+
70
123
return nil
71
124
}
0 commit comments