Update yaml2json.go - fix #8172
parent
6082650d6c
commit
a1be3e9931
|
@ -30,16 +30,31 @@ type Data struct {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) != 3 {
|
if len(os.Args) != 3 {
|
||||||
fmt.Println("Usage: go run main.go <directory> <output_file>")
|
fmt.Println("Usage: go run main.go <directory1[,directory2,...]> <output_file>")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
directory := os.Args[1]
|
input := os.Args[1]
|
||||||
outputFile := os.Args[2]
|
outputFile := os.Args[2]
|
||||||
|
var directories []string
|
||||||
|
|
||||||
|
// Check if the input contains a comma
|
||||||
|
if strings.Contains(input, ",") {
|
||||||
|
directories = strings.Split(input, ",")
|
||||||
|
} else {
|
||||||
|
directories = []string{input}
|
||||||
|
}
|
||||||
|
|
||||||
var data []Data
|
var data []Data
|
||||||
|
|
||||||
|
for _, directory := range directories {
|
||||||
|
fmt.Println("Generating data for", directory)
|
||||||
|
|
||||||
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error accessing path %s: %v\n", path, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
|
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
|
||||||
yamlFile, err := ioutil.ReadFile(path)
|
yamlFile, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -71,6 +86,7 @@ func main() {
|
||||||
fmt.Printf("Error reading directory: %v\n", err)
|
fmt.Printf("Error reading directory: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var jsonData []byte
|
var jsonData []byte
|
||||||
for _, d := range data {
|
for _, d := range data {
|
||||||
|
@ -82,7 +98,7 @@ func main() {
|
||||||
jsonData = append(jsonData, temp...)
|
jsonData = append(jsonData, temp...)
|
||||||
jsonData = append(jsonData, byte('\n'))
|
jsonData = append(jsonData, byte('\n'))
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(outputFile, jsonData, 0644)
|
err := ioutil.WriteFile(outputFile, jsonData, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error writing JSON data to file: %v\n", err)
|
fmt.Printf("Error writing JSON data to file: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -90,4 +106,3 @@ func main() {
|
||||||
|
|
||||||
fmt.Println("JSON data written to", outputFile)
|
fmt.Println("JSON data written to", outputFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue