Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Switched to pflag for flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Sep 27, 2018
1 parent cd13d32 commit ad1e625
Show file tree
Hide file tree
Showing 23 changed files with 403 additions and 5,536 deletions.
174 changes: 83 additions & 91 deletions bookbrowser.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"

_ "github.com/geek1011/BookBrowser/formats/epub"
_ "github.com/geek1011/BookBrowser/formats/mobi"
_ "github.com/geek1011/BookBrowser/formats/pdf"
"github.com/geek1011/BookBrowser/server"
"github.com/geek1011/BookBrowser/util"
"github.com/geek1011/BookBrowser/util/sigusr"
"github.com/urfave/cli"
"github.com/spf13/pflag"
)

var curversion = "dev"
Expand All @@ -31,111 +34,100 @@ func main() {
deftempdir = filepath.Join(workdir, "_temp")
}

app := cli.NewApp()
app.Name = "BookBrowser"
app.Usage = "Web-based eBook server supporting ePub and PDF."
app.Version = curversion
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "bookdir, b",
Value: workdir,
Usage: "Load books from `DIR`. The directory must exist.",
},
cli.StringFlag{
Name: "tempdir, t",
Value: deftempdir,
Usage: "Use `DIR` as the location for storing temporary files such as cover thumbnails. The directory is created on start and deleted on exit, unless it already exists.",
},
cli.StringFlag{
Name: "addr, a",
Value: ":8090",
Usage: "`ADDR` is the address to bind the server to. It is in the format IP:PORT. The IP is optional.",
},
cli.BoolFlag{
Name: "nocovers, n",
Usage: "Do not index covers",
},
bookdir := pflag.StringP("bookdir", "b", workdir, "the directory to load books from (must exist)")
tempdir := pflag.StringP("tempdir", "t", deftempdir, "the directory to store temp files such as cover thumbnails (created on start, deleted on exit unless already exists)")
addr := pflag.StringP("addr", "a", ":8090", "the address to bind the server to ([IP]:PORT)")
nocovers := pflag.BoolP("nocovers", "n", false, "do not index covers")
help := pflag.BoolP("help", "h", false, "Show this help text")
sversion := pflag.Bool("version", false, "Show the version")
pflag.Parse()

if *sversion {
fmt.Printf("BookBrowser %s\n", curversion)
os.Exit(0)
}
app.HideHelp = true
app.Action = func(c *cli.Context) {
bookdir := c.String("bookdir")
tempdir := c.String("tempdir")
nocovers := c.Bool("nocovers")
noRemoveTempDir := false

addr := c.String("addr")
if *help || pflag.NArg() != 0 {
fmt.Fprintf(os.Stderr, "Usage: BookBrowser [OPTIONS]\n\nVersion:\n BookBrowser %s\n\nOptions:\n", curversion)
pflag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
if runtime.GOOS == "windows" {
time.Sleep(time.Second * 2)
}
os.Exit(1)
}

log.Printf("BookBrowser %s\n", curversion)
noRemoveTempDir := false

if _, err := os.Stat(bookdir); err != nil {
if os.IsNotExist(err) {
log.Fatalf("Fatal error: book directory %s does not exist\n", bookdir)
}
}
log.Printf("BookBrowser %s\n", curversion)

if fi, err := os.Stat(tempdir); err == nil || (fi != nil && fi.IsDir()) {
noRemoveTempDir = true
if tempdir == deftempdir {
noRemoveTempDir = false
}
if _, err := os.Stat(*bookdir); err != nil {
if os.IsNotExist(err) {
log.Fatalf("Error: book directory %s does not exist\n", *bookdir)
}
}

bookdir, err = filepath.Abs(bookdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve book directory %s: %s\n", bookdir, err)
if fi, err := os.Stat(*tempdir); err == nil || (fi != nil && fi.IsDir()) {
noRemoveTempDir = true
if *tempdir == deftempdir {
noRemoveTempDir = false
}
}

if _, err := os.Stat(tempdir); os.IsNotExist(err) {
os.Mkdir(tempdir, os.ModePerm)
}
*bookdir, err = filepath.Abs(*bookdir)
if err != nil {
log.Fatalf("Error: could not resolve book directory %s: %v\n", *bookdir, err)
}

tempdir, err = filepath.Abs(tempdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve temp directory %s: %s\n", tempdir, err)
}
if _, err := os.Stat(*tempdir); os.IsNotExist(err) {
os.Mkdir(*tempdir, os.ModePerm)
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
if noRemoveTempDir {
log.Println("Not removing temp dir because dir already existed at start")
} else {
log.Println("Cleaning up temp dir")
os.RemoveAll(tempdir)
}
os.Exit(0)
}()

if !strings.Contains(addr, ":") {
log.Fatalln("Invalid listening address")
*tempdir, err = filepath.Abs(*tempdir)
if err != nil {
log.Fatalf("Error: could not resolve temp directory %s: %v\n", *tempdir, err)
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
if noRemoveTempDir {
log.Println("Not removing temp dir because dir already existed at start")
} else {
log.Println("Cleaning up temp dir")
os.RemoveAll(*tempdir)
}
os.Exit(0)
}()

if !strings.Contains(*addr, ":") {
log.Fatalln("Error: invalid listening address")
}

sp := strings.SplitN(addr, ":", 2)
if sp[0] == "" {
ip := util.GetIP()
if ip != nil {
log.Printf("This server can be accessed at http://%s:%s\n", ip.String(), sp[1])
}
sp := strings.SplitN(*addr, ":", 2)
if sp[0] == "" {
ip := util.GetIP()
if ip != nil {
log.Printf("This server can be accessed at http://%s:%s\n", ip.String(), sp[1])
}
}

s := server.NewServer(addr, bookdir, tempdir, curversion, true, nocovers)
go func() {
s.RefreshBookIndex()
if len(s.Indexer.BookList()) == 0 {
log.Fatalln("Fatal error: no books found")
}
}()

sigusr.Handle(func() {
log.Println("Booklist refresh triggered by SIGUSR1")
s.RefreshBookIndex()
})

err = s.Serve()
if err != nil {
log.Fatalf("Error starting server: %s\n", err)
s := server.NewServer(*addr, *bookdir, *tempdir, curversion, true, *nocovers)
go func() {
s.RefreshBookIndex()
if len(s.Indexer.BookList()) == 0 {
log.Fatalln("Fatal error: no books found")
}
}()

sigusr.Handle(func() {
log.Println("Booklist refresh triggered by SIGUSR1")
s.RefreshBookIndex()
})

err = s.Serve()
if err != nil {
log.Fatalf("Error starting server: %s\n", err)
}
app.Run(os.Args)
}
8 changes: 3 additions & 5 deletions public/public-packr.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Code generated by github.com/gobuffalo/packr. DO NOT EDIT

package public

import "github.com/gobuffalo/packr"

// !!! GENERATED FILE !!!
// Do NOT hand edit this file!!
// It is recommended that you DO NOT check into this file into SCM.
// We STRONGLY recommend you delete this file after you have built your
// Go binary. You can use the "packr clean" command to clean up this,
// You can use the "packr clean" command to clean up this,
// and any other packr generated files.
func init() {
packr.PackJSONBytes(".", "public.go", "\"H4sIAAAAAAAA/yTKMQpCMQwA0D2nCJl0sNkFl38A75CW/Fj835TQavH0oq6P16TcxRTbyFstAHVvHh3Jar+NnIrvbJ7Husrm/M1BAMzmZ9OHhnTFn+LpDfCUwMUnXv6WrvpafB4o0RE+AQAA////M9mkaQAAAA==\"")
Expand Down
7 changes: 5 additions & 2 deletions vendor/github.com/spf13/pflag/flag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ad1e625

Please sign in to comment.