Rewrote flag parsing to use flag-like interface

This commit is contained in:
Philip K 2019-06-21 21:56:03 +02:00 committed by Drew DeVault
parent 9a1cd340f3
commit 143fbe7bbc
3 changed files with 42 additions and 45 deletions

2
go.mod
View file

@ -3,7 +3,7 @@ module git.sr.ht/~sircmpwn/openring
go 1.12
require (
git.sr.ht/~sircmpwn/getopt v0.0.0-20190214165041-9a4f886f9fc7
git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0
github.com/SlyMarbo/rss v1.0.1
github.com/mattn/go-runewidth v0.0.4
github.com/microcosm-cc/bluemonday v1.0.2

2
go.sum
View file

@ -1,5 +1,7 @@
git.sr.ht/~sircmpwn/getopt v0.0.0-20190214165041-9a4f886f9fc7 h1:xTFH5S/3ltiRvAtETLLDFWm5nVIouT5GeCPHm8UaVEU=
git.sr.ht/~sircmpwn/getopt v0.0.0-20190214165041-9a4f886f9fc7/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0 h1:gUeOEsT0mhoCKxKYJk8HeYtUZME686xs70eG2l80W5U=
git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
github.com/SlyMarbo/rss v1.0.1 h1:fiaIU5UhcXauVOniHOIocWG7uj8Ej6pHNarMGPJilzA=
github.com/SlyMarbo/rss v1.0.1/go.mod h1:JNF+T33oj4m5WLCQXpBTCgO+SxRbYVgdiiimHNgzcbA=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=

View file

@ -8,15 +8,35 @@ import (
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
"git.sr.ht/~sircmpwn/getopt"
"github.com/SlyMarbo/rss"
"github.com/mattn/go-runewidth"
"github.com/microcosm-cc/bluemonday"
"git.sr.ht/~sircmpwn/getopt"
)
type urlSlice []*url.URL
func (us *urlSlice) String() string {
var str []string
for _, u := range *us {
str = append(str, u.String())
}
return strings.Join(str, ", ")
}
func (us *urlSlice) Set(val string) error {
u, err := url.Parse(val)
if err != nil {
return err
}
*us = append(*us, u)
return nil
}
type Article struct {
Date time.Time
Link string
@ -28,48 +48,23 @@ type Article struct {
func main() {
var (
narticles int = 3
perSource int = 1
summaryLen int = 256
narticles = getopt.Int("n", 3, "article count")
perSource = getopt.Int("p", 1, "articles to take from each source")
summaryLen = getopt.Int("l", 256, "length of summaries")
sources []*url.URL
)
getopt.Var((*urlSlice)(&sources), "s", "list of sources")
opts, optind, err := getopt.Getopts(os.Args[1:], "l:n:p:s:")
if err != nil {
panic(err)
}
for _, opt := range opts {
switch opt.Option {
case 'l':
summaryLen, err = strconv.Atoi(opt.Value)
if err != nil {
panic(err)
}
case 'n':
narticles, err = strconv.Atoi(opt.Value)
if err != nil {
panic(err)
}
case 'p':
perSource, err = strconv.Atoi(opt.Value)
if err != nil {
panic(err)
}
case 's':
u, err := url.Parse(opt.Value)
if err != nil {
panic(err)
}
sources = append(sources, u)
}
}
if len(os.Args[optind+1:]) != 0 {
log.Fatalf(
"Usage: %s [-s https://source.rss...] < in.html > out.html",
getopt.Usage = func() {
log.Fatalf("Usage: %s [-s https://source.rss...] < in.html > out.html",
os.Args[0])
}
err := getopt.Parse()
if err != nil {
panic(err)
}
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
@ -111,8 +106,8 @@ func main() {
continue
}
items := feed.Items
if len(items) > perSource {
items = items[:perSource]
if len(items) > *perSource {
items = items[:*perSource]
}
for _, item := range items {
raw_summary := item.Summary
@ -120,7 +115,7 @@ func main() {
raw_summary = html.UnescapeString(item.Content)
}
summary := runewidth.Truncate(
policy.Sanitize(raw_summary), summaryLen, "…")
policy.Sanitize(raw_summary), *summaryLen, "…")
articles = append(articles, &Article{
Date: item.Date,
SourceLink: feed.Link,
@ -134,11 +129,11 @@ func main() {
sort.Slice(articles, func(i, j int) bool {
return articles[i].Date.After(articles[j].Date)
})
if len(articles) < narticles {
narticles = len(articles)
if len(articles) < *narticles {
*narticles = len(articles)
}
articles = articles[:narticles]
err = tmpl.Execute(os.Stdout, struct{
articles = articles[:*narticles]
err = tmpl.Execute(os.Stdout, struct {
Articles []*Article
}{
Articles: articles,