Skip to content

Commit 14af4cb

Browse files
authored
Fix exit code #12 (#17)
1 parent 3a5e3d9 commit 14af4cb

File tree

1 file changed

+83
-16
lines changed

1 file changed

+83
-16
lines changed

main.go

+83-16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ var (
2424
appUsage = "A simple command line RSS feed reader"
2525
)
2626

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+
2740
var urlFile = filepath.Join(configdir.LocalConfig(), appName, "urls.txt")
2841

2942
// var cacheDBFile = path.Join(configdir.LocalCache(), appName, "cache.db")
@@ -47,7 +60,7 @@ func initApp() *cli.App {
4760
},
4861
Action: func(ctx *cli.Context) error {
4962
if ctx.NArg() == 0 {
50-
return errors.New("must require arguments")
63+
return cli.Exit("must require arguments", int(exitCodeErrArgs))
5164
}
5265
return nil
5366
},
@@ -58,20 +71,38 @@ func initApp() *cli.App {
5871
Usage: "Add url entry",
5972
Action: func(ctx *cli.Context) error {
6073
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+
)
6278
}
6379
url := ctx.Args().Get(0)
6480
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+
)
6685
}
6786
urls, err := readURLsFromEntry()
6887
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+
)
7092
}
7193
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+
)
7398
}
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
75106
},
76107
},
77108
{
@@ -89,13 +120,26 @@ func initApp() *cli.App {
89120
},
90121
Action: func(ctx *cli.Context) error {
91122
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+
)
93127
}
94128
editor := strings.TrimSpace(ctx.String("editor"))
95129
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+
)
97141
}
98-
return execEditor(editor, urlFile)
142+
return nil
99143
},
100144
},
101145
{
@@ -125,17 +169,31 @@ func initApp() *cli.App {
125169
idx, err := ui.FindItem(items)
126170
if err != nil {
127171
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+
)
129181
}
130182
}
131183
pager, err := ui.NewPager(items[idx])
132184
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+
)
134189
}
135-
136190
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+
)
138195
}
196+
return nil
139197
}
140198
},
141199
},
@@ -152,7 +210,10 @@ func initApp() *cli.App {
152210
for _, v := range urls {
153211
f, err := fetchFeed(v)
154212
if err != nil {
155-
return err
213+
return cli.Exit(
214+
fmt.Sprintf("failed to fetch feeds: %s", err),
215+
int(exitCodeErrFetchFeeds),
216+
)
156217
}
157218
feeds = append(feeds, *f)
158219
}
@@ -164,11 +225,17 @@ func initApp() *cli.App {
164225

165226
choises, err := ui.FindItemMulti(items)
166227
if err != nil {
167-
return err
228+
return cli.Exit(
229+
fmt.Sprintf("an error occured on fuzzyfinder: %s", err),
230+
int(exitCodeErrFuzzyFinder),
231+
)
168232
}
169233
for _, idx := range choises {
170234
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+
)
172239
}
173240
}
174241
return nil

0 commit comments

Comments
 (0)