refine.go 1.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
package main

import (
	"context"
	"flag"
	"log"
	"os"

	"github.com/google/subcommands"

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

ale's avatar
ale committed
14 15 16 17 18 19 20
type refineCommand struct {
	noninteractive bool
}

func (c *refineCommand) SetFlags(f *flag.FlagSet) {
	f.BoolVar(&c.noninteractive, "noninteractive", false, "disable user prompts")
}
21

ale's avatar
ale committed
22 23
func (c *refineCommand) Name() string     { return "refine" }
func (c *refineCommand) Synopsis() string { return "Improve metadata" }
24
func (c *refineCommand) Usage() string {
ale's avatar
ale committed
25 26 27 28
	return `refine [<OPTIONS>]
  Improve metadata of books already in the database.

  Expects a list of book IDs on standard input.
29 30 31 32 33 34 35 36 37 38 39 40 41

`
}

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()

ale's avatar
ale committed
42 43 44
	if err := db.WithBookIDs(os.Stdin, db.RefineFunc(util.ExpandTilde(*bookDir), promptUser(c.noninteractive))); err != nil {
		log.Printf("error: %v", err)
		return subcommands.ExitFailure
45 46 47 48
	}

	return subcommands.ExitSuccess
}