-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
90 lines (79 loc) · 2.04 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
//
// Copyright (c) 2020 10X Genomics, Inc. All rights reserved.
//
// Command mrg is the martian command-line invocation generator.
//
// It can be used to convert back and forth between a call MRO file and a json
// representation thereof.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/martian-lang/docopt.go"
"github.com/martian-lang/martian/martian/core"
"github.com/martian-lang/martian/martian/util"
)
func main() {
util.SetupSignalHandlers()
// Command-line arguments.
doc := `Martian Invocation Generator.
Usage:
mrg
mrg --reverse
mrg -h | --help | --version
Options:
--reverse Generate invocation data from mro source.
-h --help Show this message.
--version Show version.`
martianVersion := util.GetVersion()
opts, _ := docopt.Parse(doc, nil, true, martianVersion, false)
util.ENABLE_LOGGING = false
// Martian environment variables.
cwd, _ := filepath.Abs(path.Dir(os.Args[0]))
mroPaths := util.ParseMroPath(cwd)
if value := os.Getenv("MROPATH"); len(value) > 0 {
mroPaths = util.ParseMroPath(value)
}
if opts["--reverse"].(bool) {
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
invocation, err := core.InvocationDataFromSource(src, mroPaths)
if err != nil {
os.Stderr.WriteString("Error parsing source: ")
os.Stderr.WriteString(err.Error())
os.Stderr.WriteString("\n")
os.Exit(1)
}
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(invocation); err != nil {
os.Stderr.WriteString("Error generating json: ")
os.Stderr.WriteString(err.Error())
os.Stderr.WriteString("\n")
os.Exit(1)
}
os.Exit(0)
}
// Read and parse JSON from stdin.
dec := json.NewDecoder(os.Stdin)
dec.UseNumber()
var input core.InvocationData
if err := dec.Decode(&input); err == nil {
src, bldErr := input.BuildCallSource(mroPaths)
if bldErr == nil {
fmt.Print(src)
os.Exit(0)
} else {
fmt.Println(bldErr)
os.Exit(1)
}
}
os.Exit(1)
}