-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgen.go
126 lines (111 loc) · 3.63 KB
/
gen.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package gen
import (
"fmt"
"os"
"github.com/hyperledger-labs/fabric-smart-client/integration"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/api"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc"
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
type Topology struct {
Type string `yaml:"type,omitempty"`
}
type Topologies struct {
Topologies []Topology `yaml:"topologies,omitempty"`
}
type T struct {
Topologies []interface{} `yaml:"topologies,omitempty"`
}
var topologyFile string
var output string
var port int
// Cmd returns the Cobra Command for Version
func Cmd() *cobra.Command {
// Set the flags on the node start command.
flags := cobraCommand.Flags()
flags.StringVarP(&topologyFile, "topology", "t", "", "topology file in yaml format")
flags.StringVarP(&output, "output", "o", "./testdata", "output folder")
flags.IntVarP(&port, "port", "p", 20000, "host starting port")
return cobraCommand
}
var cobraCommand = &cobra.Command{
Use: "artifacts",
Short: "Gen artifacts.",
Long: `Read topology from file and generates artifacts.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("trailing args detected")
}
// Parsing of the command line is done so silence cmd usage
cmd.SilenceUsage = true
return gen(args)
},
}
// gen read topology and generates artifacts
func gen(args []string) error {
if len(topologyFile) == 0 {
return errors.Errorf("expecting topology file path")
}
raw, err := os.ReadFile(topologyFile)
if err != nil {
return errors.Wrapf(err, "failed reading topology file [%s]", topologyFile)
}
names := &Topologies{}
if err := yaml.Unmarshal(raw, names); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t := &T{}
if err := yaml.Unmarshal(raw, t); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 := []api.Topology{}
for i, topology := range names.Topologies {
switch topology.Type {
case fabric.TopologyName:
top := fabric.NewDefaultTopology()
r, err := yaml.Marshal(t.Topologies[i])
if err != nil {
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
}
if err := yaml.Unmarshal(r, top); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 = append(t2, top)
case fsc.TopologyName:
top := fsc.NewTopology()
r, err := yaml.Marshal(t.Topologies[i])
if err != nil {
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
}
if err := yaml.Unmarshal(r, top); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 = append(t2, top)
case token.TopologyName:
top := token.NewTopology()
r, err := yaml.Marshal(t.Topologies[i])
if err != nil {
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
}
if err := yaml.Unmarshal(r, top); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 = append(t2, top)
}
}
network, err := integration.New(port, output, t2...)
if err != nil {
return errors.Wrapf(err, "cannot instantate integration infrastructure")
}
network.RegisterPlatformFactory(token.NewPlatformFactory())
network.Generate()
return nil
}