github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/cmd/check.go (about)

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