-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
106 lines (101 loc) · 2.91 KB
/
command.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"dyn-https/blockchain/dynamic"
"dyn-https/configs"
"dyn-https/configs/settings"
"dyn-https/https/rest"
"dyn-https/util"
"golang.org/x/crypto/ssh/terminal"
)
func unlockWallet(d *dynamic.Dynamicd) bool {
fmt.Print("wallet passphrase> ")
bytePassword, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
walletpassphrase = strings.Trim(string(bytePassword), "\r\n ")
err := d.UnlockWallet(walletpassphrase)
if err == nil {
util.Info.Println("Wallet unlocked.")
return true
}
util.Error.Println(err)
return false
}
func appCommandLoop(acc *[]dynamic.Account, al *dynamic.ActiveLinks,
shutdown *rest.AppShutdown, d *dynamic.Dynamicd,
status *dynamic.SyncStatus, sync bool, c *settings.Configuration) {
go func() {
var err error
var unlocked = false
for {
select {
default:
if !unlocked {
errUnlock := d.UnlockWallet("")
if errUnlock != nil {
util.Info.Println("Wallet locked.")
} else {
unlocked = true
}
}
reader := bufio.NewReader(os.Stdin)
fmt.Print(DefaultName + `> `)
cmdText, _ := reader.ReadString('\n')
if len(cmdText) > 1 {
cmdText = strings.Trim(cmdText, "\r\n ") //cmdText[:len(cmdText)-2]
}
if strings.HasPrefix(cmdText, "exit") || strings.HasPrefix(cmdText, "shutdown") || strings.HasPrefix(cmdText, "stop") {
util.Info.Println("Exit command. Stopping services.")
shutdown.ShutdownAppliction()
return
} else if strings.HasPrefix(cmdText, "unlock") {
unlocked = unlockWallet(d)
if unlocked {
al, err = d.GetActiveLinks(time.Second * 120)
if err != nil {
util.Error.Println("GetActiveLinks error", err)
} else {
util.Info.Printf("Found %v links\n", len(al.Links))
}
}
} else if strings.HasPrefix(cmdText, "dynamic-cli") {
req, errNewRequest := dynamic.NewRequest(cmdText)
if errNewRequest != nil {
util.Error.Println("Error:", errNewRequest)
} else {
strResp, _ := util.BeautifyJSON(<-d.ExecCmdRequest(req))
util.Info.Println(strResp)
}
} else if strings.HasPrefix(cmdText, "restart") {
rest.RestartWebServiceRouter()
} else if strings.HasPrefix(cmdText, "useradd") {
user, err := configs.AdminUserCommand(c, cmdText)
if err != nil {
util.Error.Println("User add command failed. %v\n", err)
} else {
util.Info.Printf("User %v successfully added or updated\n", user)
}
} else {
util.Warning.Println("Invalid command", cmdText)
status, err = d.GetSyncStatus()
}
status, err = d.GetSyncStatus()
if err != nil {
util.Error.Println("syncstatus unmarshal error", err)
} else {
if !sync {
util.Info.Println("Sync " + fmt.Sprintf("%f", status.SyncProgress*100) + " percent complete!")
if status.SyncProgress == 1 {
sync = true
}
}
}
case <-*shutdown.Close:
return
}
}
}()
}