-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : add log system and command interval
- Loading branch information
Showing
9 changed files
with
228 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |