24
24
appUsage = "A simple command line RSS feed reader"
25
25
)
26
26
27
+ type exitCode int
28
+
29
+ const (
30
+ exitCodeOK exitCode = iota
31
+ exitCodeErrArgs
32
+ exitCodeErrFetchFeeds
33
+ exitCodeErrUrlEntry
34
+ exitCodeErrFuzzyFinder
35
+ exitCodeErrPager
36
+ exitCodeErrEditor
37
+ exitCodeErrBrowser
38
+ )
39
+
27
40
var urlFile = filepath .Join (configdir .LocalConfig (), appName , "urls.txt" )
28
41
29
42
// var cacheDBFile = path.Join(configdir.LocalCache(), appName, "cache.db")
@@ -47,7 +60,7 @@ func initApp() *cli.App {
47
60
},
48
61
Action : func (ctx * cli.Context ) error {
49
62
if ctx .NArg () == 0 {
50
- return errors . New ("must require arguments" )
63
+ return cli . Exit ("must require arguments" , int ( exitCodeErrArgs ) )
51
64
}
52
65
return nil
53
66
},
@@ -58,20 +71,38 @@ func initApp() *cli.App {
58
71
Usage : "Add url entry" ,
59
72
Action : func (ctx * cli.Context ) error {
60
73
if ctx .NArg () != 1 {
61
- return errors .New ("requires URL as an argument" )
74
+ return cli .Exit (
75
+ "requires URL as an argument" ,
76
+ int (exitCodeErrArgs ),
77
+ )
62
78
}
63
79
url := ctx .Args ().Get (0 )
64
80
if ! isValidURL (url ) {
65
- return fmt .Errorf ("invalid URL (%s)" , url )
81
+ return cli .Exit (
82
+ fmt .Sprintf ("invalid URL (%s)" , url ),
83
+ int (exitCodeErrUrlEntry ),
84
+ )
66
85
}
67
86
urls , err := readURLsFromEntry ()
68
87
if err != nil {
69
- return fmt .Errorf ("failed to read from URL entry file: %w" , err )
88
+ return cli .Exit (
89
+ fmt .Sprintf ("failed to read URL entry file (%s)" , url ),
90
+ int (exitCodeErrUrlEntry ),
91
+ )
70
92
}
71
93
if ! isUniqueURL (urls , url ) {
72
- return fmt .Errorf ("the URL is already registered: %s" , url )
94
+ return cli .Exit (
95
+ fmt .Sprintf ("the URL is already registered (%s)" , url ),
96
+ int (exitCodeErrUrlEntry ),
97
+ )
73
98
}
74
- return addURLEntry (url )
99
+ if err := addURLEntry (url ); err != nil {
100
+ return cli .Exit (
101
+ fmt .Sprintf ("failed to add URL entry (%s): %s" , url , err ),
102
+ int (exitCodeErrUrlEntry ),
103
+ )
104
+ }
105
+ return nil
75
106
},
76
107
},
77
108
{
@@ -89,13 +120,26 @@ func initApp() *cli.App {
89
120
},
90
121
Action : func (ctx * cli.Context ) error {
91
122
if ctx .NArg () != 0 {
92
- return fmt .Errorf ("extra arguments (%s)" , ctx .Args ().Slice ())
123
+ return cli .Exit (
124
+ fmt .Sprintf ("extra arguments (%s)" , ctx .Args ().Slice ()),
125
+ int (exitCodeErrArgs ),
126
+ )
93
127
}
94
128
editor := strings .TrimSpace (ctx .String ("editor" ))
95
129
if editor == "" {
96
- return errors .New ("requires editor name" )
130
+ return cli .Exit (
131
+ "requires editor command name" ,
132
+ int (exitCodeErrArgs ),
133
+ )
134
+ }
135
+ err := execEditor (editor , urlFile )
136
+ if err != nil {
137
+ return cli .Exit (
138
+ fmt .Sprintf ("failed to launch editor: %s" , err ),
139
+ int (exitCodeErrEditor ),
140
+ )
97
141
}
98
- return execEditor ( editor , urlFile )
142
+ return nil
99
143
},
100
144
},
101
145
{
@@ -125,17 +169,31 @@ func initApp() *cli.App {
125
169
idx , err := ui .FindItem (items )
126
170
if err != nil {
127
171
if errors .Is (fuzzyfinder .ErrAbort , err ) {
128
- return errors .New ("quit" )
172
+ return cli .Exit (
173
+ "quit" ,
174
+ int (exitCodeOK ),
175
+ )
176
+ } else {
177
+ return cli .Exit (
178
+ fmt .Sprintf ("an error occured on fuzzyfinder: %s" , err ),
179
+ int (exitCodeErrFuzzyFinder ),
180
+ )
129
181
}
130
182
}
131
183
pager , err := ui .NewPager (items [idx ])
132
184
if err != nil {
133
- return fmt .Errorf ("failed to create pager: %w" , err )
185
+ return cli .Exit (
186
+ fmt .Sprintf ("failed to init pager: %s" , err ),
187
+ int (exitCodeErrPager ),
188
+ )
134
189
}
135
-
136
190
if err := pager .Start (); err != nil {
137
- return fmt .Errorf ("an error occured on pager: %w" , err )
191
+ return cli .Exit (
192
+ fmt .Sprintf ("an error occured on pager: %s" , err ),
193
+ int (exitCodeErrPager ),
194
+ )
138
195
}
196
+ return nil
139
197
}
140
198
},
141
199
},
@@ -152,7 +210,10 @@ func initApp() *cli.App {
152
210
for _ , v := range urls {
153
211
f , err := fetchFeed (v )
154
212
if err != nil {
155
- return err
213
+ return cli .Exit (
214
+ fmt .Sprintf ("failed to fetch feeds: %s" , err ),
215
+ int (exitCodeErrFetchFeeds ),
216
+ )
156
217
}
157
218
feeds = append (feeds , * f )
158
219
}
@@ -164,11 +225,17 @@ func initApp() *cli.App {
164
225
165
226
choises , err := ui .FindItemMulti (items )
166
227
if err != nil {
167
- return err
228
+ return cli .Exit (
229
+ fmt .Sprintf ("an error occured on fuzzyfinder: %s" , err ),
230
+ int (exitCodeErrFuzzyFinder ),
231
+ )
168
232
}
169
233
for _ , idx := range choises {
170
234
if err := openURL (items [idx ].Link ); err != nil {
171
- return err
235
+ return cli .Exit (
236
+ fmt .Sprintf ("failed to open URL in browser: %s" , err ),
237
+ int (exitCodeErrBrowser ),
238
+ )
172
239
}
173
240
}
174
241
return nil
0 commit comments