bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/build/build.go (about)

     1  // Simple script to build bosun and scollector. This is not required, but it will properly insert version date and commit
     2  // metadata into the resulting binaries, which `go build` will not do by default.
     3  package main
     4  
     5  import (
     6  	"flag"
     7  	"fmt"
     8  	"log"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  	"strings"
    13  	"time"
    14  )
    15  
    16  var (
    17  	shaFlag         = flag.String("sha", "", "SHA to embed.")
    18  	buildBosun      = flag.Bool("bosun", false, "Only build Bosun.")
    19  	buildTsdb       = flag.Bool("tsdbrelay", false, "Only build tsdbrelay")
    20  	buildScollector = flag.Bool("scollector", false, "Only build scollector.")
    21  	output          = flag.String("output", "", "Output directory; defaults to $GOPATH/bin.")
    22  
    23  	allProgs = []string{"bosun", "scollector", "tsdbrelay"}
    24  )
    25  
    26  func main() {
    27  	flag.Parse()
    28  	// Get current commit SHA
    29  	sha := *shaFlag
    30  	if sha == "" {
    31  		cmd := exec.Command("git", "rev-parse", "HEAD")
    32  		cmd.Stderr = os.Stderr
    33  		output, err := cmd.Output()
    34  		if err != nil {
    35  			log.Fatal(err)
    36  		}
    37  		sha = strings.TrimSpace(string(output))
    38  	}
    39  
    40  	timeStr := time.Now().UTC().Format("20060102150405")
    41  	ldFlags := fmt.Sprintf("-X bosun.org/_version.VersionSHA=%s -X bosun.org/_version.VersionDate=%s", sha, timeStr)
    42  
    43  	progs := allProgs
    44  	if *buildBosun {
    45  		progs = []string{"bosun"}
    46  	} else if *buildScollector {
    47  		progs = []string{"scollector"}
    48  	} else if *buildTsdb {
    49  		progs = []string{"tsdbrelay"}
    50  	}
    51  	for _, app := range progs {
    52  		fmt.Println("building", app)
    53  		var args []string
    54  		if *output != "" {
    55  			args = append(args, "build", "-o", filepath.Join(*output, app))
    56  		} else {
    57  			args = append(args, "install")
    58  		}
    59  		args = append(args, "-ldflags", ldFlags, fmt.Sprintf("bosun.org/cmd/%s", app))
    60  		fmt.Println("go", strings.Join(args, " "))
    61  		cmd := exec.Command("go", args...)
    62  		cmd.Stdout = os.Stdout
    63  		cmd.Stderr = os.Stderr
    64  		err := cmd.Run()
    65  		if err != nil {
    66  			log.Fatal(err)
    67  		}
    68  	}
    69  }