Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/store/store.go
2604 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package store
5
6
import (
7
"context"
8
"errors"
9
"fmt"
10
"os"
11
"path/filepath"
12
"strings"
13
14
"github.com/lima-vm/lima/v2/pkg/driverutil"
15
"github.com/lima-vm/lima/v2/pkg/identifiers"
16
"github.com/lima-vm/lima/v2/pkg/limatype"
17
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
18
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
19
"github.com/lima-vm/lima/v2/pkg/limayaml"
20
)
21
22
// Directory returns the LimaDir.
23
func Directory() string {
24
limaDir, err := dirnames.LimaDir()
25
if err != nil {
26
return ""
27
}
28
return limaDir
29
}
30
31
// Validate checks the LimaDir.
32
func Validate() error {
33
limaDir, err := dirnames.LimaDir()
34
if err != nil {
35
return err
36
}
37
names, err := Instances()
38
if err != nil {
39
return err
40
}
41
for _, name := range names {
42
// Each instance directory needs to have limayaml
43
instDir := filepath.Join(limaDir, name)
44
yamlPath := filepath.Join(instDir, filenames.LimaYAML)
45
if _, err := os.Stat(yamlPath); err != nil {
46
return err
47
}
48
}
49
return nil
50
}
51
52
// Instances returns the names of the instances under LimaDir.
53
func Instances() ([]string, error) {
54
limaDir, err := dirnames.LimaDir()
55
if err != nil {
56
return nil, err
57
}
58
limaDirList, err := os.ReadDir(limaDir)
59
if err != nil {
60
if errors.Is(err, os.ErrNotExist) {
61
return nil, nil
62
}
63
return nil, err
64
}
65
var names []string
66
for _, f := range limaDirList {
67
if strings.HasPrefix(f.Name(), ".") || strings.HasPrefix(f.Name(), "_") {
68
continue
69
}
70
if !f.IsDir() {
71
continue
72
}
73
names = append(names, f.Name())
74
}
75
return names, nil
76
}
77
78
func Disks() ([]string, error) {
79
limaDiskDir, err := dirnames.LimaDisksDir()
80
if err != nil {
81
return nil, err
82
}
83
limaDiskDirList, err := os.ReadDir(limaDiskDir)
84
if err != nil {
85
if errors.Is(err, os.ErrNotExist) {
86
return nil, nil
87
}
88
return nil, err
89
}
90
var names []string
91
for _, f := range limaDiskDirList {
92
names = append(names, f.Name())
93
}
94
return names, nil
95
}
96
97
func DiskDir(name string) (string, error) {
98
if err := identifiers.Validate(name); err != nil {
99
return "", err
100
}
101
limaDisksDir, err := dirnames.LimaDisksDir()
102
if err != nil {
103
return "", err
104
}
105
dir := filepath.Join(limaDisksDir, name)
106
return dir, nil
107
}
108
109
// LoadYAMLByFilePath loads and validates the yaml.
110
func LoadYAMLByFilePath(ctx context.Context, filePath string) (*limatype.LimaYAML, error) {
111
// We need to use the absolute path because it may be used to determine hostSocket locations.
112
absPath, err := filepath.Abs(filePath)
113
if err != nil {
114
return nil, err
115
}
116
yContent, err := os.ReadFile(absPath)
117
if err != nil {
118
return nil, err
119
}
120
y, err := limayaml.Load(ctx, yContent, absPath)
121
if err != nil {
122
return nil, err
123
}
124
if err := driverutil.ResolveVMType(ctx, y, filePath); err != nil {
125
return nil, fmt.Errorf("failed to resolve vm for %q: %w", filePath, err)
126
}
127
if err := limayaml.Validate(y, false); err != nil {
128
return nil, err
129
}
130
return y, nil
131
}
132
133