github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/fn/langs/go.go (about)

     1  package langs
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  )
     9  
    10  type GoLangHelper struct {
    11  	BaseHelper
    12  }
    13  
    14  func (lh *GoLangHelper) Entrypoint() string {
    15  	return "./func"
    16  }
    17  
    18  func (lh *GoLangHelper) HasPreBuild() bool {
    19  	return true
    20  }
    21  
    22  // PreBuild for Go builds the binary so the final image can be as small as possible
    23  func (lh *GoLangHelper) PreBuild() error {
    24  	wd, err := os.Getwd()
    25  	if err != nil {
    26  		return err
    27  	}
    28  	// todo: this won't work if the function is more complex since the import paths won't match up, need to fix
    29  	pbcmd := fmt.Sprintf("docker run --rm -v %s:/go/src/github.com/x/y -w /go/src/github.com/x/y iron/go:dev go build -o func", wd)
    30  	fmt.Println("Running prebuild command:", pbcmd)
    31  	parts := strings.Fields(pbcmd)
    32  	head := parts[0]
    33  	parts = parts[1:len(parts)]
    34  	cmd := exec.Command(head, parts...)
    35  	// cmd.Dir = dir
    36  	cmd.Stderr = os.Stderr
    37  	cmd.Stdout = os.Stdout
    38  	if err := cmd.Run(); err != nil {
    39  		return fmt.Errorf("error running docker build: %v", err)
    40  	}
    41  	return nil
    42  }
    43  
    44  func (lh *GoLangHelper) AfterBuild() error {
    45  	return os.Remove("func")
    46  
    47  }