forked from hyperledger/fabric-sdk-go
/
list.go
149 lines (126 loc) · 3.42 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package golang
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
)
const listTimeout = 3 * time.Minute
// PackageInfo is the subset of data from `go list -deps -json` that's
// necessary to calculate chaincode package dependencies.
type PackageInfo struct {
ImportPath string
Dir string
GoFiles []string
Goroot bool
CFiles []string
CgoFiles []string
HFiles []string
SFiles []string
IgnoredGoFiles []string
Incomplete bool
}
func (p PackageInfo) Files() []string {
var files []string
files = append(files, p.GoFiles...)
files = append(files, p.CFiles...)
files = append(files, p.CgoFiles...)
files = append(files, p.HFiles...)
files = append(files, p.SFiles...)
files = append(files, p.IgnoredGoFiles...)
return files
}
// gopathDependencyPackageInfo extracts dependency information for
// specified package.
func gopathDependencyPackageInfo(goos, goarch, pkg string) ([]PackageInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), listTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "go", "list", "-deps", "-json", pkg)
cmd.Env = append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, wrapExitErr(err, "'go list -deps' failed")
}
decoder := json.NewDecoder(stdout)
err = cmd.Start()
if err != nil {
return nil, err
}
var list []PackageInfo
for {
var packageInfo PackageInfo
err := decoder.Decode(&packageInfo)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if packageInfo.Incomplete {
return nil, fmt.Errorf("failed to calculate dependencies: incomplete package: %s", packageInfo.ImportPath)
}
if packageInfo.Goroot {
continue
}
list = append(list, packageInfo)
}
err = cmd.Wait()
if err != nil {
return nil, errors.Wrapf(err, "listing deps for package %s failed", pkg)
}
return list, nil
}
func wrapExitErr(err error, message string) error {
if ee, ok := err.(*exec.ExitError); ok {
return errors.Wrapf(err, message+" with: %s", strings.TrimRight(string(ee.Stderr), "\n\r\t"))
}
return errors.Wrap(err, message)
}
type ModuleInfo struct {
Dir string
GoMod string
ImportPath string
ModulePath string
}
// listModuleInfo extracts module information for the curent working directory.
func listModuleInfo(extraEnv ...string) (*ModuleInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), listTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "go", "list", "-json", ".")
cmd.Env = append(os.Environ(), "GO111MODULE=on")
cmd.Env = append(cmd.Env, extraEnv...)
output, err := cmd.Output()
if err != nil {
return nil, wrapExitErr(err, "'go list' failed")
}
var moduleData struct {
ImportPath string
Module struct {
Dir string
Path string
GoMod string
}
}
if err := json.Unmarshal(output, &moduleData); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal output from 'go list'")
}
return &ModuleInfo{
Dir: moduleData.Module.Dir,
GoMod: moduleData.Module.GoMod,
ImportPath: moduleData.ImportPath,
ModulePath: moduleData.Module.Path,
}, nil
}