mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
Fixed how multiple files are written for a target
This commit is contained in:
parent
9c51c0fa31
commit
aea3c55dd8
1 changed files with 18 additions and 17 deletions
|
|
@ -24,16 +24,6 @@ var generateCmd = &cobra.Command{
|
||||||
Use: "generate",
|
Use: "generate",
|
||||||
Short: "Generate a config file from state management",
|
Short: "Generate a config file from state management",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// if we have more than one target and output is set, create configs in directory
|
|
||||||
targetCount := len(targets)
|
|
||||||
if outputPath != "" && targetCount > 1 {
|
|
||||||
err := os.MkdirAll(outputPath, 0o755)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to make output directory: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure that we have a token present before trying to make request
|
// make sure that we have a token present before trying to make request
|
||||||
if config.AccessToken == "" {
|
if config.AccessToken == "" {
|
||||||
// TODO: make request to check if request will need token
|
// TODO: make request to check if request will need token
|
||||||
|
|
@ -85,6 +75,12 @@ var generateCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
outputMap := util.ConvertMapOutput(outputBytes)
|
outputMap := util.ConvertMapOutput(outputBytes)
|
||||||
|
|
||||||
|
// if we have more than one target and output is set, create configs in directory
|
||||||
|
var (
|
||||||
|
targetCount = len(targets)
|
||||||
|
templateCount = len(outputMap)
|
||||||
|
)
|
||||||
if outputPath == "" {
|
if outputPath == "" {
|
||||||
// write only to stdout by default
|
// write only to stdout by default
|
||||||
if len(outputMap) == 1 {
|
if len(outputMap) == 1 {
|
||||||
|
|
@ -93,13 +89,12 @@ var generateCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for path, contents := range outputMap {
|
for path, contents := range outputMap {
|
||||||
fmt.Printf("-- file: %s, size: %d\n%s\n", path, len(contents), string(contents))
|
fmt.Printf("-- file: %s, size: %d B\n%s\n", path, len(contents), string(contents))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if outputPath != "" && targetCount == 1 && len(outputMap) == 1 {
|
} else if outputPath != "" && targetCount == 1 && templateCount == 1 {
|
||||||
// write just a single file using provided name
|
// write just a single file using provided name
|
||||||
for _, contents := range outputBytes {
|
for path, contents := range outputMap {
|
||||||
// FIXME: fix output paths to not overwrite each other with multiple templates
|
|
||||||
err := os.WriteFile(outputPath, contents, 0o644)
|
err := os.WriteFile(outputPath, 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)
|
||||||
|
|
@ -107,10 +102,16 @@ var generateCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
fmt.Printf("wrote file to '%s'\n", outputPath)
|
fmt.Printf("wrote file to '%s'\n", outputPath)
|
||||||
}
|
}
|
||||||
} else if outputPath != "" && targetCount > 1 || len(outputMap) > 1 {
|
} else if outputPath != "" && targetCount > 1 || templateCount > 1 {
|
||||||
// write multiple files in directory using template name
|
// write multiple files in directory using template name
|
||||||
for _, contents := range outputBytes {
|
err := os.MkdirAll(filepath.Clean(outputPath), 0o755)
|
||||||
cleanPath := fmt.Sprintf("%s/%s.%s", filepath.Clean(outputPath), target, "conf")
|
if err != nil {
|
||||||
|
fmt.Printf("failed to make output directory: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
for path, contents := range outputBytes {
|
||||||
|
filename := filepath.Base(path)
|
||||||
|
cleanPath := fmt.Sprintf("%s/%s.%s", filepath.Clean(outputPath), filename)
|
||||||
err := os.WriteFile(cleanPath, 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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue