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

     1  package cmd
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	"github.com/apex/log"
     8  	"github.com/caarlos0/ctrlc"
     9  	"github.com/fatih/color"
    10  	"github.com/goreleaser/goreleaser/internal/middleware"
    11  	"github.com/goreleaser/goreleaser/internal/pipeline"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type buildCmd struct {
    17  	cmd  *cobra.Command
    18  	opts buildOpts
    19  }
    20  
    21  type buildOpts struct {
    22  	config        string
    23  	buildIDs      []string
    24  	snapshot      bool
    25  	skipValidate  bool
    26  	skipPostHooks bool
    27  	rmDist        bool
    28  	deprecated    bool
    29  	parallelism   int
    30  	timeout       time.Duration
    31  }
    32  
    33  func newBuildCmd() *buildCmd {
    34  	var root = &buildCmd{}
    35  	// nolint: dupl
    36  	var cmd = &cobra.Command{
    37  		Use:           "build",
    38  		Aliases:       []string{"b"},
    39  		Short:         "Builds the current project",
    40  		SilenceUsage:  true,
    41  		SilenceErrors: true,
    42  		Args:          cobra.NoArgs,
    43  		RunE: func(cmd *cobra.Command, args []string) error {
    44  			start := time.Now()
    45  
    46  			log.Infof(color.New(color.Bold).Sprint("building..."))
    47  
    48  			ctx, err := buildProject(root.opts)
    49  			if err != nil {
    50  				return wrapError(err, color.New(color.Bold).Sprintf("build failed after %0.2fs", time.Since(start).Seconds()))
    51  			}
    52  
    53  			if ctx.Deprecated {
    54  				log.Warn(color.New(color.Bold).Sprintf("your config is using deprecated properties, check logs above for details"))
    55  			}
    56  
    57  			log.Infof(color.New(color.Bold).Sprintf("build succeeded after %0.2fs", time.Since(start).Seconds()))
    58  			return nil
    59  		},
    60  	}
    61  
    62  	cmd.Flags().StringVarP(&root.opts.config, "config", "f", "", "Load configuration from file")
    63  	cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot build, skipping all validations and without publishing any artifacts")
    64  	cmd.Flags().BoolVar(&root.opts.skipValidate, "skip-validate", false, "Skips several sanity checks")
    65  	cmd.Flags().BoolVar(&root.opts.skipPostHooks, "skip-post-hooks", false, "Skips all post-build hooks")
    66  	cmd.Flags().BoolVar(&root.opts.rmDist, "rm-dist", false, "Remove the dist folder before building")
    67  	cmd.Flags().IntVarP(&root.opts.parallelism, "parallelism", "p", runtime.NumCPU(), "Amount tasks to run concurrently")
    68  	cmd.Flags().DurationVar(&root.opts.timeout, "timeout", 30*time.Minute, "Timeout to the entire build process")
    69  	cmd.Flags().StringSliceVar(&root.opts.buildIDs, "build-id", nil, "Build only the passed IDs (default empty). This is specified as a comma-separated list of IDs.")
    70  	cmd.Flags().BoolVar(&root.opts.deprecated, "deprecated", false, "Force print the deprecation message - tests only")
    71  	_ = cmd.Flags().MarkHidden("deprecated")
    72  
    73  	root.cmd = cmd
    74  	return root
    75  }
    76  
    77  func buildProject(options buildOpts) (*context.Context, error) {
    78  	cfg, err := loadConfig(options.config)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	ctx, cancel := context.NewWithTimeout(cfg, options.timeout)
    83  	defer cancel()
    84  	setupBuildContext(ctx, options)
    85  	return ctx, ctrlc.Default.Run(ctx, func() error {
    86  		for _, pipe := range pipeline.BuildPipeline {
    87  			if err := middleware.Logging(
    88  				pipe.String(),
    89  				middleware.ErrHandler(pipe.Run),
    90  				middleware.DefaultInitialPadding,
    91  			)(ctx); err != nil {
    92  				return err
    93  			}
    94  		}
    95  		return nil
    96  	})
    97  }
    98  
    99  func setupBuildContext(ctx *context.Context, options buildOpts) *context.Context {
   100  	ctx.Parallelism = options.parallelism
   101  	log.Debugf("parallelism: %v", ctx.Parallelism)
   102  	ctx.Snapshot = options.snapshot
   103  	ctx.SkipValidate = ctx.Snapshot || options.skipValidate
   104  	ctx.SkipPostBuildHooks = options.skipPostHooks
   105  	ctx.RmDist = options.rmDist
   106  	ctx.SkipTokenCheck = true
   107  	ctx.BuildIDs = options.buildIDs
   108  
   109  	// test only
   110  	ctx.Deprecated = options.deprecated
   111  	return ctx
   112  }