-
Notifications
You must be signed in to change notification settings - Fork 1
/
ls.go
111 lines (88 loc) · 2.31 KB
/
ls.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
package main
import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"os"
"strings"
)
var LsCmd = &Command{
Usage: "ls",
Short: "list the pages and posts of the journal",
Long: `ls will list the pages and posts of the journal. The pages of the journal will
be listed first, followed by the posts.
The -c flag can be given to only display posts in the given category. The -h
flag can be given to hide pages, and display only posts. The -v flag can be
given to detail hash information about each page or post. This will display
whether or not the current item has been modified along with its current
hash.`,
Run: lsCmd,
}
func printHashInfo(hash *Hash, argv0, id string, h Hasher) {
status := "unmodified "
b, _ := hash.Get(id)
if !bytes.Equal(b, h.Hash()) {
status = "modified "
}
hex := hex.EncodeToString(b)
if hex == "" {
hex = "000000000000"
}
fmt.Println(status, id, hex)
}
func lsCmd(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)
}
var (
category string
hide bool
verbose bool
)
fs := flag.NewFlagSet(cmd.Argv0+" "+args[0], flag.ExitOnError)
fs.StringVar(&category, "c", "", "display only posts in the category")
fs.BoolVar(&hide, "h", false, "don't display pages")
fs.BoolVar(&verbose, "v", false, "display hash information about the page or post")
fs.Parse(args[1:])
pages, err := Pages()
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: failed to find pages: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
posts, err := Posts()
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: failed to find posts: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
hash, err := OpenHash()
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: failed to open hash: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
defer hash.Close()
if !hide {
for _, page := range pages {
if verbose {
printHashInfo(hash, cmd.Argv0+" "+args[0], page.ID, page)
continue
}
fmt.Println(page.ID)
}
}
category = strings.ToLower(category)
for _, post := range posts {
print_ := true
if category != "" {
print_ = category == strings.ToLower(post.Category.Name)
}
if print_ {
if verbose {
printHashInfo(hash, cmd.Argv0+" "+args[0], post.ID, post)
continue
}
fmt.Println(post.ID)
}
}
}