-
Notifications
You must be signed in to change notification settings - Fork 13
/
run-setup.go
87 lines (70 loc) · 1.8 KB
/
run-setup.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"path"
"strings"
"github.com/urfave/cli"
"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
)
func runSetup(c *cli.Context) error {
m := c.App.Metadata["config"].(*metadata)
testnet := m.testnet
name, err := checkName(c.GlobalString("identity"))
if nil != err {
return err
}
connect, err := checkConnect(c.String("connect"))
if nil != err {
return err
}
description, err := checkDescription(c.String("description"))
if nil != err {
return err
}
seed, err := checkSeed(c.String("seed"), c.Bool("new"), m.testnet)
if nil != err {
return err
}
if m.verbose {
fmt.Fprintf(m.e, "config: %s\n", m.file)
fmt.Fprintf(m.e, "testnet: %t\n", testnet)
fmt.Fprintf(m.e, "connect: %s\n", connect)
fmt.Fprintf(m.e, "identity: %s\n", name)
fmt.Fprintf(m.e, "description: %s\n", description)
}
// Create the folder hierarchy for configuration if not existing
configDir := path.Dir(m.file)
d, err := checkFileExists(configDir)
if nil != err {
if err = os.MkdirAll(configDir, 0750); nil != err {
return err
}
} else if !d {
return fmt.Errorf("path: %q is not a directory", configDir)
}
config := &configuration.Configuration{
DefaultIdentity: name,
TestNet: testnet,
Connections: strings.Split(connect, ","),
Identities: make(map[string]configuration.Identity),
}
password := c.GlobalString("password")
if "" == password {
password, err = promptNewPassword()
if nil != err {
return err
}
}
err = config.AddIdentity(name, description, seed, password)
if nil != err {
return err
}
m.config = config
m.save = true
return nil
}