Skip to content

Commit 540282c

Browse files
authored
Merge pull request #48 from AkihiroSuda/dev
add `limactl validate FILE.yaml [FILE.yaml, ...]`
2 parents 1032c25 + 341b815 commit 540282c

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ jobs:
8888
echo "this file was created on macOS" > ~/foo
8989
lima cat ~/foo
9090
limactl stop default
91+
- name: Validate examples
92+
run: limactl validate examples/*.yaml
9193

9294
artifacts:
9395
name: Artifacts

cmd/limactl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func newApp() *cli.App {
4646
shellCommand,
4747
listCommand,
4848
deleteCommand,
49+
validateCommand,
4950
pruneCommand,
5051
completionCommand,
5152
hostagentCommand, // hidden

cmd/limactl/start.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
5252
)
5353

5454
if argSeemsYAMLPath(arg) {
55-
instName = instNameFromYAMLPath(arg)
55+
instName, err = instNameFromYAMLPath(arg)
56+
if err != nil {
57+
return nil, err
58+
}
5659
logrus.Debugf("interpreting argument %q as a file path for instance %q", arg, instName)
5760
yBytes, err = os.ReadFile(arg)
5861
if err != nil {
@@ -183,17 +186,14 @@ func argSeemsYAMLPath(arg string) bool {
183186
return strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml")
184187
}
185188

186-
func instNameFromYAMLPath(yamlPath string) string {
189+
func instNameFromYAMLPath(yamlPath string) (string, error) {
187190
s := strings.ToLower(filepath.Base(yamlPath))
188191
s = strings.TrimSuffix(strings.TrimSuffix(s, ".yml"), ".yaml")
189192
s = strings.ReplaceAll(s, ".", "-")
190193
if err := identifiers.Validate(s); err != nil {
191-
logrus.WithField("candidate", s).WithError(err).
192-
Warnf("failed to determine the name of the new instance from file path %q, using the default name %q",
193-
yamlPath, DefaultInstanceName)
194-
return DefaultInstanceName
194+
return "", errors.Wrapf(err, "filename %q is invalid", yamlPath)
195195
}
196-
return s
196+
return s, nil
197197
}
198198

199199
func startBashComplete(clicontext *cli.Context) {

cmd/limactl/validate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"github.com/AkihiroSuda/lima/pkg/store"
5+
"github.com/pkg/errors"
6+
"github.com/sirupsen/logrus"
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
var validateCommand = &cli.Command{
11+
Name: "validate",
12+
Usage: "Validate yaml files",
13+
ArgsUsage: "FILE.yaml [FILE.yaml, ...]",
14+
Action: validateAction,
15+
}
16+
17+
func validateAction(clicontext *cli.Context) error {
18+
if clicontext.NArg() == 0 {
19+
return errors.Errorf("requires at least 1 argument")
20+
}
21+
22+
for _, f := range clicontext.Args().Slice() {
23+
_, err := store.LoadYAMLByFilePath(f)
24+
if err != nil {
25+
return errors.Wrapf(err, "failed to load YAML file %q", f)
26+
}
27+
if _, err := instNameFromYAMLPath(f); err != nil {
28+
return err
29+
}
30+
logrus.Infof("%q: OK", f)
31+
}
32+
33+
return nil
34+
}

0 commit comments

Comments
 (0)