github.com/zaquestion/lab@v0.25.1/cmd/ci_lint.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/MakeNowJust/heredoc/v2"
     6  	"io/ioutil"
     7  	"os"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/rsteube/carapace"
    11  	"github.com/spf13/cobra"
    12  	lab "github.com/zaquestion/lab/internal/gitlab"
    13  )
    14  
    15  // ciLintCmd represents the lint command
    16  var ciLintCmd = &cobra.Command{
    17  	Use:   "lint",
    18  	Short: "Validate .gitlab-ci.yml against GitLab",
    19  	Example: heredoc.Doc(`
    20  		lab ci lint
    21  		lab ci lint upstream`),
    22  	PersistentPreRun: labPersistentPreRun,
    23  	Run: func(cmd *cobra.Command, args []string) {
    24  		path := ".gitlab-ci.yml"
    25  		if len(args) == 1 {
    26  			path = args[0]
    27  		}
    28  		b, err := ioutil.ReadFile(path)
    29  		if !os.IsNotExist(err) && err != nil {
    30  			log.Fatal(err)
    31  		}
    32  		ok, err := lab.Lint(string(b))
    33  		if !ok || err != nil {
    34  			log.Fatal(errors.Wrap(err, "ci yaml invalid"))
    35  		}
    36  		fmt.Println("Valid!")
    37  	},
    38  }
    39  
    40  func init() {
    41  	ciCmd.AddCommand(ciLintCmd)
    42  	carapace.Gen(ciLintCmd).PositionalCompletion(
    43  		carapace.ActionFiles(".gitlab-ci.yml"),
    44  	)
    45  }