-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathlint.go
79 lines (69 loc) · 2.35 KB
/
lint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/elastic/elastic-package/internal/cobraext"
"github.com/elastic/elastic-package/internal/docs"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/validation"
)
const lintLongDescription = `Use this command to validate the contents of a package using the package specification (see: https://github.com/elastic/package-spec).
The command ensures that the package is aligned with the package spec and the README file is up-to-date with its template (if present).`
func setupLintCommand() *cobraext.Command {
cmd := &cobra.Command{
Use: "lint",
Short: "Lint the package",
Long: lintLongDescription,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := cobraext.ComposeCommandActions(cmd, args,
lintCommandAction,
validateSourceCommandAction,
)
if err != nil {
return err
}
cmd.Println("Done")
return nil
},
}
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
}
func lintCommandAction(cmd *cobra.Command, args []string) error {
cmd.Println("Lint the package")
readmeFiles, err := docs.AreReadmesUpToDate()
if err != nil {
for _, f := range readmeFiles {
if !f.UpToDate {
cmd.Printf("%s is outdated. Rebuild the package with 'elastic-package build'\n%s", f.FileName, f.Diff)
}
if f.Error != nil {
cmd.Printf("check if %s is up-to-date failed: %s\n", f.FileName, f.Error)
}
}
return fmt.Errorf("checking readme files are up-to-date failed: %w", err)
}
return nil
}
func validateSourceCommandAction(cmd *cobra.Command, args []string) error {
packageRootPath, found, err := packages.FindPackageRoot()
if !found {
return errors.New("package root not found")
}
if err != nil {
return fmt.Errorf("locating package root failed: %w", err)
}
errs, skipped := validation.ValidateAndFilterFromPath(packageRootPath)
if skipped != nil {
logger.Infof("Skipped errors: %v", skipped)
}
if errs != nil {
return fmt.Errorf("linting package failed: %w", errs)
}
return nil
}