Skip to content

Commit

Permalink
[FAB-2493] Package up GOLANG source precisely
Browse files Browse the repository at this point in the history
This patch modifies the current GOLANG platform driver
for packaging the source code (used by the CLI, etc) to
be much more precise.  The current code more or less
grabs the entire GOPATH on the platform in which the
CLI is invoked.  The new code will only grab the bare
minimum of source files to build a specified package.

Fixes FAB-2493 for the fabric-peer component.

Change-Id: I048e4f654f2d13fe658dbe8153898020e2b2f4c7
Signed-off-by: Greg Haskins <gregory.haskins@gmail.com>
  • Loading branch information
ghaskins committed May 31, 2017
1 parent 3084bb0 commit 89719ab
Show file tree
Hide file tree
Showing 9 changed files with 542 additions and 139 deletions.
67 changes: 67 additions & 0 deletions core/chaincode/platforms/golang/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2017 - Greg Haskins <gregory.haskins@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package golang

import (
"os"
"path/filepath"
"strings"
)

type Env map[string]string

func getEnv() Env {
env := make(Env)
for _, entry := range os.Environ() {
tokens := strings.Split(entry, "=")
if len(tokens) > 1 {
env[tokens[0]] = tokens[1]
}
}

return env
}

func flattenEnv(env Env) []string {
result := make([]string, 0)
for k, v := range env {
result = append(result, k+"="+v)
}

return result
}

type Paths map[string]bool

func splitEnvPaths(value string) Paths {
_paths := filepath.SplitList(value)
paths := make(Paths)
for _, path := range _paths {
paths[path] = true
}
return paths
}

func flattenEnvPaths(paths Paths) string {

_paths := make([]string, 0)
for path, _ := range paths {
_paths = append(_paths, path)
}

return strings.Join(_paths, string(os.PathListSeparator))
}
29 changes: 29 additions & 0 deletions core/chaincode/platforms/golang/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2017 - Greg Haskins <gregory.haskins@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package golang

import (
"os"
"testing"

"github.com/docker/docker/pkg/testutil/assert"
)

func Test_splitEnvPath(t *testing.T) {
paths := splitEnvPaths("foo" + string(os.PathListSeparator) + "bar" + string(os.PathListSeparator) + "baz")
assert.Equal(t, len(paths), 3)
}
72 changes: 72 additions & 0 deletions core/chaincode/platforms/golang/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2017 - Greg Haskins <gregory.haskins@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package golang

import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"time"
)

// Logic inspired by: https://dave.cheney.net/2014/09/14/go-list-your-swiss-army-knife
func list(env Env, template, pkg string) ([]string, error) {

if env == nil {
env = getEnv()
}

var stdOut bytes.Buffer
var stdErr bytes.Buffer

cmd := exec.Command("go", "list", "-f", template, pkg)
cmd.Env = flattenEnv(env)
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
err := cmd.Start()

// Create a go routine that will wait for the command to finish
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()

select {
case <-time.After(60 * time.Second):
if err = cmd.Process.Kill(); err != nil {
return nil, fmt.Errorf("go list: failed to kill: %s", err)
} else {
return nil, errors.New("go list: timeout")
}
case err = <-done:
if err != nil {
return nil, fmt.Errorf("go list: failed with error: \"%s\"\n%s", err, string(stdErr.Bytes()))
}

return strings.Split(string(stdOut.Bytes()), "\n"), nil
}
}

func listDeps(env Env, pkg string) ([]string, error) {
return list(env, "{{ join .Deps \"\\n\"}}", pkg)
}

func listImports(env Env, pkg string) ([]string, error) {
return list(env, "{{ join .Imports \"\\n\"}}", pkg)
}
26 changes: 26 additions & 0 deletions core/chaincode/platforms/golang/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2017 - Greg Haskins <gregory.haskins@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package golang

import "testing"

func Test_listDeps(t *testing.T) {
_, err := listDeps(nil, "github.com/hyperledger/fabric/peer")
if err != nil {
t.Errorf("list failed: %s", err)
}
}
Loading

0 comments on commit 89719ab

Please sign in to comment.