This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
190 lines (183 loc) · 4.85 KB
/
main.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
package main
import (
"errors"
"os"
"os/exec"
"strings"
"syscall"
"github.com/gilliek/go-opml/opml"
"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const Version = "0.3.0"
func addFeed(name string, url string) error {
homePath, err := os.UserHomeDir()
if err != nil {
log.Error("Failed to get home path")
os.Exit(1)
}
cfg, err := ini.Load(homePath + "/.config/rssnix/config.ini")
for _, key := range cfg.Section("feeds").Keys() {
if key.Name() == name {
return errors.New("Feed named '" + name + "' already exists")
}
}
file, err := os.OpenFile(homePath+"/.config/rssnix/config.ini", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString("\n" + name + " = " + url)
return err
}
func main() {
syscall.Umask(0)
LoadConfig()
app := &cli.App{
Commands: []*cli.Command{
{
Name: "config",
Aliases: []string{"c"},
Usage: "opens the config file with $EDITOR",
Action: func(cCtx *cli.Context) error {
editor, ok := os.LookupEnv("EDITOR")
if len(editor) == 0 || !ok {
return errors.New("$EDITOR environment variable is not set")
}
homePath, err := os.UserHomeDir()
if err != nil {
log.Error("Failed to get home path")
os.Exit(1)
}
cmd := exec.Command(editor, homePath+"/.config/rssnix/config.ini")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd.Run()
},
},
{
Name: "refetch",
Aliases: []string{"r"},
Usage: "delete and refetch given feed(s) or all feeds if no argument is given",
Action: func(cCtx *cli.Context) error {
InitialiseNewArticleDirectory()
if cCtx.Args().Len() == 0 {
UpdateAllFeeds(true)
}
for i := 0; i < cCtx.Args().Len(); i++ {
UpdateFeed(cCtx.Args().Get(i), true)
}
return nil
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "update given feed(s) or all feeds if no argument is given",
Action: func(cCtx *cli.Context) error {
InitialiseNewArticleDirectory()
if cCtx.Args().Len() == 0 {
UpdateAllFeeds(false)
}
for i := 0; i < cCtx.Args().Len(); i++ {
UpdateFeed(cCtx.Args().Get(i), false)
}
return nil
},
},
{
Name: "open",
Aliases: []string{"o"},
Usage: "open given feed's directory or root feeds directory if no argument is given",
Action: func(cCtx *cli.Context) error {
var path string
if cCtx.Args().Len() == 0 {
path = Config.FeedDirectory
} else {
path = Config.FeedDirectory + "/" + cCtx.Args().Get(0)
}
cmd := exec.Command(Config.Viewer, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd.Run()
},
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a given feed to config",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() != 2 {
return errors.New("exactly two arguments are required, first being feed name, second being URL")
}
return addFeed(cCtx.Args().Get(0), cCtx.Args().Get(1))
},
},
{
Name: "import",
Aliases: []string{"i"},
Usage: "import an OPML file",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() != 1 {
return errors.New("argument specifying OPML file path or URL is required")
}
doc, err := opml.NewOPMLFromFile(cCtx.Args().Get(0))
if err != nil {
doc, err = opml.NewOPMLFromURL(cCtx.Args().Get(0))
if err != nil {
return err
}
}
for _, outline := range doc.Body.Outlines {
if len(outline.XMLURL) > 0 {
var title string
if len(outline.Title) > 0 {
title = outline.Title
} else if len(outline.Text) > 0 {
title = outline.Text
} else {
continue
}
err = addFeed(strings.ReplaceAll(title, " ", "-"), outline.XMLURL)
if err != nil {
log.Error("Failed to add feed titled '" + title + "', error: " + err.Error())
continue
}
}
for _, innerOutline := range outline.Outlines {
if len(innerOutline.XMLURL) > 0 {
var title string
if len(innerOutline.Title) > 0 {
title = innerOutline.Title
} else if len(innerOutline.Text) > 0 {
title = innerOutline.Text
} else {
continue
}
err = addFeed(strings.ReplaceAll(title, " ", "-"), innerOutline.XMLURL)
if err != nil {
log.Error("Failed to add feed titled '" + title + "', error: " + err.Error())
continue
}
}
}
}
return nil
},
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "display the version",
Action: func(cCtx *cli.Context) error {
log.Info(Version)
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Error(err)
}
}