Skip to content

Commit 1777a3f

Browse files
committed
cmd/limactl: split ioutilx (no code change)
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 51efe6a commit 1777a3f

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

cmd/limactl/start.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"net/http"
87
"net/url"
98
"os"
@@ -14,6 +13,7 @@ import (
1413
"github.com/AlecAivazis/survey/v2"
1514
"github.com/containerd/containerd/identifiers"
1615
"github.com/lima-vm/lima/pkg/editutil"
16+
"github.com/lima-vm/lima/pkg/ioutilx"
1717
"github.com/lima-vm/lima/pkg/limayaml"
1818
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
1919
"github.com/lima-vm/lima/pkg/osutil"
@@ -98,7 +98,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
9898
return nil, err
9999
}
100100
defer resp.Body.Close()
101-
st.yBytes, err = readAtMaximum(resp.Body, yBytesLimit)
101+
st.yBytes, err = ioutilx.ReadAtMaximum(resp.Body, yBytesLimit)
102102
if err != nil {
103103
return nil, err
104104
}
@@ -115,7 +115,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
115115
return nil, err
116116
}
117117
defer r.Close()
118-
st.yBytes, err = readAtMaximum(r, yBytesLimit)
118+
st.yBytes, err = ioutilx.ReadAtMaximum(r, yBytesLimit)
119119
if err != nil {
120120
return nil, err
121121
}
@@ -132,7 +132,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
132132
return nil, err
133133
}
134134
defer r.Close()
135-
st.yBytes, err = readAtMaximum(r, yBytesLimit)
135+
st.yBytes, err = ioutilx.ReadAtMaximum(r, yBytesLimit)
136136
if err != nil {
137137
return nil, err
138138
}
@@ -419,17 +419,3 @@ func startBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]
419419
}
420420
return comp, cobra.ShellCompDirectiveDefault
421421
}
422-
423-
func readAtMaximum(r io.Reader, n int64) ([]byte, error) {
424-
lr := &io.LimitedReader{
425-
R: r,
426-
N: n,
427-
}
428-
b, err := io.ReadAll(lr)
429-
if err != nil {
430-
if errors.Is(err, io.EOF) && lr.N <= 0 {
431-
err = fmt.Errorf("exceeded the limit (%d bytes): %w", n, err)
432-
}
433-
}
434-
return b, err
435-
}

pkg/ioutilx/ioutilx.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ioutilx
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
)
8+
9+
// ReadAtMaximum reands n at maximum.
10+
func ReadAtMaximum(r io.Reader, n int64) ([]byte, error) {
11+
lr := &io.LimitedReader{
12+
R: r,
13+
N: n,
14+
}
15+
b, err := io.ReadAll(lr)
16+
if err != nil {
17+
if errors.Is(err, io.EOF) && lr.N <= 0 {
18+
err = fmt.Errorf("exceeded the limit (%d bytes): %w", n, err)
19+
}
20+
}
21+
return b, err
22+
}

0 commit comments

Comments
 (0)