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
/
root.go
149 lines (126 loc) · 3.31 KB
/
root.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
package cmd
import (
"fmt"
"os"
"github.com/arrow2nd/ishell/v2"
"github.com/arrow2nd/twnyan/config"
"github.com/arrow2nd/twnyan/twitter"
"github.com/arrow2nd/twnyan/view"
"github.com/spf13/pflag"
)
// Cmd コマンド管理
type Cmd struct {
shell *ishell.Shell
flagSet *pflag.FlagSet
config *config.Config
twitter *twitter.Twitter
view *view.View
}
// New 作成
func New() *Cmd {
config := config.New()
return &Cmd{
shell: ishell.New(),
flagSet: pflag.NewFlagSet("twnyan", pflag.ContinueOnError),
config: config,
twitter: twitter.New(),
view: view.New(config),
}
}
// Init 初期化
func (cmd *Cmd) Init() {
var err error
// コマンド・フラグを登録
cmd.registerCommands()
cmd.registerFlags()
// 設定ファイル読み込み
ok := cmd.config.Load()
// メインアカウントがない場合、認証
if !ok || cmd.config.Cred.Main.Token == "" || cmd.config.Cred.Main.Secret == "" {
if cmd.config.Cred.Main, _, err = cmd.twitter.Auth(); err != nil {
cmd.showErrorMessage(err.Error())
os.Exit(1)
}
cmd.config.Save()
}
// フラグをパース
if err := cmd.flagSet.Parse(os.Args[1:]); err != nil {
cmd.showErrorMessage(err.Error())
os.Exit(1)
}
// ログイン
if err = cmd.login(); err != nil {
cmd.showErrorMessage(err.Error())
os.Exit(1)
}
cmd.setDefaultPrompt()
}
// Run 実行
func (cmd *Cmd) Run() {
// ヘルプの表示
if ok, _ := cmd.flagSet.GetBool("help"); ok {
fmt.Print(cmd.shell.HelpText())
cmd.flagSet.Usage()
return
}
// 対話モードで実行
if cmd.flagSet.NArg() == 0 {
cmd.shell.Process("timeline")
cmd.shell.Run()
return
}
// 直接実行
if err := cmd.shell.Process(cmd.flagSet.Args()...); err != nil {
os.Exit(1)
}
}
func (cmd *Cmd) registerFlags() {
// フラグを登録
cmd.flagSet.StringP("account", "A", "", "specify the account to use")
cmd.flagSet.BoolP("help", "H", false, "display help with options")
// フラグのヘルプ表示
cmd.flagSet.Usage = func() {
fmt.Println("Flags:")
cmd.flagSet.PrintDefaults()
fmt.Print("\n")
}
}
func (cmd *Cmd) registerCommands() {
// コマンドを登録
cmd.shell.AddCmd(cmd.newAccountCmd())
cmd.shell.AddCmd(cmd.newTweetCmd())
cmd.shell.AddCmd(cmd.newReplyCmd())
cmd.shell.AddCmd(cmd.newTimelineCmd())
cmd.shell.AddCmd(cmd.newMentionCmd())
cmd.shell.AddCmd(cmd.newListCmd())
cmd.shell.AddCmd(cmd.newSearchCmd())
cmd.shell.AddCmd(cmd.newUserCmd())
cmd.shell.AddCmd(cmd.newLikeCmd())
cmd.shell.AddCmd(cmd.newRetweetCmd())
cmd.shell.AddCmd(cmd.newLikertCmd())
cmd.shell.AddCmd(cmd.newQuoteCmd())
cmd.shell.AddCmd(cmd.newFollowCmd())
cmd.shell.AddCmd(cmd.newBlockCmd())
cmd.shell.AddCmd(cmd.newMuteCmd())
cmd.shell.AddCmd(cmd.newOpenCmd())
cmd.shell.AddCmd(cmd.newStreamCmd())
cmd.shell.AddCmd(cmd.newVersionCmd())
// コマンドエラー時の表示
cmd.shell.NotFound(func(c *ishell.Context) {
cmd.showErrorMessage(fmt.Sprintf("command not found: %s", c.Args[0]))
})
}
func (cmd *Cmd) login() error {
screenName, _ := cmd.flagSet.GetString("account")
// メインアカウント
if screenName == "" {
cmd.twitter.Init(cmd.config.Cred.Main)
return nil
}
// サブアカウント
if cred, ok := cmd.config.Cred.Sub[screenName]; ok {
cmd.twitter.Init(cred)
return nil
}
return fmt.Errorf("account does not exist: %s", screenName)
}