Skip to content

Commit d9e9b2c

Browse files
committed
splitting the peer into a client and a server
Signed-off-by: Fedor Partanskiy <fredprtnsk@gmail.com>
1 parent cdd7bea commit d9e9b2c

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

cmd/cli/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package main
8+
9+
import (
10+
_ "net/http/pprof"
11+
"os"
12+
"strings"
13+
14+
"github.com/hyperledger/fabric-lib-go/bccsp/factory"
15+
"github.com/hyperledger/fabric/internal/peer/chaincode"
16+
"github.com/hyperledger/fabric/internal/peer/channel"
17+
"github.com/hyperledger/fabric/internal/peer/common"
18+
"github.com/hyperledger/fabric/internal/peer/lifecycle"
19+
"github.com/hyperledger/fabric/internal/peer/snapshot"
20+
"github.com/hyperledger/fabric/internal/peer/version"
21+
"github.com/spf13/cobra"
22+
"github.com/spf13/viper"
23+
)
24+
25+
// The main command describes the service and
26+
// defaults to printing the help message.
27+
var mainCmd = &cobra.Command{Use: "cli"}
28+
29+
func main() {
30+
setEnvConfig(viper.GetViper())
31+
32+
// Define command-line flags that are valid for all cli commands and
33+
// subcommands.
34+
mainFlags := mainCmd.PersistentFlags()
35+
36+
mainFlags.String("logging-level", "", "Legacy logging level flag")
37+
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
38+
mainFlags.MarkHidden("logging-level")
39+
40+
cryptoProvider := factory.GetDefault()
41+
42+
mainCmd.AddCommand(version.Cmd())
43+
mainCmd.AddCommand(chaincode.Cmd(nil, cryptoProvider))
44+
mainCmd.AddCommand(channel.Cmd(nil))
45+
mainCmd.AddCommand(lifecycle.Cmd(cryptoProvider))
46+
mainCmd.AddCommand(snapshot.Cmd(cryptoProvider))
47+
48+
// On failure Cobra prints the usage message and error string, so we only
49+
// need to exit with a non-0 status
50+
if mainCmd.Execute() != nil {
51+
os.Exit(1)
52+
}
53+
}
54+
55+
func setEnvConfig(v *viper.Viper) {
56+
v.SetEnvPrefix(common.CmdRoot)
57+
v.AllowEmptyEnv(true)
58+
v.AutomaticEnv()
59+
replacer := strings.NewReplacer(".", "_")
60+
v.SetEnvKeyReplacer(replacer)
61+
}

cmd/cli/main_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package main
8+
9+
import (
10+
"testing"
11+
12+
"github.com/spf13/viper"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestSetEnvConfig(t *testing.T) {
18+
vp := viper.New()
19+
t.Setenv("CORE_FRUIT", "Apple")
20+
t.Setenv("CORE_COLOR", "")
21+
err := vp.BindEnv("Fruit")
22+
require.NoError(t, err)
23+
err = vp.BindEnv("Color")
24+
require.NoError(t, err)
25+
vp.SetDefault("Color", "Green")
26+
27+
setEnvConfig(vp)
28+
29+
assert.Equal(t, "Apple", vp.Get("Fruit"))
30+
assert.Equal(t, "", vp.Get("Color"))
31+
}

cmd/peer/main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ import (
1111
"os"
1212
"strings"
1313

14-
"github.com/hyperledger/fabric-lib-go/bccsp/factory"
15-
"github.com/hyperledger/fabric/internal/peer/chaincode"
16-
"github.com/hyperledger/fabric/internal/peer/channel"
1714
"github.com/hyperledger/fabric/internal/peer/common"
18-
"github.com/hyperledger/fabric/internal/peer/lifecycle"
1915
"github.com/hyperledger/fabric/internal/peer/node"
20-
"github.com/hyperledger/fabric/internal/peer/snapshot"
2116
"github.com/hyperledger/fabric/internal/peer/version"
2217
"github.com/spf13/cobra"
2318
"github.com/spf13/viper"
@@ -38,14 +33,8 @@ func main() {
3833
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
3934
mainFlags.MarkHidden("logging-level")
4035

41-
cryptoProvider := factory.GetDefault()
42-
4336
mainCmd.AddCommand(version.Cmd())
4437
mainCmd.AddCommand(node.Cmd())
45-
mainCmd.AddCommand(chaincode.Cmd(nil, cryptoProvider))
46-
mainCmd.AddCommand(channel.Cmd(nil))
47-
mainCmd.AddCommand(lifecycle.Cmd(cryptoProvider))
48-
mainCmd.AddCommand(snapshot.Cmd(cryptoProvider))
4938

5039
// On failure Cobra prints the usage message and error string, so we only
5140
// need to exit with a non-0 status

0 commit comments

Comments
 (0)