refine.go 1.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
package main

import (
	"context"
	"flag"
	"log"
	"os"
	"path/filepath"

	"github.com/google/subcommands"

	"git.autistici.org/ale/liber/util"
)

type refineCommand struct{}

func (c *refineCommand) Name() string             { return "update" }
func (c *refineCommand) Synopsis() string         { return "Add books to the local db" }
func (c *refineCommand) SetFlags(f *flag.FlagSet) {}
func (c *refineCommand) Usage() string {
	return `update [<OPTIONS>]
  Add books to the local database.

`
}

func (c *refineCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
	if f.NArg() > 0 {
		log.Printf("Too many arguments")
		return subcommands.ExitUsageError
	}

	db := openDB()
	defer db.Close()

	// Redirect logging to dbdir/refine.log.
	logf, err := os.OpenFile(filepath.Join(*databaseDir, "refine.log"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
	if err == nil {
		defer logf.Close()
		log.SetOutput(logf)
		log.SetFlags(log.Ldate | log.Ltime)
	}

	db.Refine(util.ExpandTilde(*bookDir))

	return subcommands.ExitSuccess
}