/
createconfig.go
54 lines (47 loc) · 1.65 KB
/
createconfig.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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package cmd
import (
"fmt"
"io/ioutil"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/pkg/log"
)
// createConfigCmd represents the create-config command
var createConfigCmd = &cobra.Command{
Use: "create-config [# output-file]",
Short: "Creates a yaml config using generated pub/pri key pair.",
Long: `Creates a yaml config using generated pub/pri key pair.`,
Run: func(cmd *cobra.Command, args []string) {
private, err := crypto.GenerateKey()
if err != nil {
log.L().Fatal("failed to create key pair", zap.Error(err))
}
priKeyBytes := private.Bytes()
pubKeyBytes := private.PublicKey().Bytes()
cfgStr := fmt.Sprintf(
`chain:
producerPrivKey: "%x"
producerPubKey: "%x"
`,
priKeyBytes,
pubKeyBytes,
)
if err := ioutil.WriteFile(_outputFile, []byte(cfgStr), 0666); err != nil {
log.L().Fatal("failed to write file", zap.Error(err))
}
},
}
var _outputFile string
func init() {
createConfigCmd.Flags().StringVarP(&_outputFile, "output-file", "o", "", "config output file")
if err := createConfigCmd.MarkFlagRequired("output-file"); err != nil {
log.L().Fatal(err.Error())
}
rootCmd.AddCommand(createConfigCmd)
}