github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/cmd/check.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/apex/log"
     8  	"github.com/apex/log/handlers/cli"
     9  	"github.com/caarlos0/ctrlc"
    10  	"github.com/fatih/color"
    11  	"github.com/goreleaser/goreleaser/internal/pipe/defaults"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type checkCmd struct {
    17  	cmd        *cobra.Command
    18  	config     string
    19  	quiet      bool
    20  	deprecated bool
    21  }
    22  
    23  func newCheckCmd() *checkCmd {
    24  	root := &checkCmd{}
    25  	cmd := &cobra.Command{
    26  		Use:           "check",
    27  		Aliases:       []string{"c"},
    28  		Short:         "Checks if configuration is valid",
    29  		SilenceUsage:  true,
    30  		SilenceErrors: true,
    31  		Args:          cobra.NoArgs,
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			if root.quiet {
    34  				log.SetHandler(cli.New(io.Discard))
    35  			}
    36  
    37  			cfg, err := loadConfig(root.config)
    38  			if err != nil {
    39  				return err
    40  			}
    41  			ctx := context.New(cfg)
    42  			ctx.Deprecated = root.deprecated
    43  
    44  			if err := ctrlc.Default.Run(ctx, func() error {
    45  				log.Info(color.New(color.Bold).Sprint("checking config:"))
    46  				return defaults.Pipe{}.Run(ctx)
    47  			}); err != nil {
    48  				log.WithError(err).Error(color.New(color.Bold).Sprintf("config is invalid"))
    49  				return fmt.Errorf("invalid config: %w", err)
    50  			}
    51  
    52  			if ctx.Deprecated {
    53  				return wrapErrorWithCode(
    54  					fmt.Errorf("config is valid, but uses deprecated properties, check logs above for details"),
    55  					2,
    56  					"",
    57  				)
    58  			}
    59  			log.Infof(color.New(color.Bold).Sprintf("config is valid"))
    60  			return nil
    61  		},
    62  	}
    63  
    64  	cmd.Flags().StringVarP(&root.config, "config", "f", "", "Configuration file to check")
    65  	cmd.Flags().BoolVarP(&root.quiet, "quiet", "q", false, "Quiet mode: no output")
    66  	cmd.Flags().BoolVar(&root.deprecated, "deprecated", false, "Force print the deprecation message - tests only")
    67  	_ = cmd.Flags().MarkHidden("deprecated")
    68  
    69  	root.cmd = cmd
    70  	return root
    71  }