2020-05-15 16:19:20 +02:00
package cmd
import (
2021-04-21 21:36:53 +02:00
"fmt"
"os"
2022-02-05 23:19:12 +02:00
"path/filepath"
2021-01-13 19:21:04 +02:00
"runtime"
2022-06-29 04:37:38 +02:00
"strings"
2020-05-15 16:19:20 +02:00
"time"
"github.com/caarlos0/ctrlc"
2022-06-22 02:11:15 +02:00
"github.com/caarlos0/log"
2022-02-05 23:19:12 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2023-01-29 05:24:11 +02:00
"github.com/goreleaser/goreleaser/internal/deprecate"
2022-02-05 23:19:12 +02:00
"github.com/goreleaser/goreleaser/internal/gio"
2021-09-18 15:21:29 +02:00
"github.com/goreleaser/goreleaser/internal/middleware/errhandler"
"github.com/goreleaser/goreleaser/internal/middleware/logging"
"github.com/goreleaser/goreleaser/internal/middleware/skip"
2020-05-15 16:19:20 +02:00
"github.com/goreleaser/goreleaser/internal/pipeline"
2021-04-21 21:36:53 +02:00
"github.com/goreleaser/goreleaser/pkg/config"
2020-05-15 16:19:20 +02:00
"github.com/goreleaser/goreleaser/pkg/context"
2022-05-07 01:38:50 +02:00
"github.com/spf13/cobra"
2020-05-15 16:19:20 +02:00
)
type buildCmd struct {
2022-05-07 01:38:50 +02:00
cmd * cobra . Command
2020-05-15 16:19:20 +02:00
opts buildOpts
}
type buildOpts struct {
config string
2022-06-29 04:37:38 +02:00
ids [ ] string
2020-05-15 16:19:20 +02:00
snapshot bool
skipValidate bool
2022-06-23 02:56:53 +02:00
skipBefore bool
2020-05-15 16:19:20 +02:00
skipPostHooks bool
2023-01-21 04:47:08 +02:00
clean bool
2023-01-29 05:24:11 +02:00
rmDist bool // deprecated
2020-05-15 16:19:20 +02:00
deprecated bool
parallelism int
timeout time . Duration
2021-04-21 21:36:53 +02:00
singleTarget bool
2022-02-05 23:19:12 +02:00
output string
2020-05-15 16:19:20 +02:00
}
func newBuildCmd ( ) * buildCmd {
2021-04-21 21:36:53 +02:00
root := & buildCmd { }
2020-05-15 16:19:20 +02:00
// nolint: dupl
2022-05-07 01:38:50 +02:00
cmd := & cobra . Command {
2021-04-25 19:20:49 +02:00
Use : "build" ,
Aliases : [ ] string { "b" } ,
Short : "Builds the current project" ,
2022-04-16 20:31:43 +02:00
Long : ` The ` + "`goreleaser build`" + ` command is analogous to the ` + "`go build`" + ` command , in the sense it only builds binaries .
2021-04-22 15:58:58 +02:00
2022-04-20 00:34:24 +02:00
Its intended usage is , for example , within Makefiles to avoid setting up ldflags and etc in several places . That way , the GoReleaser config becomes the source of truth for how the binaries should be built .
2021-04-22 15:58:58 +02:00
2022-04-16 20:31:43 +02:00
It also allows you to generate a local build for your current machine only using the ` + " ` -- single - target ` " + ` option , and specific build IDs using the ` + " ` -- id ` " + ` option in case you have more than one .
2021-11-30 15:41:05 +02:00
2022-04-20 00:34:24 +02:00
When using ` + " ` -- single - target ` " + ` , the ` + " ` GOOS ` " + ` and ` + " ` GOARCH ` " + ` environment variables are used to determine the target , defaulting to the current machine target if not set .
2021-04-22 15:58:58 +02:00
` ,
2020-05-15 16:19:20 +02:00
SilenceUsage : true ,
SilenceErrors : true ,
2022-05-07 01:38:50 +02:00
Args : cobra . NoArgs ,
2022-06-22 06:48:11 +02:00
RunE : timedRunE ( "build" , func ( cmd * cobra . Command , args [ ] string ) error {
2020-05-15 16:19:20 +02:00
ctx , err := buildProject ( root . opts )
if err != nil {
2022-06-22 06:48:11 +02:00
return err
2020-05-15 16:19:20 +02:00
}
2022-06-22 06:48:11 +02:00
deprecateWarn ( ctx )
2020-05-15 16:19:20 +02:00
return nil
2022-06-22 06:48:11 +02:00
} ) ,
2020-05-15 16:19:20 +02:00
}
cmd . Flags ( ) . StringVarP ( & root . opts . config , "config" , "f" , "" , "Load configuration from file" )
2021-04-22 15:58:58 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . snapshot , "snapshot" , false , "Generate an unversioned snapshot build, skipping all validations" )
2020-05-15 16:19:20 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipValidate , "skip-validate" , false , "Skips several sanity checks" )
2022-06-23 02:56:53 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipBefore , "skip-before" , false , "Skips global before hooks" )
2020-05-15 16:19:20 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipPostHooks , "skip-post-hooks" , false , "Skips all post-build hooks" )
2023-01-21 04:47:08 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . clean , "clean" , false , "Remove the dist folder before building" )
2023-01-29 05:24:11 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . rmDist , "rm-dist" , false , "Remove the dist folder before building" )
2021-04-22 15:45:36 +02:00
cmd . Flags ( ) . IntVarP ( & root . opts . parallelism , "parallelism" , "p" , 0 , "Amount tasks to run concurrently (default: number of CPUs)" )
2020-05-15 16:19:20 +02:00
cmd . Flags ( ) . DurationVar ( & root . opts . timeout , "timeout" , 30 * time . Minute , "Timeout to the entire build process" )
2022-07-29 03:29:01 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . singleTarget , "single-target" , false , "Builds only for current GOOS and GOARCH, regardless of what's set in the configuration file" )
2022-06-29 04:37:38 +02:00
cmd . Flags ( ) . StringArrayVar ( & root . opts . ids , "id" , nil , "Builds only the specified build ids" )
2020-05-15 16:19:20 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . deprecated , "deprecated" , false , "Force print the deprecation message - tests only" )
2022-07-29 03:29:01 +02:00
cmd . Flags ( ) . StringVarP ( & root . opts . output , "output" , "o" , "" , "Copy the binary to the path after the build. Only taken into account when using --single-target and a single id (either with --id or if configuration only has one build)" )
2022-10-06 11:24:13 +02:00
_ = cmd . Flags ( ) . SetAnnotation ( "output" , cobra . BashCompFilenameExt , [ ] string { "" } )
2023-01-21 04:47:08 +02:00
_ = cmd . Flags ( ) . MarkHidden ( "rm-dist" )
2020-05-15 16:19:20 +02:00
_ = cmd . Flags ( ) . MarkHidden ( "deprecated" )
root . cmd = cmd
return root
}
func buildProject ( options buildOpts ) ( * context . Context , error ) {
cfg , err := loadConfig ( options . config )
if err != nil {
return nil , err
}
ctx , cancel := context . NewWithTimeout ( cfg , options . timeout )
defer cancel ( )
2021-04-21 21:36:53 +02:00
if err := setupBuildContext ( ctx , options ) ; err != nil {
return nil , err
}
2020-05-15 16:19:20 +02:00
return ctx , ctrlc . Default . Run ( ctx , func ( ) error {
2022-02-05 23:19:12 +02:00
for _ , pipe := range setupPipeline ( ctx , options ) {
2021-09-18 15:21:29 +02:00
if err := skip . Maybe (
pipe ,
logging . Log (
pipe . String ( ) ,
errhandler . Handle ( pipe . Run ) ,
) ,
2020-05-15 16:19:20 +02:00
) ( ctx ) ; err != nil {
return err
}
}
return nil
} )
}
2022-02-05 23:19:12 +02:00
func setupPipeline ( ctx * context . Context , options buildOpts ) [ ] pipeline . Piper {
2022-06-29 04:37:38 +02:00
if options . output != "" && options . singleTarget && ( len ( options . ids ) > 0 || len ( ctx . Config . Builds ) == 1 ) {
2022-02-05 23:19:12 +02:00
return append ( pipeline . BuildCmdPipeline , withOutputPipe { options . output } )
}
return pipeline . BuildCmdPipeline
}
2021-04-21 21:36:53 +02:00
func setupBuildContext ( ctx * context . Context , options buildOpts ) error {
2023-01-29 05:24:11 +02:00
ctx . Deprecated = options . deprecated // test only
2021-04-22 15:45:36 +02:00
ctx . Parallelism = runtime . NumCPU ( )
if options . parallelism > 0 {
ctx . Parallelism = options . parallelism
}
2020-05-15 16:19:20 +02:00
log . Debugf ( "parallelism: %v" , ctx . Parallelism )
ctx . Snapshot = options . snapshot
ctx . SkipValidate = ctx . Snapshot || options . skipValidate
2022-06-23 02:56:53 +02:00
ctx . SkipBefore = options . skipBefore
2020-05-15 16:19:20 +02:00
ctx . SkipPostBuildHooks = options . skipPostHooks
ctx . SkipTokenCheck = true
2023-01-29 05:24:11 +02:00
ctx . Clean = options . clean || options . rmDist
if options . rmDist {
deprecate . NoticeCustom ( ctx , "-rm-dist" , "--rm-dist was deprecated in favor of --clean, check {{ .URL }} for more details" )
}
2020-05-15 16:19:20 +02:00
2021-04-21 21:36:53 +02:00
if options . singleTarget {
setupBuildSingleTarget ( ctx )
}
2022-06-29 04:37:38 +02:00
if len ( options . ids ) > 0 {
if err := setupBuildID ( ctx , options . ids ) ; err != nil {
2021-04-21 21:36:53 +02:00
return err
}
}
return nil
}
func setupBuildSingleTarget ( ctx * context . Context ) {
goos := os . Getenv ( "GOOS" )
if goos == "" {
goos = runtime . GOOS
}
goarch := os . Getenv ( "GOARCH" )
if goarch == "" {
goarch = runtime . GOARCH
}
2022-07-29 03:29:01 +02:00
log . WithField ( "reason" , "single target is enabled" ) . Warnf ( "building only for %s/%s" , goos , goarch )
2021-04-21 21:36:53 +02:00
if len ( ctx . Config . Builds ) == 0 {
ctx . Config . Builds = append ( ctx . Config . Builds , config . Build { } )
}
for i := range ctx . Config . Builds {
build := & ctx . Config . Builds [ i ]
build . Goos = [ ] string { goos }
build . Goarch = [ ] string { goarch }
2022-09-16 03:17:40 +02:00
build . Goarm = nil
build . Gomips = nil
build . Goamd64 = nil
build . Targets = nil
2021-04-21 21:36:53 +02:00
}
}
2022-06-29 04:37:38 +02:00
func setupBuildID ( ctx * context . Context , ids [ ] string ) error {
2021-04-21 21:36:53 +02:00
if len ( ctx . Config . Builds ) < 2 {
log . Warn ( "single build in config, '--id' ignored" )
return nil
}
var keep [ ] config . Build
for _ , build := range ctx . Config . Builds {
2022-06-29 04:37:38 +02:00
for _ , id := range ids {
if build . ID == id {
keep = append ( keep , build )
break
}
2021-04-21 21:36:53 +02:00
}
}
if len ( keep ) == 0 {
2022-06-29 04:37:38 +02:00
return fmt . Errorf ( "no builds with ids %s" , strings . Join ( ids , ", " ) )
2021-04-21 21:36:53 +02:00
}
ctx . Config . Builds = keep
return nil
2020-05-15 16:19:20 +02:00
}
2022-02-05 23:19:12 +02:00
// withOutputPipe copies the binary from dist to the specified output path.
type withOutputPipe struct {
output string
}
func ( w withOutputPipe ) String ( ) string {
return fmt . Sprintf ( "copying binary to %q" , w . output )
}
func ( w withOutputPipe ) Run ( ctx * context . Context ) error {
path := ctx . Artifacts . Filter ( artifact . ByType ( artifact . Binary ) ) . List ( ) [ 0 ] . Path
out := w . output
2022-02-19 20:10:37 +02:00
if out == "." {
2022-02-05 23:19:12 +02:00
out = filepath . Base ( path )
}
return gio . Copy ( path , out )
}