1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

feat: allow to goreleaser check multiple files (#3980)

This will be more useful for goreleaser pro than for oss, but I thought
it might worth it having it in both versions.
This commit is contained in:
Carlos Alexandro Becker 2023-05-03 23:28:55 -03:00 committed by GitHub
parent e8be671703
commit 3707fe4d82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 26 deletions

View File

@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"io"
"os"
"github.com/caarlos0/ctrlc"
"github.com/caarlos0/log"
@ -21,48 +22,77 @@ type checkCmd struct {
func newCheckCmd() *checkCmd {
root := &checkCmd{}
cmd := &cobra.Command{
Use: "check",
Use: "check [configuration files]",
Aliases: []string{"c"},
Short: "Checks if configuration is valid",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
Args: cobra.ArbitraryArgs,
RunE: func(_ *cobra.Command, args []string) error {
if root.quiet {
log.Log = log.New(io.Discard)
}
cfg, err := loadConfig(root.config)
if err != nil {
return err
}
ctx := context.New(cfg)
ctx.Deprecated = root.deprecated
if err := ctrlc.Default.Run(ctx, func() error {
log.Info(boldStyle.Render("checking config..."))
return defaults.Pipe{}.Run(ctx)
}); err != nil {
log.WithError(err).Error(boldStyle.Render("config is invalid"))
return fmt.Errorf("invalid config: %w", err)
var errs []*exitError
if root.config != "" {
args = append(args, root.config)
}
if ctx.Deprecated {
return wrapErrorWithCode(
fmt.Errorf("config is valid, but uses deprecated properties, check logs above for details"),
2,
"",
)
for _, config := range args {
cfg, err := loadConfig(config)
if err != nil {
return err
}
ctx := context.New(cfg)
ctx.Deprecated = root.deprecated
if err := ctrlc.Default.Run(ctx, func() error {
log.Info(boldStyle.Render("checking config..."))
return defaults.Pipe{}.Run(ctx)
}); err != nil {
log.WithError(err).Error(boldStyle.Render("config is invalid"))
errs = append(errs, wrapErrorWithCode(
fmt.Errorf("configuration is invalid: %w", err),
1,
config,
))
}
if ctx.Deprecated {
errs = append(errs, wrapErrorWithCode(
fmt.Errorf("configuration is valid, but uses deprecated properties"),
2,
config,
))
}
}
log.Infof(boldStyle.Render("config is valid"))
exit := 0
for _, err := range errs {
if err.code < exit || exit == 0 {
exit = err.code
}
log.Log = log.New(os.Stderr)
if err.code == 1 {
log.WithError(err.err).Error(err.details)
} else {
log.WithError(err.err).Warn(err.details)
}
}
if exit > 0 {
return wrapErrorWithCode(fmt.Errorf("%d out of %d configuration file(s) have issues", len(errs), len(args)), exit, "")
}
log.Info(boldStyle.Render(fmt.Sprintf("%d configuration file(s) validated", len(args))))
return nil
},
}
cmd.Flags().StringVarP(&root.config, "config", "f", "", "Configuration file to check")
cmd.Flags().StringVarP(&root.config, "config", "f", "", "Configuration file(s) to check")
cmd.Flags().BoolVarP(&root.quiet, "quiet", "q", false, "Quiet mode: no output")
cmd.Flags().BoolVar(&root.deprecated, "deprecated", false, "Force print the deprecation message - tests only")
_ = cmd.Flags().MarkHidden("deprecated")
_ = cmd.Flags().MarkHidden("config")
root.cmd = cmd
return root

View File

@ -28,7 +28,7 @@ func TestCheckConfigUnmarshalError(t *testing.T) {
func TestCheckConfigInvalid(t *testing.T) {
cmd := newCheckCmd()
cmd.cmd.SetArgs([]string{"-f", "testdata/invalid.yml"})
require.EqualError(t, cmd.cmd.Execute(), "invalid config: found 2 builds with the ID 'a', please fix your config")
require.Error(t, cmd.cmd.Execute())
}
func TestCheckConfigInvalidQuiet(t *testing.T) {
@ -40,5 +40,5 @@ func TestCheckConfigInvalidQuiet(t *testing.T) {
func TestCheckConfigDeprecated(t *testing.T) {
cmd := newCheckCmd()
cmd.cmd.SetArgs([]string{"-f", "testdata/good.yml", "--deprecated"})
require.EqualError(t, cmd.cmd.Execute(), "config is valid, but uses deprecated properties, check logs above for details")
require.Error(t, cmd.cmd.Execute())
}