forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subst.go
93 lines (78 loc) · 2.23 KB
/
subst.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package config
import (
"bytes"
"go/build"
"path/filepath"
"strings"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
)
// goPath returns the current GOPATH. If the system
// has multiple GOPATHs then the first is used.
func goPath() string {
gpDefault := build.Default.GOPATH
gps := filepath.SplitList(gpDefault)
return gps[0]
}
// SubstPathVars replaces instances of '${VARNAME}' (eg ${GOPATH}) with the variable.
// As a special case, $GOPATH is also replaced.
// NOTE: this function currently only performs substitution when the path string starts with $
// as the path variables are intended to assist with testing.
func SubstPathVars(path string) string {
if !strings.HasPrefix(path, "$") {
return path
}
splits := strings.Split(path, "$")
// Due to the first check above, the following code is currently not possible:
//if len(splits) == 1 && path == splits[0] {
// // No variables are in the path
// return path
//}
var buffer bytes.Buffer
buffer.WriteString(splits[0]) // first split precedes the first $ so should always be written
for _, s := range splits[1:] {
// special case for GOPATH
if strings.HasPrefix(s, "GOPATH") {
buffer.WriteString(goPath())
buffer.WriteString(s[6:]) // Skip "GOPATH"
continue
}
if !strings.HasPrefix(s, "{") {
// not a variable
buffer.WriteString("$")
buffer.WriteString(s)
continue
}
endPos := strings.Index(s, "}") // not worrying about embedded '{'
if endPos == -1 {
// not a variable
buffer.WriteString("$")
buffer.WriteString(s)
continue
}
subs, ok := substVar(s[1:endPos]) // fix if not ASCII variable names
if !ok {
// not a variable
buffer.WriteString("$")
buffer.WriteString(s)
continue
}
buffer.WriteString(subs)
buffer.WriteString(s[endPos+1:]) // fix if not ASCII variable names
}
return buffer.String()
}
// substVar returns the substituted variable
func substVar(v string) (s string, ok bool) {
// TODO: optimize if the number of variable names grows
switch v {
case "GOPATH":
return goPath(), true
case "CRYPTOCONFIG_FIXTURES_PATH":
return metadata.CryptoConfigPath, true
}
return "", false
}