/
newfromstring.go
59 lines (49 loc) · 1.5 KB
/
newfromstring.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
package svcdef
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Reasno/trs/truss/execprotoc"
"github.com/pkg/errors"
)
// NewFromString creates a Svcdef from a string of a valid protobuf file. Very
// useful in tests.
func NewFromString(def string, gopath []string) (*Svcdef, error) {
const defFileName = "definition.proto"
const goFileName = "definition.pb.go"
// Write our proto file to a directory
protoDir, err := ioutil.TempDir("", "trusssvcdef")
if err != nil {
return nil, errors.Wrap(err, "cannot create temp directory to store proto definition")
}
defer os.RemoveAll(protoDir)
defPath := filepath.Join(protoDir, defFileName)
err = ioutil.WriteFile(defPath, []byte(def), 0666)
if err != nil {
return nil, errors.Wrap(err, "cannot write proto definition to file")
}
// Create our pb.go file
err = execprotoc.GeneratePBDotGo([]string{defPath}, gopath, protoDir)
if err != nil {
return nil, errors.Wrap(err, "cannot create a pb.go file")
}
gPath := filepath.Join(protoDir, goFileName)
buf, err := ioutil.ReadFile(gPath)
if err != nil {
return nil, errors.Wrapf(err, "cannot read pb.go file %q", gPath)
}
pbgo := string(buf)
pbgoMap := map[string]io.Reader{
"/tmp/doesntexist.pb.go": strings.NewReader(pbgo),
}
pFileMap := map[string]io.Reader{
"/tmp/doesntexist.proto": strings.NewReader(def),
}
sd, err := New(pbgoMap, pFileMap)
if err != nil {
return nil, errors.Wrap(err, "cannot create new svcdef from pb.go and definition")
}
return sd, nil
}