Skip to content

Commit 00b9a0b

Browse files
committed
Read executable to find the Go version of the binary file.
- Remove having to use a directory. - Check if it's a Linux binary. Signed-off-by: David Calavera <[email protected]>
1 parent b28b12f commit 00b9a0b

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

go/Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@
5656
[[constraint]]
5757
name = "github.com/stretchr/testify"
5858
version = "v1.1.4"
59+
60+
[[constraint]]
61+
name = "github.com/rsc/goversion"
62+
branch = "master"

go/porcelain/deploy.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"crypto/sha1"
77
"crypto/sha256"
8+
"debug/elf"
89
"encoding/hex"
910
"fmt"
1011
"io"
@@ -23,6 +24,7 @@ import (
2324
"github.com/netlify/open-api/go/porcelain/context"
2425

2526
"github.com/go-openapi/errors"
27+
"github.com/rsc/goversion/version"
2628
)
2729

2830
const (
@@ -501,51 +503,30 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) {
501503
if err != nil {
502504
return nil, err
503505
}
506+
504507
for _, i := range info {
508+
filePath := filepath.Join(functionDir, i.Name())
509+
505510
switch {
506511
case jsFile(i):
507-
file, err := newFunctionFile(functionDir, i, "js")
512+
file, err := newFunctionFile(filePath, i, "js")
508513
if err != nil {
509514
return nil, err
510515
}
511516
functions.Add(file.Name, file)
512-
513-
if observer != nil {
514-
if err := observer.OnSuccessfulStep(file); err != nil {
515-
return nil, err
516-
}
517-
}
518-
case goDir(i):
519-
wkg := filepath.Join(functionDir, i.Name())
520-
goInfo, err := ioutil.ReadDir(wkg)
517+
case goFile(filePath, i):
518+
file, err := newFunctionFile(filePath, i, "go")
521519
if err != nil {
522520
return nil, err
523521
}
524-
525-
for _, gi := range goInfo {
526-
if m := gi.Mode(); m&0111 != 0 {
527-
{
528-
file, err := newFunctionFile(wkg, gi, "go")
529-
if err != nil {
530-
return nil, err
531-
}
532-
functions.Add(file.Name, file)
533-
534-
if observer != nil {
535-
if err := observer.OnSuccessfulStep(file); err != nil {
536-
return nil, err
537-
}
538-
}
539-
}
540-
}
541-
}
522+
functions.Add(file.Name, file)
542523
}
543524
}
544525

545526
return functions, nil
546527
}
547528

548-
func newFunctionFile(dir string, i os.FileInfo, tp string) (*FileBundle, error) {
529+
func newFunctionFile(filePath string, i os.FileInfo, tp string, observer DeployObserver) (*FileBundle, error) {
549530
file := &FileBundle{
550531
Name: strings.TrimSuffix(i.Name(), filepath.Ext(i.Name())),
551532
Runtime: tp,
@@ -558,7 +539,7 @@ func newFunctionFile(dir string, i os.FileInfo, tp string) (*FileBundle, error)
558539
if err != nil {
559540
return nil, err
560541
}
561-
fileEntry, err := os.Open(filepath.Join(dir, i.Name()))
542+
fileEntry, err := os.Open(filePath)
562543
if err != nil {
563544
return nil, err
564545
}
@@ -580,15 +561,30 @@ func newFunctionFile(dir string, i os.FileInfo, tp string) (*FileBundle, error)
580561
file.Sum = hex.EncodeToString(s.Sum(nil))
581562
file.Buffer = bytes.NewReader(fileBuffer.Bytes())
582563

564+
if observer != nil {
565+
if err := observer.OnSuccessfulStep(file); err != nil {
566+
return nil, err
567+
}
568+
}
569+
583570
return file, nil
584571
}
585572

586573
func jsFile(i os.FileInfo) bool {
587574
return filepath.Ext(i.Name()) == ".js"
588575
}
589576

590-
func goDir(i os.FileInfo) bool {
591-
return i.IsDir() && i.Name() == "well-known-go"
577+
func goFile(filePath string, i os.FileInfo) bool {
578+
if m := i.Mode(); m&0111 == 0 { // check if it's an executable file
579+
return false
580+
}
581+
582+
if _, err := elf.Open(filePath); err != nil { // check if it's a linux executable
583+
return false
584+
}
585+
586+
v, err := version.ReadExe(filePath)
587+
return err == nil && strings.HasPrefix(v.Release, "1.")
592588
}
593589

594590
func ignoreFile(rel string) bool {

0 commit comments

Comments
 (0)