Skip to content

Commit

Permalink
Merge pull request #48 from AkihiroSuda/dev
Browse files Browse the repository at this point in the history
add `limactl validate FILE.yaml [FILE.yaml, ...]`
  • Loading branch information
AkihiroSuda authored Jun 15, 2021
2 parents 1032c25 + 341b815 commit 540282c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ jobs:
echo "this file was created on macOS" > ~/foo
lima cat ~/foo
limactl stop default
- name: Validate examples
run: limactl validate examples/*.yaml

artifacts:
name: Artifacts
Expand Down
1 change: 1 addition & 0 deletions cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func newApp() *cli.App {
shellCommand,
listCommand,
deleteCommand,
validateCommand,
pruneCommand,
completionCommand,
hostagentCommand, // hidden
Expand Down
14 changes: 7 additions & 7 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func loadOrCreateInstance(clicontext *cli.Context) (*store.Instance, error) {
)

if argSeemsYAMLPath(arg) {
instName = instNameFromYAMLPath(arg)
instName, err = instNameFromYAMLPath(arg)
if err != nil {
return nil, err
}
logrus.Debugf("interpreting argument %q as a file path for instance %q", arg, instName)
yBytes, err = os.ReadFile(arg)
if err != nil {
Expand Down Expand Up @@ -183,17 +186,14 @@ func argSeemsYAMLPath(arg string) bool {
return strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml")
}

func instNameFromYAMLPath(yamlPath string) string {
func instNameFromYAMLPath(yamlPath string) (string, error) {
s := strings.ToLower(filepath.Base(yamlPath))
s = strings.TrimSuffix(strings.TrimSuffix(s, ".yml"), ".yaml")
s = strings.ReplaceAll(s, ".", "-")
if err := identifiers.Validate(s); err != nil {
logrus.WithField("candidate", s).WithError(err).
Warnf("failed to determine the name of the new instance from file path %q, using the default name %q",
yamlPath, DefaultInstanceName)
return DefaultInstanceName
return "", errors.Wrapf(err, "filename %q is invalid", yamlPath)
}
return s
return s, nil
}

func startBashComplete(clicontext *cli.Context) {
Expand Down
34 changes: 34 additions & 0 deletions cmd/limactl/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"github.com/AkihiroSuda/lima/pkg/store"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

var validateCommand = &cli.Command{
Name: "validate",
Usage: "Validate yaml files",
ArgsUsage: "FILE.yaml [FILE.yaml, ...]",
Action: validateAction,
}

func validateAction(clicontext *cli.Context) error {
if clicontext.NArg() == 0 {
return errors.Errorf("requires at least 1 argument")
}

for _, f := range clicontext.Args().Slice() {
_, err := store.LoadYAMLByFilePath(f)
if err != nil {
return errors.Wrapf(err, "failed to load YAML file %q", f)
}
if _, err := instNameFromYAMLPath(f); err != nil {
return err
}
logrus.Infof("%q: OK", f)
}

return nil
}

0 comments on commit 540282c

Please sign in to comment.