Skip to content

Commit e74920c

Browse files
authored
Merge pull request #279 from AkihiroSuda/followup-276
Follow-up to `Implement starting instances from a HTTP URL ` (#276)
2 parents f56f620 + 4424d63 commit e74920c

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

cmd/limactl/start.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/AlecAivazis/survey/v2"
1717
"github.com/containerd/containerd/identifiers"
1818
"github.com/lima-vm/lima/pkg/limayaml"
19-
"github.com/lima-vm/lima/pkg/networks/reconcile"
19+
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
2020
"github.com/lima-vm/lima/pkg/osutil"
2121
"github.com/lima-vm/lima/pkg/start"
2222
"github.com/lima-vm/lima/pkg/store"
@@ -53,8 +53,10 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
5353
err error
5454
)
5555

56+
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
57+
5658
if argSeemsHTTPURL(arg) {
57-
instName, err = instNameFromHTTPURL(arg)
59+
instName, err = instNameFromURL(arg)
5860
if err != nil {
5961
return nil, err
6062
}
@@ -64,7 +66,22 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
6466
return nil, err
6567
}
6668
defer resp.Body.Close()
67-
yBytes, err = io.ReadAll(resp.Body)
69+
yBytes, err = readAtMaximum(resp.Body, yBytesLimit)
70+
if err != nil {
71+
return nil, err
72+
}
73+
} else if argSeemsFileURL(arg) {
74+
instName, err = instNameFromURL(arg)
75+
if err != nil {
76+
return nil, err
77+
}
78+
logrus.Debugf("interpreting argument %q as a file url for instance %q", arg, instName)
79+
r, err := os.Open(strings.TrimPrefix(arg, "file://"))
80+
if err != nil {
81+
return nil, err
82+
}
83+
defer r.Close()
84+
yBytes, err = readAtMaximum(r, yBytesLimit)
6885
if err != nil {
6986
return nil, err
7087
}
@@ -74,7 +91,12 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
7491
return nil, err
7592
}
7693
logrus.Debugf("interpreting argument %q as a file path for instance %q", arg, instName)
77-
yBytes, err = os.ReadFile(arg)
94+
r, err := os.Open(arg)
95+
if err != nil {
96+
return nil, err
97+
}
98+
defer r.Close()
99+
yBytes, err = readAtMaximum(r, yBytesLimit)
78100
if err != nil {
79101
return nil, err
80102
}
@@ -269,6 +291,14 @@ func argSeemsHTTPURL(arg string) bool {
269291
return true
270292
}
271293

294+
func argSeemsFileURL(arg string) bool {
295+
u, err := url.Parse(arg)
296+
if err != nil {
297+
return false
298+
}
299+
return u.Scheme == "file"
300+
}
301+
272302
func argSeemsYAMLPath(arg string) bool {
273303
if strings.Contains(arg, "/") {
274304
return true
@@ -277,8 +307,8 @@ func argSeemsYAMLPath(arg string) bool {
277307
return strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml")
278308
}
279309

280-
func instNameFromHTTPURL(httpURL string) (string, error) {
281-
u, err := url.Parse(httpURL)
310+
func instNameFromURL(urlStr string) (string, error) {
311+
u, err := url.Parse(urlStr)
282312
if err != nil {
283313
return "", err
284314
}
@@ -299,3 +329,17 @@ func startBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]
299329
instances, _ := bashCompleteInstanceNames(cmd)
300330
return instances, cobra.ShellCompDirectiveDefault
301331
}
332+
333+
func readAtMaximum(r io.Reader, n int64) ([]byte, error) {
334+
lr := &io.LimitedReader{
335+
R: r,
336+
N: n,
337+
}
338+
b, err := io.ReadAll(lr)
339+
if err != nil {
340+
if errors.Is(err, io.EOF) && lr.N <= 0 {
341+
err = fmt.Errorf("exceeded the limit (%d bytes): %w", n, err)
342+
}
343+
}
344+
return b, err
345+
}

0 commit comments

Comments
 (0)