This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
lint.go
96 lines (81 loc) · 1.84 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package lint
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/pkg/git"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
)
func NewCmdLint(f *cmdutils.Factory) *cobra.Command {
var pipelineCILintCmd = &cobra.Command{
Use: "lint",
Short: "Checks if your .gitlab-ci.yml file is valid.",
Args: cobra.MaximumNArgs(1),
Example: heredoc.Doc(`
$ glab ci lint
#=> Uses .gitlab-ci.yml in the current directory
$ glab ci lint .gitlab-ci.yml
$ glab ci lint path/to/.gitlab-ci.yml
`),
RunE: func(cmd *cobra.Command, args []string) error {
path := ".gitlab-ci.yml"
if len(args) == 1 {
path = args[0]
}
return lintRun(f, path)
},
}
return pipelineCILintCmd
}
func lintRun(f *cmdutils.Factory, path string) error {
var err error
out := f.IO.StdOut
c := f.IO.Color()
apiClient, err := f.HttpClient()
if err != nil {
return err
}
fmt.Fprintln(f.IO.StdErr, "Getting contents in", path)
var content []byte
var stdout bytes.Buffer
if git.IsValidURL(path) {
resp, err := http.Get(path)
if err != nil {
return err
}
_, err = io.Copy(&stdout, resp.Body)
if err != nil {
return err
}
content = stdout.Bytes()
} else {
content, err = ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s: no such file or directory", path)
}
return err
}
}
fmt.Fprintln(f.IO.StdErr, "Validating...")
lint, err := api.PipelineCILint(apiClient, string(content))
if err != nil {
return err
}
if lint.Status == "invalid" {
fmt.Fprintln(out, c.Red(path+" is invalid"))
for i, err := range lint.Errors {
i++
fmt.Fprintln(out, i, err)
}
return cmdutils.SilentError
}
fmt.Fprintln(out, c.GreenCheck(), "CI yml is Valid!")
return nil
}