Skip to content

Commit 89719ab

Browse files
committed
[FAB-2493] Package up GOLANG source precisely
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 <[email protected]>
1 parent 3084bb0 commit 89719ab

File tree

9 files changed

+542
-139
lines changed

9 files changed

+542
-139
lines changed
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2017 - Greg Haskins <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package golang
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"strings"
23+
)
24+
25+
type Env map[string]string
26+
27+
func getEnv() Env {
28+
env := make(Env)
29+
for _, entry := range os.Environ() {
30+
tokens := strings.Split(entry, "=")
31+
if len(tokens) > 1 {
32+
env[tokens[0]] = tokens[1]
33+
}
34+
}
35+
36+
return env
37+
}
38+
39+
func flattenEnv(env Env) []string {
40+
result := make([]string, 0)
41+
for k, v := range env {
42+
result = append(result, k+"="+v)
43+
}
44+
45+
return result
46+
}
47+
48+
type Paths map[string]bool
49+
50+
func splitEnvPaths(value string) Paths {
51+
_paths := filepath.SplitList(value)
52+
paths := make(Paths)
53+
for _, path := range _paths {
54+
paths[path] = true
55+
}
56+
return paths
57+
}
58+
59+
func flattenEnvPaths(paths Paths) string {
60+
61+
_paths := make([]string, 0)
62+
for path, _ := range paths {
63+
_paths = append(_paths, path)
64+
}
65+
66+
return strings.Join(_paths, string(os.PathListSeparator))
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2017 - Greg Haskins <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package golang
18+
19+
import (
20+
"os"
21+
"testing"
22+
23+
"github.com/docker/docker/pkg/testutil/assert"
24+
)
25+
26+
func Test_splitEnvPath(t *testing.T) {
27+
paths := splitEnvPaths("foo" + string(os.PathListSeparator) + "bar" + string(os.PathListSeparator) + "baz")
28+
assert.Equal(t, len(paths), 3)
29+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2017 - Greg Haskins <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package golang
18+
19+
import (
20+
"bytes"
21+
"errors"
22+
"fmt"
23+
"os/exec"
24+
"strings"
25+
"time"
26+
)
27+
28+
// Logic inspired by: https://dave.cheney.net/2014/09/14/go-list-your-swiss-army-knife
29+
func list(env Env, template, pkg string) ([]string, error) {
30+
31+
if env == nil {
32+
env = getEnv()
33+
}
34+
35+
var stdOut bytes.Buffer
36+
var stdErr bytes.Buffer
37+
38+
cmd := exec.Command("go", "list", "-f", template, pkg)
39+
cmd.Env = flattenEnv(env)
40+
cmd.Stdout = &stdOut
41+
cmd.Stderr = &stdErr
42+
err := cmd.Start()
43+
44+
// Create a go routine that will wait for the command to finish
45+
done := make(chan error, 1)
46+
go func() {
47+
done <- cmd.Wait()
48+
}()
49+
50+
select {
51+
case <-time.After(60 * time.Second):
52+
if err = cmd.Process.Kill(); err != nil {
53+
return nil, fmt.Errorf("go list: failed to kill: %s", err)
54+
} else {
55+
return nil, errors.New("go list: timeout")
56+
}
57+
case err = <-done:
58+
if err != nil {
59+
return nil, fmt.Errorf("go list: failed with error: \"%s\"\n%s", err, string(stdErr.Bytes()))
60+
}
61+
62+
return strings.Split(string(stdOut.Bytes()), "\n"), nil
63+
}
64+
}
65+
66+
func listDeps(env Env, pkg string) ([]string, error) {
67+
return list(env, "{{ join .Deps \"\\n\"}}", pkg)
68+
}
69+
70+
func listImports(env Env, pkg string) ([]string, error) {
71+
return list(env, "{{ join .Imports \"\\n\"}}", pkg)
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2017 - Greg Haskins <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package golang
18+
19+
import "testing"
20+
21+
func Test_listDeps(t *testing.T) {
22+
_, err := listDeps(nil, "github.com/hyperledger/fabric/peer")
23+
if err != nil {
24+
t.Errorf("list failed: %s", err)
25+
}
26+
}

0 commit comments

Comments
 (0)