Skip to content

Commit

Permalink
feat : add log system and command interval
Browse files Browse the repository at this point in the history
  • Loading branch information
LordPax committed Apr 28, 2024
1 parent ea9640f commit 429e695
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Added

* Add log system
* Add command interval

## [1.1.0] - 2024-04-21

### Added
Expand Down
24 changes: 6 additions & 18 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"os"
"scan2epub/service"
"scan2epub/utils"

Expand All @@ -14,31 +13,20 @@ var ConvertCommand = cli.Command{
Usage: "Convert scan to epub",
ArgsUsage: "<chap> [chap...]",
Aliases: []string{"c"},
Flags: convertFlags,
Action: convertAction,
}

var convertFlags = []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "output directory",
Action: func(c *cli.Context, value string) error {
if value != "" {
os.Unsetenv("EPUB_DIR")
os.Setenv("EPUB_DIR", value)
}
return nil
},
},
}

func convertAction(c *cli.Context) error {
log, err := utils.GetLog()
if err != nil {
return err
}

if c.NArg() == 0 {
return fmt.Errorf("no chapter specified")
}

fmt.Printf("Converting %d chapters\n", c.NArg())
log.Printf("Converting %d chapters\n", c.NArg())
chaps := c.Args().Slice()
if err := service.Scan2Epub(chaps); err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions commands/interval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package commands

import (
"fmt"
"scan2epub/utils"

cli "github.com/urfave/cli/v2"
)

var IntervalCommand = cli.Command{
Name: "inter",
Usage: "Convert at regular intervals and increment the chapter number",
ArgsUsage: "<interval> <chap>",
Aliases: []string{"i"},
Action: intervalAction,
}

func intervalAction(c *cli.Context) error {
log, err := utils.GetLog()
if err != nil {
return err
}

if c.NArg() < 2 {
return fmt.Errorf("no chapter specified")
}

log.Printf("Converting %d chapters\n", c.NArg())

if err := utils.RmTmpDir(); err != nil {
return err
}

return nil
}
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
USAGE = "CLI tool to convert scan to epub"
CONFIG_DIR = path.Join(home, ".config", "scan2epub")
CONFIG_FILE = path.Join(CONFIG_DIR, "config")
LOG_FILE = path.Join(CONFIG_DIR, "log")
AVAILABLE_EXT = []string{"jpg", "jpeg", "png", "webp"}
CONFIG_EXEMPLE = `URL=https://lelscans.net/mangas/one-piece
EPUB_DIR=` + path.Join(home, "scan2epub") + `
Expand Down Expand Up @@ -44,6 +45,15 @@ func InitConfig() error {
fmt.Printf("Config file created at %s\n", CONFIG_FILE)
}

if !utils.FileExist(LOG_FILE) {
if _, err := os.Create(LOG_FILE); err != nil {
return err
}
fmt.Printf("Log file created at %s\n", LOG_FILE)
}

os.Setenv("LOG_FILE", LOG_FILE)

return nil
}

Expand Down
54 changes: 49 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,61 @@ func mainAction(c *cli.Context) error {
return fmt.Errorf("no command specified")
}

var mainFlags = []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "output directory",
Action: func(c *cli.Context, value string) error {
if value == "" {
return fmt.Errorf("output directory is empty")
}

os.Unsetenv("EPUB_DIR")
os.Setenv("EPUB_DIR", value)

return nil
},
},
&cli.BoolFlag{
Name: "silent",
Aliases: []string{"s"},
Usage: "disable printing log to stdout",
Action: func(c *cli.Context, value bool) error {
log, err := utils.GetLog()
if err != nil {
return err
}

log.SetSilent(value)

return nil
},
},
}

func main() {
os.Setenv("SILENT", "false")

if err := config.InitConfig(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

if err := godotenv.Load(config.CONFIG_FILE); err != nil {
log, err := utils.GetLog()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer log.Close()

if err := godotenv.Load(config.CONFIG_FILE); err != nil {
log.PrintfErr("%v\n", err)
os.Exit(1)
}

if err := config.InitEpubDir(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
log.PrintfErr("%v\n", err)
os.Exit(1)
}

Expand All @@ -36,14 +78,16 @@ func main() {
app.Usage = config.USAGE
app.Version = config.VERSION
app.Action = mainAction
app.Flags = mainFlags
app.Commands = []*cli.Command{
&commands.ConvertCommand,
&commands.ExistsCommand,
&commands.IntervalCommand,
}

if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
_ = utils.RmTmpDir()
os.Exit(1)
log.PrintfErr("%v\n", err)
}

_ = utils.RmTmpDir()
}
14 changes: 12 additions & 2 deletions service/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
)

func convertChap(chap string) error {
fmt.Printf("Converting chapter %s to epub\n", chap)
log, err := utils.GetLog()
if err != nil {
return err
}

log.Printf("Converting chapter %s to epub\n", chap)

tempDir := os.Getenv("TMP_DIR")
epubDir := os.Getenv("EPUB_DIR")
Expand Down Expand Up @@ -40,6 +45,11 @@ func convertChap(chap string) error {
}

func createEpub(pages []string, epubDir string, chap string) error {
log, err := utils.GetLog()
if err != nil {
return err
}

author := os.Getenv("AUTHOR")
description := os.Getenv("DESCRIPTION")

Expand Down Expand Up @@ -67,7 +77,7 @@ func createEpub(pages []string, epubDir string, chap string) error {
return err
}

fmt.Printf("Epub created at %s\n", epubPath)
log.Printf("Epub created at %s\n", epubPath)
return nil
}

Expand Down
17 changes: 14 additions & 3 deletions service/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import (
)

func downloadChap(chap string) error {
fmt.Printf("Downloading chapter %s\n", chap)
log, err := utils.GetLog()
if err != nil {
return err
}

log.Printf("Downloading chapter %s\n", chap)

channel := make(chan error)
url := os.Getenv("URL")
Expand All @@ -25,7 +30,7 @@ func downloadChap(chap string) error {
}

pages := getListOfPages(urlChap, pathChap)
fmt.Printf("%d pages found\n", len(pages))
log.Printf("%d pages found\n", len(pages))

if len(pages) == 0 {
return fmt.Errorf("No pages found for chapter %s", chap)
Expand All @@ -45,6 +50,12 @@ func downloadChap(chap string) error {
}

func downloadPage(url, path string, ch chan<- error) {
log, err := utils.GetLog()
if err != nil {
ch <- err
return
}

out, err := os.Create(path)
if err != nil {
ch <- err
Expand All @@ -65,6 +76,6 @@ func downloadPage(url, path string, ch chan<- error) {
return
}

fmt.Printf("Downloaded page from %s\n", url)
log.Printf("Downloaded page from %s\n", url)
ch <- nil
}
9 changes: 7 additions & 2 deletions service/scanToEpub.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package service

import (
"fmt"
"os"
"scan2epub/utils"
)

func Scan2Epub(chaps []string) error {
log, err := utils.GetLog()
if err != nil {
return err
}

for _, chap := range chaps {
if err := downloadChap(chap); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
log.PrintfErr("%v\n", err)
continue
}
if err := convertChap(chap); err != nil {
Expand Down
88 changes: 88 additions & 0 deletions utils/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package utils

import (
"fmt"
"os"
"time"
)

type Log struct {
file os.File
silent bool
}

var instance *Log

func NewLog(fileName string) (*Log, error) {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}

return &Log{file: *file, silent: false}, nil
}

func GetLog() (*Log, error) {
var err error
logFile := os.Getenv("LOG_FILE")

if instance == nil {
instance, err = NewLog(logFile)
if err != nil {
return nil, err
}
}

return instance, nil
}

func (l *Log) SetSilent(silent bool) {
l.silent = silent
}

func (l *Log) IsSilent() bool {
return l.silent
}

func (l *Log) WriteLog(msg string) error {
date := time.Now().Format("2006-01-02 15:04:05")
text := fmt.Sprintf("[%s] %s", date, msg)

_, err := l.file.WriteString(text)
if err != nil {
return err
}

return nil
}

func (l *Log) Printf(format string, a ...any) {
text := fmt.Sprintf(format, a...)

if !l.silent {
fmt.Print(text)
}

if err := l.WriteLog(text); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

func (l *Log) PrintfErr(format string, a ...any) {
text := fmt.Sprintf(format, a...)
text = fmt.Sprintf("[ERROR] %s", text)

if !l.silent {
fmt.Fprint(os.Stderr, text)
}

if err := l.WriteLog(text); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

func (l *Log) Close() error {
return l.file.Close()
}

0 comments on commit 429e695

Please sign in to comment.