-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (83 loc) · 2.46 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
// Copyright 2016 David Lavieri. All rights reserved.
// Use of this source code is governed by a MIT License
// License that can be found in the LICENSE file.
package main
import (
"errors"
"flag"
"fmt"
"os"
)
var inputArg = flag.String("i", "", "Input file path")
var outputArg = flag.String("o", "", "Output file path (if not provied will overwrite input file)")
var braceArg = flag.Bool("b", false, "Parse braces into {delim}")
var delimArg = flag.Bool("d", false, "Parse {delim} into braces")
var rmArg = flag.Bool("rm", false, "Remove backup file after parse")
var owArg = flag.Bool("ow", false, "Overwrite backup file if already exist")
func main() {
flag.Parse()
args := map[string]interface{}{
"backupSuffix": "_backup",
"inputPath": *inputArg,
"outputPath": *outputArg,
"removeBackup": *rmArg,
"overWrite": *owArg,
"brace": *braceArg,
"delim": *delimArg,
}
code, err := altMain(args)
if err != nil {
fmt.Println(err)
}
os.Exit(code)
}
func altMain(args map[string]interface{}) (int, error) {
backupSuffix := args["backupSuffix"].(string)
inputPath := args["inputPath"].(string)
outputPath := args["outputPath"].(string)
removeBackup := args["removeBackup"].(bool)
overWrite := args["overWrite"].(bool)
brace := args["brace"].(bool)
delim := args["delim"].(bool)
if !brace && !delim {
return 1, errors.New("Must choose an type of action delim or brace parse")
} else if brace && delim {
return 1, errors.New("Must choose between delim or brace parse, not both")
}
if outputPath == "" {
outputPath = inputPath
}
backup, err := createBackup(inputPath, backupSuffix, overWrite)
if err != nil {
return 2, fmt.Errorf("Error ocurred during backup creation: %s", err)
}
inputFile, err := os.Open(backup)
defer inputFile.Close()
if err != nil {
return 3, fmt.Errorf("Error ocurred while trying to read backup file: %s", err)
}
outputFile, err := os.Create(outputPath)
defer inputFile.Close()
if err != nil {
return 4, fmt.Errorf("Error ocurred creating output file: %s", err)
}
if brace {
err = parseBraces(inputFile, outputFile)
} else if delim {
err = parseDelims(inputFile, outputFile)
}
if err != nil {
t := "brace"
if delim {
t = "delim"
}
return 5, fmt.Errorf("Error during %s parse operation: %s", t, err)
}
if removeBackup {
err = os.Remove(inputFile.Name())
if err != nil {
return 6, fmt.Errorf("Error ocurred trying to remove backup file %s", err)
}
}
return 0, nil
}