-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
67 lines (53 loc) · 1.28 KB
/
utils.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"path/filepath"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lexmodelbuildingservice"
"github.com/ghodss/yaml"
)
func checkError(err error) {
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case lexmodelbuildingservice.ErrCodeNotFoundException:
break
default:
log.Fatal(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Fatal(err.Error())
}
}
}
func readAndUnmarshal(fileName string, destination interface{}) {
thefile, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal("reading config file", err.Error())
}
switch filepath.Ext(fileName) {
case ".yaml", ".yml":
err = yaml.Unmarshal(thefile, destination)
default:
err = json.Unmarshal(thefile, destination)
}
if err != nil {
log.Fatal("parsing config file", err.Error())
}
}
func marshalAndWrite(fileName string, toJson bool, source interface{}) {
var file []byte
extension := ".yaml"
if toJson {
file, _ = json.MarshalIndent(source, "", " ")
extension = ".json"
} else {
file, _ = yaml.Marshal(source)
}
err := ioutil.WriteFile(fileName+extension, file, 0644)
checkError(err)
}