forked from lmika/oaipmh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdget.go
97 lines (84 loc) · 2.45 KB
/
cmdget.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
package main
import (
"fmt"
"os"
"bufio"
"flag"
"log"
"strings"
"encoding/xml"
)
// ---------------------------------------------------------------------------------------------------
// Get command
// Used to retrieve records.
type GetCommand struct {
Ctx *Context
header *bool
test *bool
separator *string
count int
}
func (gc *GetCommand) Flags(fs *flag.FlagSet) *flag.FlagSet {
gc.header = fs.Bool("H", false, "Display record header")
gc.test = fs.Bool("t", false, "Test that the record can be retrieved")
gc.separator = fs.String("s", "====", "Record separator")
return fs
}
func (gc *GetCommand) Run(args []string) {
for _, id := range args {
gc.eachId(id, func(urn string) {
gc.displayRecord(urn)
})
}
}
// Interprets an id and calls the callback with each interpred ID.
func (gc *GetCommand) eachId(idExpr string, callback func(string)) {
if (idExpr[0] == '@') {
var err error
var file *os.File
if (idExpr[1:] == "-") {
file = os.Stdin
} else {
file, err = os.Open(idExpr[1:])
if (err != nil) {
panic(err)
}
defer file.Close()
}
lineReader := bufio.NewReader(file)
for id, err := lineReader.ReadString('\n') ; err == nil ; id, err = lineReader.ReadString('\n') {
id = strings.TrimSpace(id)
callback(id)
}
if err != nil {
panic(err)
}
} else {
callback(idExpr)
}
}
func (gc *GetCommand) displayRecord(id string) {
if (gc.count >= 1) && (! *(gc.test)) {
fmt.Printf("%s\n", *(gc.separator))
}
resp, err := gc.Ctx.Session.GetRecord(id)
if (err == nil) {
if *(gc.test) {
fmt.Printf("+ %s\n", resp.Header.Identifier)
} else if *(gc.header) {
fmt.Printf("Id:\t%s\n", resp.Header.Identifier)
fmt.Printf("Date:\t%s\n", resp.Header.DateStamp.String())
fmt.Printf("Sets:\t%s\n", strings.Join(resp.Header.SetSpec, ", "))
} else {
fmt.Print(xml.Header)
fmt.Println(strings.TrimSpace(resp.Content.Xml))
}
} else {
if *(gc.test) {
fmt.Printf("- %s\n", id)
} else {
log.Printf("Error: Cannot get record '%s', %s", id, err.Error())
}
}
gc.count++
}