Skip to content

Commit

Permalink
Merge pull request #104 from metafates/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
metafates authored Oct 23, 2022
2 parents 1fc3e27 + e305dab commit 5a46299
Show file tree
Hide file tree
Showing 24 changed files with 318 additions and 188 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to
[Semantic Versioning](https://semver.org).

## 3.13.0

- Support environment variables for `downloader.path` config field #103
- Replace Mangakakalot with Manganato #102
- Move `install` & `gen` commands to `sources` subcommands. E.g. if you used `mangal install` before use `mangal sources install`. Old commands are still present, but marked as deprecated.
- New flags `--builtin` & `--custom` for `sources` command to filter sources by type.
- New flag `--json` added for `config info` command to show fields in json format.
- New command `mangal sources remove <name>` to remove custom source.
- Minor performance improvements.

## 3.12.0

- Faster and more optimized page downloader
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Use env variable `MANGAL_CONFIG_PATH` to set custom config path.
TLDR; To browse and install a custom scraper
from [mangal-scrapers repository](https://github.com/metafates/mangal-scrapers) run

mangal install
mangal sources install

Mangal has a Lua5.1 VM built-in + some useful libraries, such as headless chrome, http client, html parser and so on...

Expand Down
16 changes: 12 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var configCmd = &cobra.Command{
func init() {
configCmd.AddCommand(configInfoCmd)
configInfoCmd.Flags().StringP("key", "k", "", "The key to get the value for")
configInfoCmd.Flags().BoolP("json", "j", false, "Output as JSON")
_ = configInfoCmd.RegisterFlagCompletionFunc("key", completionConfigKeys)
}

Expand All @@ -57,6 +58,7 @@ var configInfoCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
var (
key = lo.Must(cmd.Flags().GetString("key"))
asJson = lo.Must(cmd.Flags().GetBool("json"))
fields = lo.Values(config.Default)
)

Expand All @@ -69,14 +71,20 @@ var configInfoCmd = &cobra.Command{
}

sort.Slice(fields, func(i, j int) bool {
return fields[i].Name < fields[j].Name
return fields[i].Key < fields[j].Key
})

for i, field := range fields {
fmt.Print(field.Pretty())
if asJson {
fmt.Println(field.Json())
} else {
fmt.Print(field.Pretty())
}

if i < len(fields)-1 {
fmt.Println()
if !asJson {
if i < len(fields)-1 {
fmt.Println()
}
}
}
},
Expand Down
67 changes: 5 additions & 62 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
package cmd

import (
"github.com/metafates/mangal/constant"
"github.com/metafates/mangal/filesystem"
"github.com/metafates/mangal/util"
"github.com/metafates/mangal/where"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"os/user"
"path/filepath"
"strings"
"text/template"
)

func init() {
Expand All @@ -26,56 +16,9 @@ func init() {
}

var genCmd = &cobra.Command{
Use: "gen",
Short: "Generate a new lua source",
Long: `Generate a new lua source.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.SetOut(os.Stdout)

author := viper.GetString(constant.GenAuthor)
if author == "" {
usr, err := user.Current()
if err == nil {
author = usr.Username
} else {
author = "Anonymous"
}
}

s := struct {
Name string
URL string
SearchMangaFn string
MangaChaptersFn string
ChapterPagesFn string
Author string
}{
Name: lo.Must(cmd.Flags().GetString("name")),
URL: lo.Must(cmd.Flags().GetString("url")),
SearchMangaFn: constant.SearchMangaFn,
MangaChaptersFn: constant.MangaChaptersFn,
ChapterPagesFn: constant.ChapterPagesFn,
Author: author,
}

funcMap := template.FuncMap{
"repeat": strings.Repeat,
"plus": func(a, b int) int { return a + b },
"max": util.Max[int],
}

tmpl, err := template.New("source").Funcs(funcMap).Parse(constant.SourceTemplate)
handleErr(err)

target := filepath.Join(where.Sources(), util.SanitizeFilename(s.Name)+".lua")
f, err := filesystem.Api().Create(target)
handleErr(err)

defer util.Ignore(f.Close)

err = tmpl.Execute(f, s)
handleErr(err)

cmd.Println(target)
},
Use: "gen",
Short: "Generate a new lua source",
Long: `Generate a new lua source.`,
Deprecated: "use `mangal sources gen` instead.",
Run: sourcesGenCmd.Run,
}
10 changes: 4 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/metafates/mangal/tui"
"github.com/spf13/cobra"
)

Expand All @@ -10,11 +9,10 @@ func init() {
}

var installCmd = &cobra.Command{
Use: "install",
Short: "Browse and install custom scrapers",
Use: "install",
Short: "Browse and install custom scrapers",
Deprecated: "use `mangal sources install` instead.",
Long: `Browse and install custom scrapers from official GitHub repo.
https://github.com/metafates/mangal-scrapers`,
Run: func(cmd *cobra.Command, args []string) {
handleErr(tui.Run(&tui.Options{Install: true}))
},
Run: sourcesInstallCmd.Run,
}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func init() {
lo.Must0(rootCmd.RegisterFlagCompletionFunc("source", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var sources []string

for _, p := range provider.DefaultProviders() {
for _, p := range provider.Builtins() {
sources = append(sources, p.Name)
}

for _, p := range provider.CustomProviders() {
for _, p := range provider.Customs() {
sources = append(sources, p.Name)
}

Expand Down
148 changes: 135 additions & 13 deletions cmd/sources.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package cmd

import (
"fmt"
"github.com/metafates/mangal/constant"
"github.com/metafates/mangal/tui"
"github.com/metafates/mangal/util"
"github.com/spf13/viper"
"os"
"os/user"
"path/filepath"
"strings"
"text/template"

"github.com/metafates/mangal/filesystem"
"github.com/metafates/mangal/icon"
"github.com/metafates/mangal/provider"
"github.com/metafates/mangal/style"
"github.com/metafates/mangal/where"
"github.com/samber/lo"
"github.com/spf13/cobra"
"os"
)

func init() {
rootCmd.AddCommand(sourcesCmd)
sourcesCmd.Flags().BoolP("raw", "r", false, "do not print headers")
sourcesCmd.Flags().BoolP("custom", "c", false, "show only custom sources")
sourcesCmd.Flags().BoolP("builtin", "b", false, "show only builtin sources")

sourcesCmd.MarkFlagsMutuallyExclusive("custom", "builtin")
sourcesCmd.SetOut(os.Stdout)
}

Expand All @@ -19,33 +36,138 @@ var sourcesCmd = &cobra.Command{
Short: "List an available sources",
Example: "mangal sources",
Run: func(cmd *cobra.Command, args []string) {

printHeader := !lo.Must(cmd.Flags().GetBool("raw"))
headerStyle := style.Combined(style.Bold, style.HiBlue)

h := func(s string) {
if printHeader {
cmd.Println(headerStyle(s))
}
}

defaultProviders := provider.DefaultProviders()
customProviders := provider.CustomProviders()
printBuiltin := func() {
h("Builtin:")
for _, p := range provider.Builtins() {
cmd.Println(p.Name)
}
}

printCustom := func() {
h("Custom:")
for _, p := range provider.Customs() {
cmd.Println(p.Name)
}
}

h("Builtin:")
for _, p := range defaultProviders {
cmd.Println(p.Name)
switch {
case lo.Must(cmd.Flags().GetBool("builtin")):
printBuiltin()
case lo.Must(cmd.Flags().GetBool("custom")):
printCustom()
default:
printBuiltin()
cmd.Println()
printCustom()
}
},
}

func init() {
sourcesCmd.AddCommand(sourcesRemoveCmd)
}

if len(customProviders) == 0 {
var sourcesRemoveCmd = &cobra.Command{
Use: "remove",
Short: "Remove a custom source",
Example: "mangal sources remove <name>",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
handleErr(cmd.Help())
return
}

h("")
for _, name := range args {
path := filepath.Join(where.Sources(), name+provider.CustomProviderExtension)
handleErr(filesystem.Api().Remove(path))
fmt.Printf("%s successfully removed %s\n", icon.Get(icon.Success), style.Yellow(name))
}
},
}

func init() {
sourcesCmd.AddCommand(sourcesInstallCmd)
}

var sourcesInstallCmd = &cobra.Command{
Use: "install",
Short: "Browse and install custom scrapers",
Long: `Browse and install custom scrapers from official GitHub repo.
https://github.com/metafates/mangal-scrapers`,
Run: func(cmd *cobra.Command, args []string) {
handleErr(tui.Run(&tui.Options{Install: true}))
},
}

func init() {
sourcesCmd.AddCommand(sourcesGenCmd)

sourcesGenCmd.Flags().StringP("name", "n", "", "name of the source")
sourcesGenCmd.Flags().StringP("url", "u", "", "url of the website")

lo.Must0(sourcesGenCmd.MarkFlagRequired("name"))
lo.Must0(sourcesGenCmd.MarkFlagRequired("url"))
}

var sourcesGenCmd = &cobra.Command{
Use: "gen",
Short: "Generate a new lua source",
Long: `Generate a new lua source.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.SetOut(os.Stdout)

author := viper.GetString(constant.GenAuthor)
if author == "" {
usr, err := user.Current()
if err == nil {
author = usr.Username
} else {
author = "Anonymous"
}
}

s := struct {
Name string
URL string
SearchMangaFn string
MangaChaptersFn string
ChapterPagesFn string
Author string
}{
Name: lo.Must(cmd.Flags().GetString("name")),
URL: lo.Must(cmd.Flags().GetString("url")),
SearchMangaFn: constant.SearchMangaFn,
MangaChaptersFn: constant.MangaChaptersFn,
ChapterPagesFn: constant.ChapterPagesFn,
Author: author,
}

h("Custom:")
for _, p := range provider.CustomProviders() {
cmd.Println(p.Name)
funcMap := template.FuncMap{
"repeat": strings.Repeat,
"plus": func(a, b int) int { return a + b },
"max": util.Max[int],
}

tmpl, err := template.New("source").Funcs(funcMap).Parse(constant.SourceTemplate)
handleErr(err)

target := filepath.Join(where.Sources(), util.SanitizeFilename(s.Name)+".lua")
f, err := filesystem.Api().Create(target)
handleErr(err)

defer util.Ignore(f.Close)

err = tmpl.Execute(f, s)
handleErr(err)

cmd.Println(target)
},
}
Loading

0 comments on commit 5a46299

Please sign in to comment.