forked from francescomari/sdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (206 loc) · 5.52 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
func main() {
if err := newRootCommand().Execute(); err != nil {
os.Exit(1)
}
}
func newRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sdb [command]",
Short: "SDB is collection of utilities for Apache Jackrabbit Oak's Segment Store",
}
cmd.AddCommand(newTarsCommand())
cmd.AddCommand(newEntriesCommand())
cmd.AddCommand(newSegmentsCommand())
cmd.AddCommand(newSegmentCommand())
cmd.AddCommand(newIndexCommand())
cmd.AddCommand(newGraphCommand())
cmd.AddCommand(newBinariesCommand())
return cmd
}
func newTarsCommand() *cobra.Command {
var all bool
cmd := &cobra.Command{
Use: "tars [dir]",
Short: "Prints the TAR files at the provided path.",
Run: func(cmd *cobra.Command, args []string) {
directory, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to determine the working directory: %v.\n", err)
os.Exit(1)
}
if len(args) > 1 {
fmt.Fprintf(os.Stderr, "Too many arguments.\n")
os.Exit(1)
}
if len(args) == 1 {
directory = args[0]
}
if err := forEachTarFile(directory, all, doPrintTo(os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print TAR files: %v.\n", err)
os.Exit(1)
}
},
}
cmd.Flags().BoolVar(&all, "all", false, "List both active and non-active TAR files")
return cmd
}
func newEntriesCommand() *cobra.Command {
return &cobra.Command{
Use: "entries file",
Short: "Prints the entries from the specified TAR file.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
fmt.Fprintf(os.Stderr, "Too many arguments.\n")
os.Exit(1)
}
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "Too few arguments.\n")
os.Exit(1)
}
if err := forEachEntry(args[0], doPrintNameTo(os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print TAR entries: %v.\n", err)
os.Exit(1)
}
},
}
}
func newSegmentsCommand() *cobra.Command {
return &cobra.Command{
Use: "segments file",
Short: "Prints the identifiers of the segments from the specified TAR file.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "Too many arguments.")
os.Exit(1)
}
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Too few arguments.")
os.Exit(1)
}
if err := forEachMatchingEntry(args[0], isAnySegment, doPrintSegmentNameTo(os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print segment IDs: %v.\n", err)
os.Exit(1)
}
},
}
}
func newSegmentCommand() *cobra.Command {
f := formatText
cmd := &cobra.Command{
Use: "segment file id",
Short: "Prints the identifiers of the segments from the specified TAR file.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "Too few arguments.\n")
os.Exit(1)
}
if len(args) > 2 {
fmt.Fprintf(os.Stderr, "Too many arguments.\n")
os.Exit(1)
}
if err := onMatchingEntry(args[0], isSegment(args[1]), doPrintSegment(f, os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print segment: %v.\n", err)
os.Exit(1)
}
},
}
cmd.Flags().Var(&f, "format", "Output format (text, hex)")
return cmd
}
func newIndexCommand() *cobra.Command {
f := formatText
cmd := &cobra.Command{
Use: "index",
Short: "Prints the index from the specified TAR file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "Too many arguments.")
os.Exit(1)
}
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Too few arguments.")
os.Exit(1)
}
if err := onMatchingEntry(args[0], isIndex, doPrintIndex(f, os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print the index: %v.\n", err)
os.Exit(1)
}
},
}
cmd.Flags().Var(&f, "format", "Output format (text, hex)")
return cmd
}
func newGraphCommand() *cobra.Command {
f := formatText
cmd := &cobra.Command{
Use: "graph",
Short: "Prints the graph from the specified TAR file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "Too many arguments.")
os.Exit(1)
}
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Too few arguments.")
os.Exit(1)
}
if err := onMatchingEntry(args[0], isGraph, doPrintGraph(f, os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print the graph: %v.\n", err)
os.Exit(1)
}
},
}
cmd.Flags().Var(&f, "format", "Output format (text, hex)")
return cmd
}
func newBinariesCommand() *cobra.Command {
f := formatText
cmd := &cobra.Command{
Use: "binaries",
Short: "Prints the index of binary references from the specified TAR file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "Too many arguments.")
os.Exit(1)
}
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Too few arguments.")
os.Exit(1)
}
if err := onMatchingEntry(args[0], isBinary, doPrintBinaries(f, os.Stdout)); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print the index of binary references: %v.\n", err)
os.Exit(1)
}
},
}
cmd.Flags().Var(&f, "format", "Output format (text, hex)")
return cmd
}
type format string
const (
formatText format = "text"
formatHex format = "hex"
)
func (f *format) String() string {
return string(*f)
}
func (f *format) Set(s string) error {
switch format(s) {
case formatHex:
*f = formatHex
case formatText:
*f = formatText
default:
return fmt.Errorf("Invalid format '%s'", s)
}
return nil
}
func (f *format) Type() string {
return "format"
}