Skip to content

Commit

Permalink
fix: do not copy binary by default
Browse files Browse the repository at this point in the history
closes #2913

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Feb 17, 2022
1 parent 14edcd3 commit 7da89e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
9 changes: 5 additions & 4 deletions cmd/build.go
Expand Up @@ -94,7 +94,7 @@ defaulting to the current's machine target if not set.
cmd.Flags().BoolVar(&root.opts.singleTarget, "single-target", false, "Builds only for current GOOS and GOARCH")
cmd.Flags().StringVar(&root.opts.id, "id", "", "Builds only the specified build id")
cmd.Flags().BoolVar(&root.opts.deprecated, "deprecated", false, "Force print the deprecation message - tests only")
cmd.Flags().StringVarP(&root.opts.output, "output", "o", "", "Path to the binary, defaults to the distribution folder according to configs. Only taked into account when using --single-target and a single id (either with --id or if config only has one build)")
cmd.Flags().StringVarP(&root.opts.output, "output", "o", "", "Copy the binary to thie path after the build. Only taked into account when using --single-target and a single id (either with --id or if config only has one build)")
_ = cmd.Flags().MarkHidden("deprecated")

root.cmd = cmd
Expand Down Expand Up @@ -129,7 +129,7 @@ func buildProject(options buildOpts) (*context.Context, error) {
}

func setupPipeline(ctx *context.Context, options buildOpts) []pipeline.Piper {
if options.singleTarget && (options.id != "" || len(ctx.Config.Builds) == 1) {
if options.output != "" && options.singleTarget && (options.id != "" || len(ctx.Config.Builds) == 1) {
return append(pipeline.BuildCmdPipeline, withOutputPipe{options.output})
}
return pipeline.BuildCmdPipeline
Expand Down Expand Up @@ -215,9 +215,10 @@ func (w withOutputPipe) String() string {

func (w withOutputPipe) Run(ctx *context.Context) error {
path := ctx.Artifacts.Filter(artifact.ByType(artifact.Binary)).List()[0].Path
name := filepath.Base(path)
out := w.output
if out == "" {
out = filepath.Base(path)
if out == "." {
out = name
}
return gio.Copy(path, out)
}
16 changes: 14 additions & 2 deletions cmd/build_test.go
Expand Up @@ -63,18 +63,30 @@ func TestSetupPipeline(t *testing.T) {
t.Run("single-target and id", func(t *testing.T) {
require.Equal(
t,
append(pipeline.BuildCmdPipeline, withOutputPipe{""}),
pipeline.BuildCmdPipeline,
setupPipeline(context.New(config.Project{}), buildOpts{
singleTarget: true,
id: "foo",
}),
)
})

t.Run("single-target and id, given output", func(t *testing.T) {
require.Equal(
t,
append(pipeline.BuildCmdPipeline, withOutputPipe{"foobar"}),
setupPipeline(context.New(config.Project{}), buildOpts{
singleTarget: true,
id: "foo",
output: ".",
}),
)
})

t.Run("single-target and single build on config", func(t *testing.T) {
require.Equal(
t,
append(pipeline.BuildCmdPipeline, withOutputPipe{""}),
pipeline.BuildCmdPipeline,
setupPipeline(
context.New(config.Project{
Builds: []config.Build{{}},
Expand Down

0 comments on commit 7da89e2

Please sign in to comment.