Cleaned up how output is written to files
This commit is contained in:
parent
87f35eef0f
commit
9c51c0fa31
1 changed files with 14 additions and 11 deletions
|
|
@ -85,18 +85,19 @@ var generateCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
outputMap := util.ConvertMapOutput(outputBytes)
|
outputMap := util.ConvertMapOutput(outputBytes)
|
||||||
// b, err := json.Marshal(outputMap)
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Printf("failed to marshal output: %v\n", err)
|
|
||||||
// os.Exit(1)
|
|
||||||
// }
|
|
||||||
if outputPath == "" {
|
if outputPath == "" {
|
||||||
// write only to stdout by default
|
// write only to stdout by default
|
||||||
|
if len(outputMap) == 1 {
|
||||||
for _, contents := range outputMap {
|
for _, contents := range outputMap {
|
||||||
fmt.Printf("%s\n", string(contents))
|
fmt.Printf("%s\n", string(contents))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
for path, contents := range outputMap {
|
||||||
|
fmt.Printf("-- file: %s, size: %d\n%s\n", path, len(contents), string(contents))
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if outputPath != "" && targetCount == 1 && len(outputMap) == 1 {
|
} else if outputPath != "" && targetCount == 1 && len(outputMap) == 1 {
|
||||||
// write just a single file using template name
|
// write just a single file using provided name
|
||||||
for _, contents := range outputBytes {
|
for _, contents := range outputBytes {
|
||||||
// FIXME: fix output paths to not overwrite each other with multiple templates
|
// FIXME: fix output paths to not overwrite each other with multiple templates
|
||||||
err := os.WriteFile(outputPath, contents, 0o644)
|
err := os.WriteFile(outputPath, contents, 0o644)
|
||||||
|
|
@ -104,16 +105,18 @@ var generateCmd = &cobra.Command{
|
||||||
fmt.Printf("failed to write config to file: %v", err)
|
fmt.Printf("failed to write config to file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("wrote file to '%s'\n", outputPath)
|
||||||
}
|
}
|
||||||
} else if outputPath != "" && targetCount > 1 {
|
} else if outputPath != "" && targetCount > 1 || len(outputMap) > 1 {
|
||||||
// write multiple files in directory using template name
|
// write multiple files in directory using template name
|
||||||
for _, contents := range outputBytes {
|
for _, contents := range outputBytes {
|
||||||
// FIXME: fix output paths to not overwrite each other with multiple templates
|
cleanPath := fmt.Sprintf("%s/%s.%s", filepath.Clean(outputPath), target, "conf")
|
||||||
err := os.WriteFile(fmt.Sprintf("%s/%s.%s", filepath.Clean(outputPath), target, "conf"), contents, 0o644)
|
err := os.WriteFile(cleanPath, contents, 0o644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to write config to file: %v", err)
|
fmt.Printf("failed to write config to file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("wrote file to '%s'\n", cleanPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue