github.com/goreleaser/goreleaser@v1.25.1/cmd/check.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/caarlos0/ctrlc"
     9  	"github.com/caarlos0/log"
    10  	"github.com/goreleaser/goreleaser/internal/pipe/defaults"
    11  	"github.com/goreleaser/goreleaser/pkg/context"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type checkCmd struct {
    16  	cmd        *cobra.Command
    17  	config     string
    18  	quiet      bool
    19  	deprecated bool
    20  	checked    int
    21  }
    22  
    23  func newCheckCmd() *checkCmd {
    24  	root := &checkCmd{}
    25  	cmd := &cobra.Command{
    26  		Use:           "check [configuration files]",
    27  		Aliases:       []string{"c"},
    28  		Short:         "Checks if configuration is valid",
    29  		SilenceUsage:  true,
    30  		SilenceErrors: true,
    31  		Args:          cobra.ArbitraryArgs,
    32  		RunE: func(_ *cobra.Command, args []string) error {
    33  			if root.quiet {
    34  				log.Log = log.New(io.Discard)
    35  			}
    36  
    37  			var errs []*exitError
    38  			if root.config != "" || len(args) == 0 {
    39  				args = append(args, root.config)
    40  			}
    41  			for _, config := range args {
    42  				cfg, path, err := loadConfigCheck(config)
    43  				if err != nil {
    44  					return err
    45  				}
    46  				ctx := context.New(cfg)
    47  				ctx.Deprecated = root.deprecated
    48  
    49  				if err := ctrlc.Default.Run(ctx, func() error {
    50  					log.WithField("path", config).
    51  						Info(boldStyle.Render("checking"))
    52  
    53  					return defaults.Pipe{}.Run(ctx)
    54  				}); err != nil {
    55  					log.WithError(err).Error(boldStyle.Render("configuration is invalid"))
    56  					errs = append(errs, wrapErrorWithCode(
    57  						fmt.Errorf("configuration is invalid: %w", err),
    58  						1,
    59  						path,
    60  					))
    61  				}
    62  
    63  				if ctx.Deprecated {
    64  					errs = append(errs, wrapErrorWithCode(
    65  						fmt.Errorf("configuration is valid, but uses deprecated properties"),
    66  						2,
    67  						path,
    68  					))
    69  				}
    70  			}
    71  
    72  			root.checked = len(args)
    73  			exit := 0
    74  			for _, err := range errs {
    75  				if err.code < exit || exit == 0 {
    76  					exit = err.code
    77  				}
    78  				log.Log = log.New(os.Stderr)
    79  				if err.code == 1 {
    80  					log.WithError(err.err).Error(err.details)
    81  				} else {
    82  					log.WithError(err.err).Warn(err.details)
    83  				}
    84  			}
    85  
    86  			if exit > 0 {
    87  				return wrapErrorWithCode(fmt.Errorf("%d out of %d configuration file(s) have issues", len(errs), len(args)), exit, "")
    88  			}
    89  
    90  			log.Info(boldStyle.Render(fmt.Sprintf("%d configuration file(s) validated", len(args))))
    91  			return nil
    92  		},
    93  	}
    94  
    95  	cmd.Flags().StringVarP(&root.config, "config", "f", "", "Configuration file(s) to check")
    96  	_ = cmd.MarkFlagFilename("config", "yaml", "yml")
    97  	cmd.Flags().BoolVarP(&root.quiet, "quiet", "q", false, "Quiet mode: no output")
    98  	cmd.Flags().BoolVar(&root.deprecated, "deprecated", false, "Force print the deprecation message - tests only")
    99  	_ = cmd.Flags().MarkHidden("deprecated")
   100  	_ = cmd.Flags().MarkHidden("config")
   101  
   102  	root.cmd = cmd
   103  	return root
   104  }