Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #409 from bringhub/development
Browse files Browse the repository at this point in the history
Added a ldflags option to the build command
  • Loading branch information
markbates authored May 1, 2017
2 parents 63eaadb + 7556236 commit d42baa8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion buffalo/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
Expand All @@ -25,6 +26,7 @@ var outputBinName string
var zipBin bool
var extractAssets bool
var hasDB bool
var ldflags string
var buildTags string

type builder struct {
Expand Down Expand Up @@ -343,7 +345,24 @@ func (b *builder) buildBin() error {
version = strings.TrimSpace(out.String())
}
}
buildArgs = append(buildArgs, "-ldflags", fmt.Sprintf("-X main.version=%s -X main.buildTime=%s", version, buildTime))

flags := []string{
fmt.Sprintf("-X main.version=%s", version),
fmt.Sprintf("-X main.buildTime=%s", buildTime),
}

// Add any additional ldflags passed in to the build args
if len(ldflags) > 0 {
if foundVersion, _ := regexp.MatchString("-X\\s+main.version=", ldflags); foundVersion {
return errors.New("the ldflag option '-X main.version=' is reserved for Buffalo use")
}
if foundBuildTime, _ := regexp.MatchString("-X\\s+main.buildTime=", ldflags); foundBuildTime {
return errors.New("the ldflag option '-X main.buildTime=' is reserved for Buffalo use")
}
flags = append(flags, ldflags)
}

buildArgs = append(buildArgs, "-ldflags", strings.Join(flags, " "))

return b.exec("go", buildArgs...)
}
Expand Down Expand Up @@ -401,6 +420,7 @@ func init() {
buildCmd.Flags().StringVarP(&buildTags, "tags", "t", "", "compile with specific build tags")
buildCmd.Flags().BoolVarP(&zipBin, "zip", "z", false, "zips the assets to the binary, this requires zip installed")
buildCmd.Flags().BoolVarP(&extractAssets, "extract-assets", "e", false, "extract the assets and put them in a distinct archive")
buildCmd.Flags().StringVar(&ldflags, "ldflags", "", "set any ldflags to be passed to the go build")
}

var buildMainTmpl = `package main
Expand Down

0 comments on commit d42baa8

Please sign in to comment.