/
predict.go
145 lines (134 loc) · 4.13 KB
/
predict.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package flag
import (
"context"
"flag"
"os"
"strings"
"time"
"github.com/posener/complete"
)
func predictAddress(fa bool, num int, flagName, suffix string) complete.PredictFunc {
if len(flagName) == 0 {
return func(a complete.Args) []string {
// Count the number of complete arguments that are not flags.
argc := len(a.Completed[1:])
for _, arg := range a.Completed[1:] {
if string(arg[0]) == "-" {
argc--
}
}
if len(suffix) > 0 && len(a.Last) > 0 &&
a.Last[len(a.Last)-1:len(a.Last)] == suffix {
return nil
}
if argc < num {
adrs := listAddresses(fa)
if len(suffix) > 0 {
for i := range adrs {
adrs[i] += suffix
}
}
return adrs
}
return nil
}
}
return func(a complete.Args) []string {
// Count the number of complete arguments that are not flags.
argc := 0
for i := len(a.Completed) - 1; i > 0; i-- {
arg := a.Completed[i]
if string(arg) == flagName {
break
}
argc++
}
if len(suffix) > 0 && len(a.Last) > 0 &&
a.Last[len(a.Last)-1:len(a.Last)] == suffix {
return nil
}
if argc < num {
adrs := listAddresses(fa)
if len(suffix) > 0 {
for i := range adrs {
adrs[i] += suffix
}
}
return adrs
}
return nil
}
}
func listAddresses(fa bool) []string {
parseWalletFlags()
fss, ess, err := FactomClient.GetPrivateAddresses(context.Background())
if err != nil {
os.Exit(6)
}
var adrStrs []string
if fa {
adrStrs = make([]string, len(fss))
for i, fs := range fss {
adrStrs[i] = fs.FAAddress().String()
}
} else {
adrStrs = make([]string, len(ess))
for i, es := range ess {
adrStrs[i] = es.ECAddress().String()
}
}
return adrStrs
}
var cliFlags *flag.FlagSet
// Parse any previously specified factom-cli options required for connecting to
// factom-walletd
func parseWalletFlags() {
if cliFlags != nil {
// We already parsed the flags.
return
}
// Using flag.FlagSet allows us to parse a custom array of flags
// instead of this programs args.
cliFlags = flag.NewFlagSet("", flag.ContinueOnError)
cliFlags.StringVar(&FactomClient.WalletdServer, "w", "localhost:8089", "")
cliFlags.StringVar(&FactomClient.Walletd.User, "walletuser", "", "")
cliFlags.StringVar(&FactomClient.Walletd.Password, "walletpassword", "", "")
// flags.Parse will print warnings if it comes across an unrecognized
// flag. We don't want this so we temprorarily redirect everything to
// /dev/null before we call flags.Parse().
stdout, stderr := os.Stdout, os.Stderr
devNull, err := os.Open(os.DevNull)
if err != nil {
os.Exit(5)
}
os.Stdout, os.Stderr = devNull, devNull
// The current command line being typed is stored in the environment
// variable COMP_LINE. We split on spaces and discard the first in the
// list because it is the program name `factom-cli`.
cliFlags.Parse(strings.Fields(os.Getenv("COMP_LINE"))[1:])
// Restore stdout and stderr.
os.Stdout, os.Stderr = stdout, stderr
// We need a short timeout or the CLI completion will hang.
FactomClient.Walletd.Timeout = time.Second / 2
}