Skip to content

Commit 51efe6a

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

File tree

4 files changed

+108
-86
lines changed

4 files changed

+108
-86
lines changed

cmd/limactl/info.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/lima-vm/lima/pkg/limayaml"
8-
"github.com/lima-vm/lima/pkg/store/dirnames"
9-
"github.com/lima-vm/lima/pkg/version"
7+
"github.com/lima-vm/lima/pkg/infoutil"
108
"github.com/spf13/cobra"
119
)
1210

@@ -20,37 +18,8 @@ func newInfoCommand() *cobra.Command {
2018
return infoCommand
2119
}
2220

23-
type TemplateYAML struct {
24-
Name string `json:"name"`
25-
Location string `json:"location"`
26-
}
27-
28-
type Info struct {
29-
Version string `json:"version"`
30-
Templates []TemplateYAML `json:"templates"`
31-
DefaultTemplate *limayaml.LimaYAML `json:"defaultTemplate"`
32-
LimaHome string `json:"limaHome"`
33-
// TODO: add diagnostic info of QEMU
34-
}
35-
3621
func infoAction(cmd *cobra.Command, args []string) error {
37-
b, err := readDefaultTemplate()
38-
if err != nil {
39-
return err
40-
}
41-
y, err := limayaml.Load(b, "")
42-
if err != nil {
43-
return err
44-
}
45-
info := &Info{
46-
Version: version.Version,
47-
DefaultTemplate: y,
48-
}
49-
info.Templates, err = listTemplateYAMLs()
50-
if err != nil {
51-
return err
52-
}
53-
info.LimaHome, err = dirnames.LimaDir()
22+
info, err := infoutil.GetInfo()
5423
if err != nil {
5524
return err
5625
}

cmd/limactl/start.go

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"io/fs"
87
"net/http"
98
"net/url"
109
"os"
@@ -14,15 +13,14 @@ import (
1413

1514
"github.com/AlecAivazis/survey/v2"
1615
"github.com/containerd/containerd/identifiers"
17-
securejoin "github.com/cyphar/filepath-securejoin"
1816
"github.com/lima-vm/lima/pkg/editutil"
1917
"github.com/lima-vm/lima/pkg/limayaml"
2018
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
2119
"github.com/lima-vm/lima/pkg/osutil"
2220
"github.com/lima-vm/lima/pkg/start"
2321
"github.com/lima-vm/lima/pkg/store"
2422
"github.com/lima-vm/lima/pkg/store/filenames"
25-
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
23+
"github.com/lima-vm/lima/pkg/templatestore"
2624
"github.com/mattn/go-isatty"
2725
"github.com/sirupsen/logrus"
2826
"github.com/spf13/cobra"
@@ -59,22 +57,6 @@ $ limactl start --name=default https://raw.githubusercontent.com/lima-vm/lima/ma
5957
return startCommand
6058
}
6159

62-
func readTemplate(name string) ([]byte, error) {
63-
dir, err := usrlocalsharelima.Dir()
64-
if err != nil {
65-
return nil, err
66-
}
67-
defaultYAMLPath, err := securejoin.SecureJoin(filepath.Join(dir, "examples"), name+".yaml")
68-
if err != nil {
69-
return nil, err
70-
}
71-
return os.ReadFile(defaultYAMLPath)
72-
}
73-
74-
func readDefaultTemplate() ([]byte, error) {
75-
return readTemplate("default")
76-
}
77-
7860
func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, error) {
7961
var arg string // can be empty
8062
if len(args) > 0 {
@@ -99,7 +81,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
9981
// e.g., templateName = "deprecated/centos-7" , st.instName = "centos-7"
10082
st.instName = filepath.Base(templateName)
10183
}
102-
st.yBytes, err = readTemplate(templateName)
84+
st.yBytes, err = templatestore.Read(templateName)
10385
if err != nil {
10486
return nil, err
10587
}
@@ -184,7 +166,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
184166
logrus.Warnf("This form is deprecated. Use `limactl start --name=%s template://default` instead", st.instName)
185167
}
186168
// Read the default template for creating a new instance
187-
st.yBytes, err = readDefaultTemplate()
169+
st.yBytes, err = templatestore.Read(templatestore.Default)
188170
if err != nil {
189171
return nil, err
190172
}
@@ -299,7 +281,7 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
299281
}
300282
return st, nil
301283
case prompt.Options[2]: // "Choose another example..."
302-
examples, err := listTemplateYAMLs()
284+
examples, err := templatestore.Templates()
303285
if err != nil {
304286
return st, err
305287
}
@@ -336,40 +318,11 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
336318
}
337319
}
338320

339-
func listTemplateYAMLs() ([]TemplateYAML, error) {
340-
usrlocalsharelimaDir, err := usrlocalsharelima.Dir()
341-
if err != nil {
342-
return nil, err
343-
}
344-
examplesDir := filepath.Join(usrlocalsharelimaDir, "examples")
345-
346-
var res []TemplateYAML
347-
walkDirFn := func(p string, d fs.DirEntry, err error) error {
348-
if err != nil {
349-
return err
350-
}
351-
base := filepath.Base(p)
352-
if strings.HasPrefix(base, ".") || !strings.HasSuffix(base, ".yaml") {
353-
return nil
354-
}
355-
x := TemplateYAML{
356-
// Name is like "default", "debian", "deprecated/centos-7", ...
357-
Name: strings.TrimSuffix(strings.TrimPrefix(p, examplesDir+"/"), ".yaml"),
358-
Location: p,
359-
}
360-
res = append(res, x)
361-
return nil
362-
}
363-
if err = filepath.WalkDir(examplesDir, walkDirFn); err != nil {
364-
return nil, err
365-
}
366-
return res, nil
367-
}
368321
func startAction(cmd *cobra.Command, args []string) error {
369322
if listTemplates, err := cmd.Flags().GetBool("list-templates"); err != nil {
370323
return err
371324
} else if listTemplates {
372-
if templates, err := listTemplateYAMLs(); err == nil {
325+
if templates, err := templatestore.Templates(); err == nil {
373326
w := cmd.OutOrStdout()
374327
for _, f := range templates {
375328
fmt.Fprintln(w, f.Name)
@@ -459,7 +412,7 @@ func instNameFromYAMLPath(yamlPath string) (string, error) {
459412

460413
func startBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
461414
comp, _ := bashCompleteInstanceNames(cmd)
462-
if templates, err := listTemplateYAMLs(); err == nil {
415+
if templates, err := templatestore.Templates(); err == nil {
463416
for _, f := range templates {
464417
comp = append(comp, "template://"+f.Name)
465418
}

pkg/infoutil/infoutil.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package infoutil
2+
3+
import (
4+
"github.com/lima-vm/lima/pkg/limayaml"
5+
"github.com/lima-vm/lima/pkg/store/dirnames"
6+
"github.com/lima-vm/lima/pkg/templatestore"
7+
"github.com/lima-vm/lima/pkg/version"
8+
)
9+
10+
type Info struct {
11+
Version string `json:"version"`
12+
Templates []templatestore.Template `json:"templates"`
13+
DefaultTemplate *limayaml.LimaYAML `json:"defaultTemplate"`
14+
LimaHome string `json:"limaHome"`
15+
// TODO: add diagnostic info of QEMU
16+
}
17+
18+
func GetInfo() (*Info, error) {
19+
b, err := templatestore.Read(templatestore.Default)
20+
if err != nil {
21+
return nil, err
22+
}
23+
y, err := limayaml.Load(b, "")
24+
if err != nil {
25+
return nil, err
26+
}
27+
info := &Info{
28+
Version: version.Version,
29+
DefaultTemplate: y,
30+
}
31+
info.Templates, err = templatestore.Templates()
32+
if err != nil {
33+
return nil, err
34+
}
35+
info.LimaHome, err = dirnames.LimaDir()
36+
if err != nil {
37+
return nil, err
38+
}
39+
return info, nil
40+
}

pkg/templatestore/templatestore.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package templatestore
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
securejoin "github.com/cyphar/filepath-securejoin"
10+
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
11+
)
12+
13+
type Template struct {
14+
Name string `json:"name"`
15+
Location string `json:"location"`
16+
}
17+
18+
func Read(name string) ([]byte, error) {
19+
dir, err := usrlocalsharelima.Dir()
20+
if err != nil {
21+
return nil, err
22+
}
23+
yamlPath, err := securejoin.SecureJoin(filepath.Join(dir, "examples"), name+".yaml")
24+
if err != nil {
25+
return nil, err
26+
}
27+
return os.ReadFile(yamlPath)
28+
}
29+
30+
const Default = "default"
31+
32+
func Templates() ([]Template, error) {
33+
usrlocalsharelimaDir, err := usrlocalsharelima.Dir()
34+
if err != nil {
35+
return nil, err
36+
}
37+
examplesDir := filepath.Join(usrlocalsharelimaDir, "examples")
38+
39+
var res []Template
40+
walkDirFn := func(p string, d fs.DirEntry, err error) error {
41+
if err != nil {
42+
return err
43+
}
44+
base := filepath.Base(p)
45+
if strings.HasPrefix(base, ".") || !strings.HasSuffix(base, ".yaml") {
46+
return nil
47+
}
48+
x := Template{
49+
// Name is like "default", "debian", "deprecated/centos-7", ...
50+
Name: strings.TrimSuffix(strings.TrimPrefix(p, examplesDir+"/"), ".yaml"),
51+
Location: p,
52+
}
53+
res = append(res, x)
54+
return nil
55+
}
56+
if err = filepath.WalkDir(examplesDir, walkDirFn); err != nil {
57+
return nil, err
58+
}
59+
return res, nil
60+
}

0 commit comments

Comments
 (0)