-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
252 lines (236 loc) · 6.23 KB
/
account.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"github.com/steverusso/lockbook-x/go-lockbook"
)
// Account related commands.
//
// clap:cmd_usage <command> [args...]
type acctCmd struct {
init *acctInitCmd
restore *acctRestoreCmd
privkey *acctPrivKeyCmd
status *acctStatusCmd
subscribe *acctSubscribeCmd
unsubscribe *acctUnsubscribeCmd
}
func (a *acctCmd) run(core lockbook.Core) error {
switch {
case a.init != nil:
return a.init.run(core)
case a.restore != nil:
return a.restore.run(core)
case a.privkey != nil:
return a.privkey.run(core)
case a.status != nil:
return a.status.run(core)
case a.subscribe != nil:
return a.subscribe.run(core)
case a.unsubscribe != nil:
return a.unsubscribe.run(core)
default:
return nil
}
}
// Create a lockbook account.
type acctInitCmd struct {
// Include the welcome document.
//
// clap:opt welcome
welcome bool
// Don't perform the initial sync.
//
// clap:opt no-sync
noSync bool
}
func (c *acctInitCmd) run(core lockbook.Core) error {
if isStdinPipe() {
return errors.New("cannot create a new account without ability to prompt for terminal input")
}
scnr := bufio.NewScanner(os.Stdin)
fmt.Printf("please choose a username: ")
if !scnr.Scan() {
return scnr.Err()
}
uname := scnr.Text()
apiURL := os.Getenv("API_URL")
if apiURL == "" {
apiURL = lockbook.DefaultAPILocation
}
fmt.Println("generating keys and checking for username availability...")
_, err := core.CreateAccount(uname, apiURL, c.welcome)
if err != nil {
return fmt.Errorf("creating account: %w", err)
}
fmt.Println("account created!")
if !c.noSync {
fmt.Print("doing initial sync... ")
if err := core.SyncAll(nil); err != nil {
return fmt.Errorf("syncing after init: %w", err)
}
fmt.Println("done")
}
return nil
}
// Restore an existing account from its secret account string.
//
// The restore command reads the secret account string from standard input (stdin).
// In other words, pipe your account string to this command like:
// 'cat lbkey.txt | lbcli acct restore'.
type acctRestoreCmd struct {
// Don't perform the initial sync.
//
// clap:opt no-sync
noSync bool
}
func (c *acctRestoreCmd) run(core lockbook.Core) error {
if !isStdinPipe() {
return errors.New("to restore an existing lockbook account, pipe your account string into this command, e.g.:\npbpaste | lockbook init --restore")
}
data, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("trying to read from stdin: %w", err)
}
if n := len(data); data[n-1] == '\n' {
data = data[:n-1]
}
_, err = core.ImportAccount(string(data))
if err != nil {
return fmt.Errorf("importing account: %w", err)
}
if !c.noSync {
fmt.Print("doing initial sync... ")
if err := core.SyncAll(nil); err != nil {
return fmt.Errorf("syncing after init: %w", err)
}
fmt.Println("done")
}
return nil
}
// Print out the private key for this lockbook.
//
// Your private key should always be kept secret and should only be displayed when you are
// in a secure location. For that reason, this command will prompt you before printing
// your private key just to double check. However, this prompt can be satisfied by passing
// the '--no-prompt' option.
type acctPrivKeyCmd struct {
// Don't require confirmation before displaying the private key.
//
// clap:opt no-prompt
noPrompt bool
}
func (c acctPrivKeyCmd) run(core lockbook.Core) error {
acctStr, err := core.ExportAccount()
if err != nil {
return fmt.Errorf("exporting account: %w", err)
}
if !c.noPrompt {
if isStdoutPipe() {
fmt.Fprintln(os.Stderr, "warning: use the `--no-prompt` option when piping your private key to stdout")
return nil
}
var answer string
fmt.Print("your private key is about to be visible. do you want to proceed? [y/N]: ")
fmt.Scanln(&answer)
if answer != "y" && answer != "Y" {
fmt.Println("aborted")
return nil
}
}
fmt.Println(acctStr)
return nil
}
// Overview of your account.
type acctStatusCmd struct{}
func (acctStatusCmd) run(core lockbook.Core) error {
u, err := core.GetUsage()
if err != nil {
return fmt.Errorf("getting usage: %w", err)
}
info, err := core.GetSubscriptionInfo()
if err != nil {
return fmt.Errorf("getting subscription info: %w", err)
}
switch {
case info.StripeLast4 != "":
fmt.Printf("type: Stripe, *%s\n", info.StripeLast4)
case info.GooglePlay != lockbook.GooglePlayNone:
fmt.Println("type: Google Play")
fmt.Printf("state: %s\n", info.GooglePlay)
case info.AppStore != lockbook.AppStoreNone:
fmt.Println("type: App Store")
fmt.Printf("state: %s\n", info.AppStore)
default:
fmt.Println("trial tier")
}
if !info.PeriodEnd.IsZero() {
fmt.Printf("renews on: %s\n", info.PeriodEnd)
}
pct := (u.ServerUsage.Exact * 100) / u.DataCap.Exact
fmt.Printf("data cap: %s, %d%% utilized\n", u.DataCap.Readable, pct)
return nil
}
// Create a new subscription with a credit card.
type acctSubscribeCmd struct{}
func (acctSubscribeCmd) run(core lockbook.Core) error {
fmt.Print("checking for existing card... ")
subInfo, err := core.GetSubscriptionInfo()
if err != nil {
return fmt.Errorf("getting subscription info: %w", err)
}
useNewCard := true
if subInfo.StripeLast4 != "" {
answer := ""
fmt.Printf("do you want to use card ending in *%s? [y/N]: ", subInfo.StripeLast4)
fmt.Scanln(&answer)
if answer == "y" || answer == "Y" {
useNewCard = false
}
} else {
fmt.Println("no existing cards found.")
}
var card *lockbook.CreditCard
if useNewCard {
card := &lockbook.CreditCard{}
fmt.Print("enter your card number: ")
fmt.Scanln(&card.Number)
for {
fmt.Print("expiration month: ")
_, err := fmt.Scanln("%d", &card.ExpiryMonth)
if err == nil {
break
}
fmt.Println(err)
}
for {
fmt.Print("expiration year: ")
_, err := fmt.Scanf("%d", &card.ExpiryYear)
if err == nil {
break
}
fmt.Println(err)
}
fmt.Print("cvc: ")
fmt.Scanln(&card.CVC)
}
if err = core.UpgradeViaStripe(card); err != nil {
return err
}
fmt.Println("subscribed!")
return nil
}
// Cancel an existing subscription.
type acctUnsubscribeCmd struct{}
func (acctUnsubscribeCmd) run(core lockbook.Core) error {
fmt.Print("cancelling subscription... ")
err := core.CancelSubscription()
if err != nil {
return err
}
fmt.Println("done")
return nil
}