Skip to content

Commit 22af485

Browse files
authored
Merge pull request #3900 from unsuman/refactor/create-limatype
refactor: extract common types and functions from `limayaml` & `store`
2 parents 91b96e6 + f07bae9 commit 22af485

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+861
-766
lines changed

cmd/limactl/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313

1414
"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
1515
"github.com/lima-vm/lima/v2/pkg/instance"
16+
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
1617
"github.com/lima-vm/lima/v2/pkg/limayaml"
1718
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
1819
"github.com/lima-vm/lima/v2/pkg/store"
19-
"github.com/lima-vm/lima/v2/pkg/store/filenames"
2020
"github.com/lima-vm/lima/v2/pkg/yqutil"
2121
)
2222

cmd/limactl/copy.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/spf13/cobra"
1818

1919
"github.com/lima-vm/lima/v2/pkg/ioutilx"
20+
"github.com/lima-vm/lima/v2/pkg/limatype"
2021
"github.com/lima-vm/lima/v2/pkg/sshutil"
2122
"github.com/lima-vm/lima/v2/pkg/store"
2223
)
@@ -63,7 +64,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
6364
if err != nil {
6465
return err
6566
}
66-
instances := make(map[string]*store.Instance)
67+
instances := make(map[string]*limatype.Instance)
6768
scpFlags := []string{}
6869
scpArgs := []string{}
6970
debug, err := cmd.Flags().GetBool("debug")
@@ -114,7 +115,7 @@ func copyAction(cmd *cobra.Command, args []string) error {
114115
}
115116
return err
116117
}
117-
if inst.Status == store.StatusStopped {
118+
if inst.Status == limatype.StatusStopped {
118119
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
119120
}
120121
if legacySSH {

cmd/limactl/disk.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919
"github.com/spf13/cobra"
2020

2121
"github.com/lima-vm/lima/v2/pkg/imgutil/proxyimgutil"
22+
"github.com/lima-vm/lima/v2/pkg/limatype"
23+
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
2224
"github.com/lima-vm/lima/v2/pkg/store"
23-
"github.com/lima-vm/lima/v2/pkg/store/filenames"
2425
)
2526

2627
func newDiskCommand() *cobra.Command {
@@ -243,7 +244,7 @@ func diskDeleteAction(cmd *cobra.Command, args []string) error {
243244
if err != nil {
244245
return err
245246
}
246-
var instances []*store.Instance
247+
var instances []*limatype.Instance
247248
for _, instName := range instNames {
248249
inst, err := store.Inspect(ctx, instName)
249250
if err != nil {
@@ -344,7 +345,7 @@ func diskUnlockAction(cmd *cobra.Command, args []string) error {
344345
diskName, disk.Instance, inst.Errors)
345346
continue
346347
}
347-
if inst.Status == store.StatusRunning {
348+
if inst.Status == limatype.StatusRunning {
348349
logrus.Warnf("Cannot unlock disk %q used by running instance %q", diskName, disk.Instance)
349350
continue
350351
}
@@ -402,7 +403,7 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
402403
if disk.Instance != "" {
403404
inst, err := store.Inspect(ctx, disk.Instance)
404405
if err == nil {
405-
if inst.Status == store.StatusRunning {
406+
if inst.Status == limatype.StatusRunning {
406407
return fmt.Errorf("cannot resize disk %q used by running instance %q. Please stop the VM instance", diskName, disk.Instance)
407408
}
408409
}

cmd/limactl/edit.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
1717
"github.com/lima-vm/lima/v2/pkg/editutil"
1818
"github.com/lima-vm/lima/v2/pkg/instance"
19+
"github.com/lima-vm/lima/v2/pkg/limatype"
20+
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
21+
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
1922
"github.com/lima-vm/lima/v2/pkg/limayaml"
2023
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
2124
"github.com/lima-vm/lima/v2/pkg/store"
22-
"github.com/lima-vm/lima/v2/pkg/store/filenames"
2325
"github.com/lima-vm/lima/v2/pkg/uiutil"
2426
"github.com/lima-vm/lima/v2/pkg/yqutil"
2527
)
@@ -46,20 +48,20 @@ func editAction(cmd *cobra.Command, args []string) error {
4648

4749
var filePath string
4850
var err error
49-
var inst *store.Instance
51+
var inst *limatype.Instance
5052

5153
if arg == "" {
5254
arg = DefaultInstanceName
5355
}
54-
if err := store.ValidateInstName(arg); err == nil {
56+
if err := dirnames.ValidateInstName(arg); err == nil {
5557
inst, err = store.Inspect(ctx, arg)
5658
if err != nil {
5759
if errors.Is(err, os.ErrNotExist) {
5860
return fmt.Errorf("instance %q not found", arg)
5961
}
6062
return err
6163
}
62-
if inst.Status == store.StatusRunning {
64+
if inst.Status == limatype.StatusRunning {
6365
return errors.New("cannot edit a running instance")
6466
}
6567
filePath = filepath.Join(inst.Dir, filenames.LimaYAML)

cmd/limactl/factory-reset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
"github.com/lima-vm/lima/v2/pkg/cidata"
1616
"github.com/lima-vm/lima/v2/pkg/instance"
17+
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
1718
"github.com/lima-vm/lima/v2/pkg/store"
18-
"github.com/lima-vm/lima/v2/pkg/store/filenames"
1919
)
2020

2121
func newFactoryResetCommand() *cobra.Command {

cmd/limactl/genschema.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
orderedmap "github.com/wk8/go-ordered-map/v2"
1616

1717
"github.com/lima-vm/lima/v2/pkg/jsonschemautil"
18-
"github.com/lima-vm/lima/v2/pkg/limayaml"
18+
"github.com/lima-vm/lima/v2/pkg/limatype"
1919
)
2020

2121
func newGenSchemaCommand() *cobra.Command {
@@ -52,7 +52,7 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
5252
return err
5353
}
5454

55-
schema := jsonschema.Reflect(&limayaml.LimaYAML{})
55+
schema := jsonschema.Reflect(&limatype.LimaYAML{})
5656
// allow Disk to be either string (name) or object (struct)
5757
schema.Definitions["Disk"].Type = "" // was: "object"
5858
schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{
@@ -72,10 +72,10 @@ func genschemaAction(cmd *cobra.Command, args []string) error {
7272
{Type: "object"},
7373
}
7474
properties := schema.Definitions["LimaYAML"].Properties
75-
getProp(properties, "os").Enum = toAny(limayaml.OSTypes)
76-
getProp(properties, "arch").Enum = toAny(limayaml.ArchTypes)
77-
getProp(properties, "mountType").Enum = toAny(limayaml.MountTypes)
78-
getProp(properties, "vmType").Enum = toAny(limayaml.VMTypes)
75+
getProp(properties, "os").Enum = toAny(limatype.OSTypes)
76+
getProp(properties, "arch").Enum = toAny(limatype.ArchTypes)
77+
getProp(properties, "mountType").Enum = toAny(limatype.MountTypes)
78+
getProp(properties, "vmType").Enum = toAny(limatype.VMTypes)
7979
j, err := json.MarshalIndent(schema, "", " ")
8080
if err != nil {
8181
return err

cmd/limactl/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sirupsen/logrus"
1717
"github.com/spf13/cobra"
1818

19+
"github.com/lima-vm/lima/v2/pkg/limatype"
1920
"github.com/lima-vm/lima/v2/pkg/store"
2021
)
2122

@@ -172,7 +173,7 @@ func listAction(cmd *cobra.Command, args []string) error {
172173
}
173174

174175
// get the state and config for all the requested instances
175-
var instances []*store.Instance
176+
var instances []*limatype.Instance
176177
for _, instanceName := range instanceNames {
177178
instance, err := store.Inspect(ctx, instanceName)
178179
if err != nil {

cmd/limactl/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"github.com/lima-vm/lima/v2/pkg/debugutil"
2121
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
2222
"github.com/lima-vm/lima/v2/pkg/fsutil"
23+
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
2324
"github.com/lima-vm/lima/v2/pkg/osutil"
24-
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
2525
"github.com/lima-vm/lima/v2/pkg/version"
2626
)
2727

cmd/limactl/prune.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313

1414
"github.com/lima-vm/lima/v2/pkg/downloader"
15+
"github.com/lima-vm/lima/v2/pkg/limatype"
1516
"github.com/lima-vm/lima/v2/pkg/limayaml"
1617
"github.com/lima-vm/lima/v2/pkg/store"
1718
"github.com/lima-vm/lima/v2/pkg/templatestore"
@@ -64,8 +65,8 @@ func pruneAction(cmd *cobra.Command, _ []string) error {
6465
return nil
6566
}
6667

67-
func knownLocations(ctx context.Context) (map[string]limayaml.File, error) {
68-
locations := make(map[string]limayaml.File)
68+
func knownLocations(ctx context.Context) (map[string]limatype.File, error) {
69+
locations := make(map[string]limatype.File)
6970

7071
// Collect locations from instances
7172
instances, err := store.Instances()
@@ -99,8 +100,8 @@ func knownLocations(ctx context.Context) (map[string]limayaml.File, error) {
99100
return locations, nil
100101
}
101102

102-
func locationsFromLimaYAML(y *limayaml.LimaYAML) map[string]limayaml.File {
103-
locations := make(map[string]limayaml.File)
103+
func locationsFromLimaYAML(y *limatype.LimaYAML) map[string]limatype.File {
104+
locations := make(map[string]limatype.File)
104105
for _, f := range y.Images {
105106
locations[downloader.CacheKey(f.Location)] = f.File
106107
if f.Kernel != nil {

cmd/limactl/shell.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/lima-vm/lima/v2/pkg/envutil"
2424
"github.com/lima-vm/lima/v2/pkg/instance"
2525
"github.com/lima-vm/lima/v2/pkg/ioutilx"
26-
"github.com/lima-vm/lima/v2/pkg/limayaml"
26+
"github.com/lima-vm/lima/v2/pkg/limatype"
2727
networks "github.com/lima-vm/lima/v2/pkg/networks/reconcile"
2828
"github.com/lima-vm/lima/v2/pkg/sshutil"
2929
"github.com/lima-vm/lima/v2/pkg/store"
@@ -86,7 +86,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
8686
}
8787
return err
8888
}
89-
if inst.Status == store.StatusStopped {
89+
if inst.Status == limatype.StatusStopped {
9090
startNow, err := askWhetherToStart()
9191
if err != nil {
9292
return err
@@ -142,7 +142,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
142142
if workDir != "" {
143143
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
144144
// FIXME: check whether y.Mounts contains the home, not just len > 0
145-
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limayaml.WSL2 {
145+
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limatype.WSL2 {
146146
hostCurrentDir, err := os.Getwd()
147147
if err == nil && runtime.GOOS == "windows" {
148148
hostCurrentDir, err = mountDirFromWindowsDir(ctx, inst, hostCurrentDir)
@@ -265,8 +265,8 @@ func shellAction(cmd *cobra.Command, args []string) error {
265265
return sshCmd.Run()
266266
}
267267

268-
func mountDirFromWindowsDir(ctx context.Context, inst *store.Instance, dir string) (string, error) {
269-
if inst.VMType == limayaml.WSL2 {
268+
func mountDirFromWindowsDir(ctx context.Context, inst *limatype.Instance, dir string) (string, error) {
269+
if inst.VMType == limatype.WSL2 {
270270
distroName := "lima-" + inst.Name
271271
return ioutilx.WindowsSubsystemPathForLinux(ctx, dir, distroName)
272272
}

0 commit comments

Comments
 (0)