-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeh.go
120 lines (99 loc) · 2.37 KB
/
meh.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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/valueof/meh/formatters"
"github.com/valueof/meh/parser"
http "github.com/valueof/meh/server"
"github.com/valueof/meh/util"
)
var VERSION string = "0.3"
var dir *string
var zip *string
var output *string
var verbose *bool
var withImages *bool
var version *bool
var server *string
var logger *log.Logger
var logbuf bytes.Buffer
func init() {
dir = flag.String("dir", "", "path to the uncompressed medium archive")
zip = flag.String("zip", "", "path to the compressed medium archive")
output = flag.String("out", "", "output directory")
server = flag.String("server", "", "run web version of meh on provided address")
verbose = flag.Bool("verbose", false, "whether to print logs to stdout")
version = flag.Bool("version", false, "print version and exit")
withImages = flag.Bool("withImages", false, "whether to download images from medium cdn")
logger = log.New(&logbuf, "meh: ", log.Lmsgprefix)
}
func run() error {
if *verbose {
logger.SetOutput(os.Stdout)
}
if *version {
fmt.Printf("meh %s\n", VERSION)
return nil
}
if *server != "" {
http.RunHTTPServer(*server)
return nil
}
if (*dir == "" && *zip == "") || *output == "" {
flag.Usage()
return nil
}
input := ""
switch {
case *zip != "":
tmp := filepath.Join(*output, ".archive")
err := util.UnzipArchive(*zip, tmp)
if err != nil {
logger.Printf("UnzipArchive(%s, %s): %v", *zip, tmp, err)
return err
}
defer func() {
logger.Printf("clean up: removing %s", tmp)
os.RemoveAll(tmp)
}()
logger.Printf("extracted archive into %s", tmp)
input, err = util.FindArchiveRoot(tmp)
if err != nil {
logger.Printf("FindArchiveRoot(%s): %v", tmp, err)
return err
}
case *dir != "":
input = *dir
}
input, err := filepath.Abs(input)
if err != nil {
logger.Printf("filepath.Abs(): %v", err)
return err
}
logger.Printf("using directory %s as input", input)
w := formatters.NewJSONFormatter(*output, *logger)
p := parser.NewParser(input, *logger, w)
err = p.Parse()
if err != nil {
logger.Printf("parser.Parse(): %v", err)
return err
}
if *withImages {
p.FetchImages(*output)
} else {
logger.Printf("not downloading images, use -withImages if you want to download images")
}
return nil
}
func main() {
flag.Parse()
err := run()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}