1// SPDX-License-Identifier: ISC 2// Copyright (c) 2014-2020 Bitmark Inc. 3// Use of this source code is governed by an ISC 4// license that can be found in the LICENSE file. 5 6package main 7 8import ( 9 "fmt" 10 "os" 11 "path" 12 "strings" 13 14 "github.com/urfave/cli" 15 16 "github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration" 17) 18 19func runSetup(c *cli.Context) error { 20 21 m := c.App.Metadata["config"].(*metadata) 22 testnet := m.testnet 23 24 name, err := checkName(c.GlobalString("identity")) 25 if nil != err { 26 return err 27 } 28 29 connect, err := checkConnect(c.String("connect")) 30 if nil != err { 31 return err 32 } 33 34 description, err := checkDescription(c.String("description")) 35 if nil != err { 36 return err 37 } 38 39 seed, err := checkSeed(c.String("seed"), c.Bool("new"), m.testnet) 40 if nil != err { 41 return err 42 } 43 44 if m.verbose { 45 fmt.Fprintf(m.e, "config: %s\n", m.file) 46 fmt.Fprintf(m.e, "testnet: %t\n", testnet) 47 fmt.Fprintf(m.e, "connect: %s\n", connect) 48 fmt.Fprintf(m.e, "identity: %s\n", name) 49 fmt.Fprintf(m.e, "description: %s\n", description) 50 } 51 52 // Create the folder hierarchy for configuration if not existing 53 configDir := path.Dir(m.file) 54 d, err := checkFileExists(configDir) 55 if nil != err { 56 if err = os.MkdirAll(configDir, 0750); nil != err { 57 return err 58 } 59 } else if !d { 60 return fmt.Errorf("path: %q is not a directory", configDir) 61 } 62 63 config := &configuration.Configuration{ 64 DefaultIdentity: name, 65 TestNet: testnet, 66 Connections: strings.Split(connect, ","), 67 Identities: make(map[string]configuration.Identity), 68 } 69 70 password := c.GlobalString("password") 71 if "" == password { 72 password, err = promptNewPassword() 73 if nil != err { 74 return err 75 } 76 } 77 78 err = config.AddIdentity(name, description, seed, password) 79 if nil != err { 80 return err 81 } 82 83 m.config = config 84 m.save = true 85 86 return nil 87} 88