|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net" |
| 11 | + "net/http" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +type FileContent struct { |
| 17 | + VolumeId string `json:"volumeId"` |
| 18 | + TargetPath string `json:"targetPath"` |
| 19 | + Contents string `json:"contents"` |
| 20 | +} |
| 21 | + |
| 22 | +func ReadFromVolume(ctx context.Context) *cobra.Command { |
| 23 | + return &cobra.Command{ |
| 24 | + Use: "read-from-volume", |
| 25 | + Short: "Read a file from the extension's volume", |
| 26 | + Args: cobra.ExactArgs(1), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 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 { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + fmt.Print(content.Contents) |
| 36 | + return nil |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 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{ |
| 66 | + Transport: &http.Transport{ |
| 67 | + DialContext: func(ctx context.Context, _, _ string) (conn net.Conn, err error) { |
| 68 | + return dialVolumeContents(ctx) |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 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) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + req.Header.Set("X-DockerDesktop-Host", "vm.docker.internal") |
| 80 | + |
| 81 | + response, err := httpClient.Do(req) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer response.Body.Close() |
| 86 | + |
| 87 | + buf, err := io.ReadAll(response.Body) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + if err := json.Unmarshal(buf, &v); err != nil { |
| 93 | + return err |
| 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 | + |
| 123 | + return nil |
| 124 | +} |
0 commit comments