Adds a proper CLI and uses args for customizations instead of config

This commit is contained in:
Miguel de la Cruz 2024-07-31 17:49:01 +02:00
parent c898be82de
commit 8558f06440
4 changed files with 80 additions and 23 deletions

View file

@ -2,29 +2,24 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"time"
"git.ctrlz.es/mgdelacroix/bookworm/templates"
"github.com/alecthomas/kong"
miniflux "miniflux.app/client"
)
type Config struct {
Title string `json:"title"`
MinifluxURL string `json:"miniflux_url"`
MinifluxUsername string `json:"miniflux_username"`
MinifluxPassword string `json:"miniflux_password"`
MinifluxAPI string `json:"miniflux_api"`
MinifluxCategory string `json:"miniflux_category"`
}
func (c *Config) IsValid() error {
if c.Title == "" {
return fmt.Errorf("title is required")
}
if c.MinifluxURL == "" {
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()
if err != nil {
return fmt.Errorf("cannot fetch categories: %w", err)
@ -88,12 +83,8 @@ func listCategories(cfg *Config, client *miniflux.Client) error {
return nil
}
func main() {
cfgFlag := flag.String("config", "bookworm.json", "path to the configuration file")
listCategoriesFlag := flag.Bool("list-categories", false, "lists the categories of the remote Miniflux instance")
flag.Parse()
cfg, err := ReadConfig(*cfgFlag)
func (l *listCmd) Run(ctx *Context) error {
cfg, err := ReadConfig(ctx.Config)
checkErr(err)
cErr := cfg.IsValid()
@ -102,14 +93,22 @@ func main() {
client, cliErr := createClient(cfg)
checkErr(cliErr)
if *listCategoriesFlag {
listCategories(cfg, client)
return
}
return listCategories(client)
}
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 fErr error
if cfg.MinifluxCategory != "" {
if g.Category != "" {
categories, catErr := client.Categories()
if catErr != nil {
checkErr(fmt.Errorf("can't get categories: %w", catErr))
@ -117,14 +116,14 @@ func main() {
var category *miniflux.Category
for _, c := range categories {
if c.Title == cfg.MinifluxCategory {
if c.Title == g.Category {
category = c
break
}
}
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)
@ -133,13 +132,27 @@ func main() {
}
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 {
Title string
OldestArticle int
MaxArticlesPerFeed int
Feeds miniflux.Feeds
}{
Title: cfg.Title,
Title: title,
OldestArticle: 7,
MaxArticlesPerFeed: 100,
Feeds: feeds,
@ -149,4 +162,17 @@ func main() {
checkErr(tErr)
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
View 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."`
}