-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (76 loc) · 1.72 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
package main
import (
"encoding/json"
"errors"
"io"
"log"
"os"
"github.com/DJDNS/go-deje/document"
"github.com/DJDNS/go-deje/util"
)
func deserializeDocument(r io.Reader) (interface{}, error) {
doc := document.NewDocument()
err := doc.Deserialize(r)
return doc, err
}
func deserializeEvent(r io.Reader) (interface{}, error) {
event := document.Event{}
decoder := json.NewDecoder(r)
err := decoder.Decode(&event)
return event, err
}
func deserializeInput(object_type string, r io.Reader) (interface{}, error) {
switch object_type {
case "document":
return deserializeDocument(r)
case "event":
return deserializeEvent(r)
default:
return nil, errors.New("No such object type: " + object_type)
}
}
func formatPretty4(object interface{}) ([]byte, error) {
return json.MarshalIndent(object, "", " ")
}
func formatCompact(object interface{}) ([]byte, error) {
return json.Marshal(object)
}
func formatHash(object interface{}) ([]byte, error) {
hash, err := util.HashObject(object)
return []byte(hash), err
}
func serializeOutput(format string, object interface{}, w io.Writer) error {
var buf []byte
var err error
switch format {
case "pretty4":
buf, err = formatPretty4(object)
case "compact":
buf, err = formatCompact(object)
case "hash":
buf, err = formatHash(object)
default:
err = errors.New("No such format: " + format)
}
if err != nil {
return err
}
_, err = w.Write(buf)
return err
}
func main() {
args := os.Args
if len(args) < 3 {
log.Fatal("Insufficient arguments")
}
object_type := args[1]
format := args[2]
object, err := deserializeInput(object_type, os.Stdin)
if err != nil {
log.Fatal(err)
}
err = serializeOutput(format, object, os.Stdout)
if err != nil {
log.Fatal(err)
}
}