This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
/
tfiles.go
88 lines (79 loc) · 2.36 KB
/
tfiles.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
package tfiles
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/ahelal/t-template/toutput"
"github.com/ghodss/yaml"
)
// TFileType verbosity level
var TFileType = map[string]int{
"JSON": 0,
"YAML": 1,
}
// ReadInputFile read file and return it's content
func ReadInputFile(filePath string) []byte {
data, err := ioutil.ReadFile(filePath)
toutput.CheckError(err, fmt.Sprintf("Failed to read input file '%s'", filePath), true)
return data
}
// ReadInputFiles loop over JSON files and combine the content
func ReadInputFiles(listFiles []string, fileType int) []interface{} {
var walkfiles []string
var fileData interface{}
var combinedData []interface{}
for _, aFile := range listFiles {
// Lets check if we have directory and flaten all files if any
fileStat, err := os.Stat(aFile)
toutput.CheckError(err, fmt.Sprintf("Failed to stat input file '%s'", aFile), true)
if fileStat.IsDir() {
err := filepath.Walk(aFile, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
walkfiles = append(walkfiles, path)
return nil
})
toutput.CheckError(err, fmt.Sprintf("Directory looping failed '%s' as JSON", aFile), true)
} else {
walkfiles = append(walkfiles, aFile)
}
}
// We can then loop over files and read the contents
for _, aFile := range walkfiles {
raw := ReadInputFile(aFile)
if fileType == TFileType["YAML"] {
err := yaml.Unmarshal(raw, &fileData)
toutput.CheckError(err, fmt.Sprintf("Could not parse '%s' as YAML", aFile), true)
} else {
err := json.Unmarshal(raw, &fileData)
toutput.CheckError(err, fmt.Sprintf("Could not parse '%s' as JSON", aFile), true)
}
combinedData = append(combinedData, fileData)
}
return combinedData
}
// ReadInputStdin Read from STDIN
func ReadInputStdin(fileType int) []interface{} {
var fileData interface{}
var combinedData []interface{}
var err error
raw := readInputFromStdin()
if fileType == TFileType["YAML"] {
err = yaml.Unmarshal(raw, &fileData)
} else {
err = json.Unmarshal(raw, &fileData)
}
toutput.CheckError(err, "Could not parse string from stdin", true)
combinedData = append(combinedData, fileData)
return combinedData
}
func readInputFromStdin() []byte {
var data []byte
var e error
data, e = ioutil.ReadAll(os.Stdin)
toutput.CheckError(e, "Failed to read input file 'stdin'", true)
return data
}