forked from Zakay/gofigure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (50 loc) · 1.1 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var stderr = log.New(os.Stderr, "", 0)
func check(e error) {
if e != nil {
panic(e)
}
}
var outFile string
var inFile string
func init() {
flag.StringVar(&outFile, "o", "", "Output filename")
flag.StringVar(&inFile, "i", "", "Input filename")
}
func main() {
flag.Parse()
if inFile == "" || len(os.Args) < 2 {
stderr.Println("Need a file to parse")
fmt.Println("usage: " + os.Args[0] + " -i inFile [-o outFile]")
flag.PrintDefaults()
os.Exit(1)
}
path, err := filepath.Abs(inFile)
check(err)
pathParts := strings.Split(path, "/")
pathParts = pathParts[:len(pathParts)-1]
path = strings.Join(pathParts, "/")
err = os.Chdir(path)
check(err)
inFilePathParts := strings.Split(inFile, "/")
inFileName := inFilePathParts[len(inFilePathParts)-1]
config := ParseFile(inFileName, nil)
mapped := config.Transform()
marshaled, err := json.Marshal(mapped)
check(err)
if outFile == "" {
fmt.Println(string(marshaled))
} else {
ioutil.WriteFile(outFile, marshaled, 0644)
}
}