/
init.go
108 lines (85 loc) · 2.56 KB
/
init.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
package cmd
import (
"fmt"
"io"
"os"
"github.com/elysiumstation/fury/cmd/furywallet/commands/cli"
"github.com/elysiumstation/fury/cmd/furywallet/commands/flags"
"github.com/elysiumstation/fury/cmd/furywallet/commands/printer"
"github.com/elysiumstation/fury/paths"
"github.com/elysiumstation/fury/wallet/service"
svcstore "github.com/elysiumstation/fury/wallet/service/store/v1"
"github.com/elysiumstation/fury/wallet/wallets"
"github.com/spf13/cobra"
)
var (
initLong = cli.LongDesc(`
Creates the folders, the configuration files and RSA keys needed by the service
to operate.
`)
initExample = cli.Examples(`
# Initialise the software
{{.Software}} init
# Re-initialise the software
{{.Software}} init --force
`)
)
type InitHandler func(home string, f *InitFlags) error
func NewCmdInit(w io.Writer, rf *RootFlags) *cobra.Command {
return BuildCmdInit(w, Init, rf)
}
func BuildCmdInit(w io.Writer, handler InitHandler, rf *RootFlags) *cobra.Command {
f := &InitFlags{}
cmd := &cobra.Command{
Use: "init",
Short: "Initialise the software",
Long: initLong,
Example: initExample,
RunE: func(_ *cobra.Command, _ []string) error {
if err := handler(rf.Home, f); err != nil {
return err
}
switch rf.Output {
case flags.InteractiveOutput:
PrintInitResponse(w)
}
return nil
},
}
cmd.Flags().BoolVarP(&f.Force,
"force", "f",
false,
"Overwrite exiting wallet configuration at the specified path",
)
return cmd
}
type InitFlags struct {
Force bool
TokensPassphraseFile string
}
func Init(home string, f *InitFlags) error {
walletStore, err := wallets.InitialiseStore(home, false)
if err != nil {
return fmt.Errorf("couldn't initialise wallets store: %w", err)
}
defer walletStore.Close()
furyPaths := paths.New(home)
svcStore, err := svcstore.InitialiseStore(furyPaths)
if err != nil {
return fmt.Errorf("couldn't initialise service store: %w", err)
}
if err = service.InitialiseService(svcStore, f.Force); err != nil {
return fmt.Errorf("couldn't initialise the service: %w", err)
}
return nil
}
func PrintInitResponse(w io.Writer) {
p := printer.NewInteractivePrinter(w)
str := p.String()
defer p.Print(str)
str.CheckMark().SuccessText("Initialisation succeeded").NextSection()
str.BlueArrow().InfoText("Create a wallet").NextLine()
str.Text("To create a wallet, use the following command:").NextSection()
str.Code(fmt.Sprintf("%s create --wallet \"YOUR_WALLET\"", os.Args[0])).NextSection()
str.Text("For more information, use ").Bold("--help").Text(" flag.").NextLine()
}