This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
167 lines (141 loc) · 3.54 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
package cmd
import (
"fmt"
"github.com/arrow2nd/ishell/v2"
"github.com/garyburd/go-oauth/oauth"
)
func (cmd *Cmd) newAccountCmd() *ishell.Cmd {
accountCmd := &ishell.Cmd{
Name: "account",
Aliases: []string{"acc"},
Help: "manage and switch accounts",
LongHelp: createLongHelp(
"Manage and switch accounts used by twnyan.",
"acc",
"twnyan account <command>",
"",
),
}
accountCmd.AddCmd(&ishell.Cmd{
Name: "add",
Func: cmd.execAccountAddCmd,
Help: "add account",
LongHelp: createLongHelp(
"Add account to twnyan.",
"",
"account add",
"",
),
})
accountCmd.AddCmd(&ishell.Cmd{
Name: "remove",
Aliases: []string{"rm"},
Completer: cmd.accountNameCompleter,
Func: cmd.execAccountRemoveCmd,
Help: "remove account",
LongHelp: createLongHelp(
"Remove account from twnyan.",
"rm",
"account remove <username>",
"account remove nekochan",
),
})
accountCmd.AddCmd(&ishell.Cmd{
Name: "list",
Aliases: []string{"ls"},
Func: cmd.execAccountListCmd,
Help: "List accounts",
LongHelp: createLongHelp(
"Lists accounts added to twnyan.",
"ls",
"account list",
"",
),
})
accountCmd.AddCmd(&ishell.Cmd{
Name: "switch",
Aliases: []string{"sw"},
Completer: cmd.accountNameCompleter,
Func: cmd.execAccountSwitchCmd,
Help: "switch the account to use",
LongHelp: createLongHelp(
`Switch the account to be used.
(Only available in interactive mode)`,
"sw",
"account switch <username>",
"account switch nekochan",
),
})
return accountCmd
}
func (cmd *Cmd) accountNameCompleter([]string) []string {
items := []string{"main"}
for name := range cmd.config.Cred.Sub {
items = append(items, name)
}
return items
}
func (cmd *Cmd) execAccountAddCmd(c *ishell.Context) {
// Auth認証
newCred, screenName, err := cmd.twitter.Auth()
if err != nil {
cmd.showErrorMessage(err.Error())
return
}
// 追加して保存
cmd.config.Cred.Sub[screenName] = newCred
cmd.config.Save()
cmd.showMessage("ADDED", screenName, cmd.config.Color.Accent3)
}
func (cmd *Cmd) execAccountRemoveCmd(c *ishell.Context) {
screenName, err := cmd.parseAccountCmdArgs(c.Args)
if err != nil {
cmd.showErrorMessage(err.Error())
return
}
// 実行確認
msg := fmt.Sprintf("Remove account (%s) from twnyan?", screenName)
if ok := cmd.showExecutionConf(msg); !ok {
cmd.showMessage("CANCELED", "Interrupted", cmd.config.Color.Accent2)
return
}
// 認証情報を削除
switch screenName {
case "main":
cmd.config.Cred.Main = &oauth.Credentials{}
default:
delete(cmd.config.Cred.Sub, screenName)
}
cmd.config.Save()
cmd.showMessage("REMOVED", screenName, cmd.config.Color.Accent3)
}
func (cmd *Cmd) execAccountListCmd(c *ishell.Context) {
for _, name := range cmd.accountNameCompleter(nil) {
if name != "main" {
name = fmt.Sprintf("@%s", name)
}
fmt.Printf("- %s\n", name)
}
}
func (cmd *Cmd) execAccountSwitchCmd(c *ishell.Context) {
if cmd.checkCommandLineMode() {
return
}
screenName, err := cmd.parseAccountCmdArgs(c.Args)
if err != nil {
cmd.showErrorMessage(err.Error())
return
}
prevScreenName := cmd.twitter.OwnUser.ScreenName
// アカウントを切り替え
switch screenName {
case "main":
cmd.twitter.Init(cmd.config.Cred.Main)
default:
cmd.twitter.Init(cmd.config.Cred.Sub[screenName])
}
// デフォルトプロンプトを更新
cmd.setDefaultPrompt()
msg := fmt.Sprintf("%s -> %s", prevScreenName, cmd.twitter.OwnUser.ScreenName)
cmd.showMessage("SWITCHED", msg, cmd.config.Color.Accent3)
}