Adds a proper CLI and uses args for customizations instead of config
This commit is contained in:
parent
c898be82de
commit
8558f06440
4 changed files with 80 additions and 23 deletions
|
@ -2,29 +2,24 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.ctrlz.es/mgdelacroix/bookworm/templates"
|
"git.ctrlz.es/mgdelacroix/bookworm/templates"
|
||||||
|
|
||||||
|
"github.com/alecthomas/kong"
|
||||||
miniflux "miniflux.app/client"
|
miniflux "miniflux.app/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Title string `json:"title"`
|
|
||||||
MinifluxURL string `json:"miniflux_url"`
|
MinifluxURL string `json:"miniflux_url"`
|
||||||
MinifluxUsername string `json:"miniflux_username"`
|
MinifluxUsername string `json:"miniflux_username"`
|
||||||
MinifluxPassword string `json:"miniflux_password"`
|
MinifluxPassword string `json:"miniflux_password"`
|
||||||
MinifluxAPI string `json:"miniflux_api"`
|
MinifluxAPI string `json:"miniflux_api"`
|
||||||
MinifluxCategory string `json:"miniflux_category"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) IsValid() error {
|
func (c *Config) IsValid() error {
|
||||||
if c.Title == "" {
|
|
||||||
return fmt.Errorf("title is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.MinifluxURL == "" {
|
if c.MinifluxURL == "" {
|
||||||
return fmt.Errorf("miniflux_url is required")
|
return fmt.Errorf("miniflux_url is required")
|
||||||
}
|
}
|
||||||
|
@ -72,7 +67,7 @@ func checkErr(err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listCategories(cfg *Config, client *miniflux.Client) error {
|
func listCategories(client *miniflux.Client) error {
|
||||||
categories, err := client.Categories()
|
categories, err := client.Categories()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot fetch categories: %w", err)
|
return fmt.Errorf("cannot fetch categories: %w", err)
|
||||||
|
@ -88,12 +83,8 @@ func listCategories(cfg *Config, client *miniflux.Client) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func (l *listCmd) Run(ctx *Context) error {
|
||||||
cfgFlag := flag.String("config", "bookworm.json", "path to the configuration file")
|
cfg, err := ReadConfig(ctx.Config)
|
||||||
listCategoriesFlag := flag.Bool("list-categories", false, "lists the categories of the remote Miniflux instance")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
cfg, err := ReadConfig(*cfgFlag)
|
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
cErr := cfg.IsValid()
|
cErr := cfg.IsValid()
|
||||||
|
@ -102,14 +93,22 @@ func main() {
|
||||||
client, cliErr := createClient(cfg)
|
client, cliErr := createClient(cfg)
|
||||||
checkErr(cliErr)
|
checkErr(cliErr)
|
||||||
|
|
||||||
if *listCategoriesFlag {
|
return listCategories(client)
|
||||||
listCategories(cfg, client)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *generateCmd) Run(ctx *Context) error {
|
||||||
|
cfg, err := ReadConfig(ctx.Config)
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
cErr := cfg.IsValid()
|
||||||
|
checkErr(cErr)
|
||||||
|
|
||||||
|
client, cliErr := createClient(cfg)
|
||||||
|
checkErr(cliErr)
|
||||||
|
|
||||||
var feeds miniflux.Feeds
|
var feeds miniflux.Feeds
|
||||||
var fErr error
|
var fErr error
|
||||||
if cfg.MinifluxCategory != "" {
|
if g.Category != "" {
|
||||||
categories, catErr := client.Categories()
|
categories, catErr := client.Categories()
|
||||||
if catErr != nil {
|
if catErr != nil {
|
||||||
checkErr(fmt.Errorf("can't get categories: %w", catErr))
|
checkErr(fmt.Errorf("can't get categories: %w", catErr))
|
||||||
|
@ -117,14 +116,14 @@ func main() {
|
||||||
|
|
||||||
var category *miniflux.Category
|
var category *miniflux.Category
|
||||||
for _, c := range categories {
|
for _, c := range categories {
|
||||||
if c.Title == cfg.MinifluxCategory {
|
if c.Title == g.Category {
|
||||||
category = c
|
category = c
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if category == nil {
|
if category == nil {
|
||||||
checkErr(fmt.Errorf("can't find category %q", cfg.MinifluxCategory))
|
checkErr(fmt.Errorf("can't find category %q", g.Category))
|
||||||
}
|
}
|
||||||
|
|
||||||
feeds, fErr = client.CategoryFeeds(category.ID)
|
feeds, fErr = client.CategoryFeeds(category.ID)
|
||||||
|
@ -133,13 +132,27 @@ func main() {
|
||||||
}
|
}
|
||||||
checkErr(fErr)
|
checkErr(fErr)
|
||||||
|
|
||||||
|
baseTitle := "All"
|
||||||
|
if g.Title != "" {
|
||||||
|
baseTitle = g.Title
|
||||||
|
} else if g.Category != "" {
|
||||||
|
baseTitle = g.Category
|
||||||
|
}
|
||||||
|
|
||||||
|
title := fmt.Sprintf(
|
||||||
|
"%s %s %s",
|
||||||
|
baseTitle,
|
||||||
|
time.Now().Add(time.Hour*24*-7).Format("01-02"),
|
||||||
|
time.Now().Format("01-02"),
|
||||||
|
)
|
||||||
|
|
||||||
data := struct {
|
data := struct {
|
||||||
Title string
|
Title string
|
||||||
OldestArticle int
|
OldestArticle int
|
||||||
MaxArticlesPerFeed int
|
MaxArticlesPerFeed int
|
||||||
Feeds miniflux.Feeds
|
Feeds miniflux.Feeds
|
||||||
}{
|
}{
|
||||||
Title: cfg.Title,
|
Title: title,
|
||||||
OldestArticle: 7,
|
OldestArticle: 7,
|
||||||
MaxArticlesPerFeed: 100,
|
MaxArticlesPerFeed: 100,
|
||||||
Feeds: feeds,
|
Feeds: feeds,
|
||||||
|
@ -149,4 +162,17 @@ func main() {
|
||||||
checkErr(tErr)
|
checkErr(tErr)
|
||||||
|
|
||||||
checkErr(os.WriteFile("out.recipe", s, 0755))
|
checkErr(os.WriteFile("out.recipe", s, 0755))
|
||||||
|
|
||||||
|
// ToDo: call ebook-convert with the right filename and the extension from the format
|
||||||
|
// ToDo: make recipe a temp file that is removed after the ebook is generated
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx := kong.Parse(&cli)
|
||||||
|
if err := ctx.Run(&Context{Config: cli.Config}); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
20
cmd/bookworm/cmd.go
Normal file
20
cmd/bookworm/cmd.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Context struct {
|
||||||
|
Config string
|
||||||
|
}
|
||||||
|
|
||||||
|
type listCmd struct{}
|
||||||
|
|
||||||
|
type generateCmd struct {
|
||||||
|
Title string `short:"t" optional:"" help:"The title of the ebook."`
|
||||||
|
Category string `short:"C" help:"Use only one category of feeds."`
|
||||||
|
Format string `short:"f" enum:"mobi,epub" default:"mobi" help:"Format of the generated ebook."`
|
||||||
|
}
|
||||||
|
|
||||||
|
var cli struct {
|
||||||
|
Config string `short:"c" default:"bookworm.json" help:"The path to the configuration file."`
|
||||||
|
|
||||||
|
List listCmd `cmd:"" help:"Get the list of categories on the remote RSS reader."`
|
||||||
|
Generate generateCmd `cmd:"" help:"Generate an ebook."`
|
||||||
|
}
|
5
go.mod
5
go.mod
|
@ -2,4 +2,7 @@ module git.ctrlz.es/mgdelacroix/bookworm
|
||||||
|
|
||||||
go 1.21.9
|
go 1.21.9
|
||||||
|
|
||||||
require miniflux.app v1.0.46
|
require (
|
||||||
|
github.com/alecthomas/kong v0.9.0
|
||||||
|
miniflux.app v1.0.46
|
||||||
|
)
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -1,2 +1,10 @@
|
||||||
|
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
|
||||||
|
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||||
|
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
|
||||||
|
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
|
||||||
|
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
|
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||||
|
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||||
|
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||||
miniflux.app v1.0.46 h1:2/xrbXiEoQlj/bAZ8MT+fPN1+gmYDYuXVCOhvSskns4=
|
miniflux.app v1.0.46 h1:2/xrbXiEoQlj/bAZ8MT+fPN1+gmYDYuXVCOhvSskns4=
|
||||||
miniflux.app v1.0.46/go.mod h1:YtEJIO1vMCvZgyzDbds7II0W/H7sGpo3auFCQscuMrE=
|
miniflux.app v1.0.46/go.mod h1:YtEJIO1vMCvZgyzDbds7II0W/H7sGpo3auFCQscuMrE=
|
||||||
|
|
Loading…
Reference in a new issue