/
edit.go
171 lines (157 loc) · 4.2 KB
/
edit.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright © 2018 Tyler Spivey <tspivey@pcdesk.net> and Niko Carpenter <nikoacarpenter@gmail.com>
//
// This source code is governed by the MIT license, which can be found in the LICENSE file.
package commands
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"text/template"
"github.com/peterh/liner"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tspivey/books"
"github.com/tspivey/books/cmd/books/edit"
)
// editCmd represents the edit command
var editCmd = &cobra.Command{
Use: "edit",
Short: "Interactively edits a book",
Run: editFunc,
}
func init() {
rootCmd.AddCommand(editCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// editCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// editCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
editCmd.Flags().BoolP("hash", "H", false, "Specify a book by one of its hashes")
}
func editFunc(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "Usage: books edit <book id>\n")
os.Exit(1)
}
useHash, err := cmd.Flags().GetBool("hash")
if err != nil {
log.Fatal(err)
}
library, err := books.OpenLibrary(libraryFile, booksRoot)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening library: %s\n", err)
os.Exit(1)
}
defer library.Close()
var bookToEdit books.Book
if useHash {
hash := args[0]
bks, err := library.GetBooksByHash(hash)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting books by hash: %s\n", err)
os.Exit(1)
}
if len(bks) == 0 {
fmt.Fprintf(os.Stderr, "Hash not found.\n")
os.Exit(1)
} else if len(bks) == 1 {
bookToEdit = bks[0]
} else {
for i, book := range bks {
bookName := books.JoinNaturally("and", book.Authors) + " - " + book.Title
if book.Series != "" {
bookName += " [" + book.Series + "]"
}
bookName += " (" + strconv.FormatInt(book.ID, 10) + ")"
fmt.Printf("%d. %s\n", i+1, bookName)
}
reader := bufio.NewReader(os.Stdin)
var bookIdx int
for {
fmt.Print("Select book: ")
text, err := reader.ReadString('\n')
if err == io.EOF {
os.Exit(0)
} else if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
bookIdx, err = strconv.Atoi(strings.TrimSpace(text))
if err != nil || bookIdx < 1 || bookIdx > len(bks) {
fmt.Printf("Please enter a number between 1 and %d.\n", len(bks))
continue
}
break
}
bookToEdit = bks[bookIdx-1]
}
} else {
// Edit book by ID
bookID, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid book ID.\n")
os.Exit(1)
}
foundBooks, err := library.GetBooksByID([]int64{int64(bookID)})
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting books by ID: %s", err)
os.Exit(1)
}
if len(foundBooks) == 0 {
fmt.Fprintf(os.Stderr, "Book not found.\n")
os.Exit(1)
}
bookToEdit = foundBooks[0]
}
outputTmplSrc := viper.GetString("output_template")
outputTmpl, err := template.New("filename").Funcs(funcMap).Parse(outputTmplSrc)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot parse output template: %s\n\n%s\n", err, outputTmplSrc)
os.Exit(1)
}
parser := edit.NewParser(&bookToEdit, library, outputTmpl)
parser.RunCommand("show", "")
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
line.SetCompleter(parser.Completer)
for {
cmd, err := line.Prompt(">")
if err != nil {
if err == liner.ErrPromptAborted || err == io.EOF {
return
}
fmt.Fprintf(os.Stderr, "Error reading line: %s\n", err)
return
}
if err := parse(parser, cmd); err != nil {
if err == io.EOF {
return
}
fmt.Fprintf(os.Stderr, "Error running command: %s\n", err)
return
}
}
}
func parse(parser *edit.Parser, cmd string) error {
if strings.TrimSpace(cmd) == "" {
return nil
}
lst := strings.SplitN(cmd, " ", 2)
var args string
if len(lst) > 1 {
args = lst[1]
}
err := parser.RunCommand(lst[0], args)
if err == edit.ErrUnknownCommand {
fmt.Println("Unknown command.")
return nil
}
return err
}