Skip to content

Commit

Permalink
refacto : move main command in commands/base.go
Browse files Browse the repository at this point in the history
  • Loading branch information
LordPax committed Aug 1, 2024
1 parent 92a21ee commit 3047895
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Changed

* Move main command in `commands/base.go`

## [1.2.0] - 2024-05-07

### Added
Expand Down
46 changes: 46 additions & 0 deletions commands/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

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

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

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 MainAction(c *cli.Context) error {
return fmt.Errorf("No command specified")
}
6 changes: 1 addition & 5 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func convertAction(c *cli.Context) error {
}

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

log.Printf("Converting %d chapters\n", c.NArg())
Expand All @@ -32,9 +32,5 @@ func convertAction(c *cli.Context) error {
return err
}

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

return nil
}
2 changes: 1 addition & 1 deletion commands/exist.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var existFlags = []cli.Flag{}

func existAction(c *cli.Context) error {
if c.NArg() == 0 {
return fmt.Errorf("no chapter specified")
return fmt.Errorf("No chapter specified")
}

chaps := c.Args().Slice()[0]
Expand Down
7 changes: 1 addition & 6 deletions commands/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"fmt"
"scan2epub/service"
"scan2epub/utils"

cli "github.com/urfave/cli/v2"
)
Expand All @@ -18,7 +17,7 @@ var IntervalCommand = cli.Command{

func intervalAction(c *cli.Context) error {
if c.NArg() < 2 {
return fmt.Errorf("no chapter or cron specified")
return fmt.Errorf("No chapter or cron specified")
}

cronStr := c.Args().Get(0)
Expand All @@ -28,9 +27,5 @@ func intervalAction(c *cli.Context) error {
return err
}

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

return nil
}
43 changes: 2 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,7 @@ import (
cli "github.com/urfave/cli/v2"
)

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)
Expand All @@ -77,8 +38,8 @@ func main() {
app.Name = config.NAME
app.Usage = config.USAGE
app.Version = config.VERSION
app.Action = mainAction
app.Flags = mainFlags
app.Action = commands.MainAction
app.Flags = commands.MainFlags
app.Commands = []*cli.Command{
&commands.ConvertCommand,
&commands.ExistsCommand,
Expand Down

0 comments on commit 3047895

Please sign in to comment.