-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat.go
73 lines (59 loc) · 1.34 KB
/
cat.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
package main
import (
"fmt"
"io"
"os"
)
var CatCmd = &Command{
Usage: "cat <page|post,...>",
Short: "display the contents of a page or post",
Long: `Cat will display the contents of the given pages or posts.`,
Run: catCmd,
}
func catCmd(cmd *Command, args []string) {
if err := initialized(""); err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "%s %s: usage: %s\n", cmd.Argv0, args[0], cmd.Usage)
os.Exit(1)
}
code := 0
for _, id := range args[1:] {
var path string
page, ok, err := GetPage(id)
if err != nil {
code = 1
fmt.Fprintf(os.Stderr, "%s %s: failed to get page: %s\n", cmd.Argv0, args[0], err)
continue
}
if ok {
path = page.SourcePath
} else {
post, ok, err := GetPost(id)
if err != nil {
code = 1
fmt.Fprintf(os.Stderr, "%s %s: failed to get post: %s\n", cmd.Argv0, args[0], err)
continue
}
if !ok {
code = 1
fmt.Fprintf(os.Stderr, "%s %s: no such page or post\n", cmd.Argv0, args[0])
continue
}
path = post.SourcePath
}
func(path string) {
f, err := os.Open(path)
if err != nil {
code = 1
fmt.Fprintf(os.Stderr, "%s %s: no such page or post\n", cmd.Argv0, args[0])
return
}
defer f.Close()
io.Copy(os.Stdout, f)
}(path)
}
os.Exit(code)
}