/
main.go
71 lines (57 loc) · 1.38 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 (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/haproxytech/hcl-converter/configuration"
)
var usage string = `Usage:
hcl-converter <input-hcl-file> [output-yml-file]
Options:
-h, --help Show this help
`
func main() {
args := os.Args[1:]
if len(args) < 1 {
fmt.Print("Please provide a path to the HCL configuration file!\n")
fatal(nil)
}
if args[0] == "-h" || args[0] == "--help" || args[0] == "-?" {
fmt.Print(usage)
os.Exit(0)
}
input := args[0]
output := strings.Replace(input, filepath.Ext(input), ".yml", 1)
if len(args) > 1 {
output = args[1]
}
storageHCL := &configuration.StorageHCL{}
err := storageHCL.Load(input)
if err != nil {
_, isPathError := err.(*os.PathError)
if isPathError {
fmt.Printf("Given input configuration file does not exist, input file: %s\n", input)
fatal(err)
} else {
fmt.Printf("Given input configuration file cannot be parsed as HCL, input file: %s\n", input)
fatal(err)
}
}
storageYAML := &configuration.StorageYAML{}
storageYAML.Set(storageHCL.Get())
err = storageYAML.SaveAs(output)
if err != nil {
fmt.Printf("There was an error while storing the YAML configuration file: %s\n", output)
fatal(err)
}
fmt.Printf("Input file: %s\n", input)
fmt.Printf("Output file: %s\n", output)
}
func fatal(err error) {
if err != nil {
fmt.Println(err)
}
fmt.Print(usage)
os.Exit(1)
}