Skip to content

Commit

Permalink
fix use of filepath.Name
Browse files Browse the repository at this point in the history
  • Loading branch information
mildwonkey committed Sep 30, 2024
1 parent 703c340 commit f68c8b3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/pkg/domains/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ func (d Domain) GetResources() (types.DomainResources, error) {
// removed.
defer os.RemoveAll(dst)

// make a map of rel filepaths to the user-supplied name, so we can re-key the DomainResources later on.
filenames := make(map[string]string, len(d.Spec.Filepaths))

// Copy files to a temporary location
for _, path := range d.Spec.Filepaths {
bytes, err := network.Fetch(path.Path)
if err != nil {
return nil, fmt.Errorf("error getting source files: %w", err)
}
// use the user-defined Name when writing the temporary file.
os.WriteFile(filepath.Join(dst, path.Name), bytes, 0666)

// We'll just use the filename when writing the file so it's easier to reference later
relname := filepath.Base(path.Path)

err = os.WriteFile(filepath.Join(dst, relname), bytes, 0666)
if err != nil {
return nil, fmt.Errorf("error writing local files: %w", err)
}
// and save this info for later
filenames[relname] = path.Name
}

// get a list of all the files we just downloaded in the temporary directory
Expand All @@ -58,14 +69,14 @@ func (d Domain) GetResources() (types.DomainResources, error) {
}

// clean up the resources so it's using the filepath.Name as the map key,
// istead of the file path.
// istead of the file path.src/pkg/domains/files/files.go
drs := make(types.DomainResources, len(config))
for k, v := range config {
rel, err := filepath.Rel(dst, k)
if err != nil {
return nil, fmt.Errorf("error determining relative file path: %w", err)
}
drs[rel] = v
drs[filenames[rel]] = v
}
return drs, nil
}
Expand Down

0 comments on commit f68c8b3

Please sign in to comment.