Skip to content

Commit

Permalink
Fix for variables_file containing a directory (#313)
Browse files Browse the repository at this point in the history
Fixes #312 

### BUG FIXES

Create a directory path for the destination file if missing.
  • Loading branch information
MichielBijland authored Sep 29, 2023
2 parents fdb6a71 + 7f944c8 commit f1fbb7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20230929-111318.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Fix for variables_file containing a directory
time: 2023-09-29T11:13:18.002246+02:00
13 changes: 10 additions & 3 deletions internal/generator/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/mach-composer/mach-composer-cli/internal/state"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/mach-composer/mach-composer-cli/internal/state"

"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -83,6 +84,12 @@ func WriteFiles(ctx context.Context, cfg *config.MachConfig, options *GenerateOp
for _, fs := range cfg.Variables.GetEncryptedSources(site.Identifier) {
target := filepath.Join(path, fs.Filename)
log.Info().Msgf("Copying %s", target)

// This can refer to a file outside of the current directory, so we need to create the directory structure
if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil {
return nil, fmt.Errorf("error creating directory structure for variables: %w", err)
}

if err := copyFile(fs.Filename, target); err != nil {
return nil, fmt.Errorf("error writing extra file: %w", err)
}
Expand Down Expand Up @@ -164,7 +171,7 @@ func copyFile(srcPath, dstPath string) error {
defer src.Close()

// Read the contents of the source file
srcContents, err := ioutil.ReadAll(src)
srcContents, err := io.ReadAll(src)
if err != nil {
return err
}
Expand Down

0 comments on commit f1fbb7d

Please sign in to comment.