From 8558f06440574ed0e10b29fc21247217feb1a3f8 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 31 Jul 2024 17:49:01 +0200 Subject: [PATCH] Adds a proper CLI and uses args for customizations instead of config --- cmd/bookworm/bookworm.go | 70 +++++++++++++++++++++++++++------------- cmd/bookworm/cmd.go | 20 ++++++++++++ go.mod | 5 ++- go.sum | 8 +++++ 4 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 cmd/bookworm/cmd.go diff --git a/cmd/bookworm/bookworm.go b/cmd/bookworm/bookworm.go index cfb9172..1236735 100644 --- a/cmd/bookworm/bookworm.go +++ b/cmd/bookworm/bookworm.go @@ -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) + } } diff --git a/cmd/bookworm/cmd.go b/cmd/bookworm/cmd.go new file mode 100644 index 0000000..5c2b265 --- /dev/null +++ b/cmd/bookworm/cmd.go @@ -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."` +} diff --git a/go.mod b/go.mod index cfaa3db..28dca28 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.ctrlz.es/mgdelacroix/bookworm go 1.21.9 -require miniflux.app v1.0.46 +require ( + github.com/alecthomas/kong v0.9.0 + miniflux.app v1.0.46 +) diff --git a/go.sum b/go.sum index 477e448..e67cced 100644 --- a/go.sum +++ b/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/go.mod h1:YtEJIO1vMCvZgyzDbds7II0W/H7sGpo3auFCQscuMrE=