Skip to content

Commit

Permalink
refactor: using afero for all FS access
Browse files Browse the repository at this point in the history
  • Loading branch information
viktomas committed Oct 8, 2022
1 parent 7edc00b commit cf3bef4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
31 changes: 5 additions & 26 deletions file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,18 @@ package main

import (
"io"
"os"
)

func readFileToString(src string) (string, error) {
srcFile, err := os.Open(src)
if err != nil {
return "", err
}
defer srcFile.Close()
bytes, err := os.ReadFile(src)
if err != nil {
return "", err
}
return string(bytes), nil
}

func writeStringToFile(dest string, content string) error {
err := os.WriteFile(dest, []byte(content), os.ModePerm)
if err != nil {
return err
}

return nil
}
"github.com/spf13/afero"
)

func copy(src, dest string) error {
srcFile, err := os.Open(src)
func copy(appFS afero.Fs, src, dest string) error {
srcFile, err := appFS.Open(src)
if err != nil {
return err
}
defer srcFile.Close()

destFile, err := os.Create(dest) // creates if file doesn't exist
destFile, err := appFS.Create(dest) // creates if file doesn't exist
if err != nil {
return err
}
Expand Down
56 changes: 33 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,44 @@ func main() {
if err != nil {
log.Fatalf("Error during walking through a folder %v", err)
}
puq := parseUnquotedProperties(*unquotedProperties)
for _, publicFile := range publicFiles {
srcContent, err := readFileToString(publicFile)
err = exportPublicPage(appFS, publicFile, *webAssetsPathPrefix, *blogFolder, *assetsRelativePath, puq)
if err != nil {
log.Fatalf("Error when reading the %q file: %v", publicFile, err)
}
_, name := filepath.Split(publicFile)
page := parsePage(name, srcContent)
result := transformPage(page, *webAssetsPathPrefix)
assetFolder := filepath.Join(*blogFolder, *assetsRelativePath)
err = copyAssets(publicFile, assetFolder, result.assets)
if err != nil {
log.Fatalf("Error when copying assets for page %q: %v", publicFile, err)
}
dest := filepath.Join(*blogFolder, result.filename)
folder, _ := filepath.Split(dest)
err = os.MkdirAll(folder, os.ModePerm)
if err != nil {
log.Fatalf("Error when creating parent directory for %q: %v", dest, err)
}
err = writeStringToFile(dest, render(result, parseUnquotedProperties(*unquotedProperties)))
if err != nil {
log.Fatalf("Error when copying file %q: %v", dest, err)
log.Fatalf("Error when exporting page %q: %v", publicFile, err)
}
}
}

func copyAssets(baseFile string, assetFolder string, assets []string) error {
err := os.MkdirAll(assetFolder, os.ModePerm)
func exportPublicPage(appFS afero.Fs, publicFile string, webAssetsPathPrefix string, blogFolder string, assetsRelativePath string, unquotedProperties []string) error {
srcContent, err := afero.ReadFile(appFS, publicFile)
if err != nil {
return fmt.Errorf("reading the %q file failed: %v", publicFile, err)
}
_, name := filepath.Split(publicFile)
page := parsePage(name, string(srcContent))
result := transformPage(page, webAssetsPathPrefix)
assetFolder := filepath.Join(blogFolder, assetsRelativePath)
err = copyAssets(appFS, publicFile, assetFolder, result.assets)
if err != nil {
return fmt.Errorf("copying assets for page %q failed: %v", publicFile, err)
}
dest := filepath.Join(blogFolder, result.filename)
folder, _ := filepath.Split(dest)
err = appFS.MkdirAll(folder, os.ModePerm)
if err != nil {
return fmt.Errorf("creating parent directory for %q failed: %v", dest, err)
}
outputFileContent := render(result, unquotedProperties)
err = afero.WriteFile(appFS, dest, []byte(outputFileContent), 0644)
if err != nil {
return fmt.Errorf("copying file %q failed: %v", dest, err)
}
return nil
}

func copyAssets(appFS afero.Fs, baseFile string, assetFolder string, assets []string) error {
err := appFS.MkdirAll(assetFolder, os.ModePerm)
if err != nil {
log.Fatalf("Error when making assets folder %q: %v", assetFolder, err)
}
Expand All @@ -116,7 +126,7 @@ func copyAssets(baseFile string, assetFolder string, assets []string) error {
assetPath := filepath.Clean(filepath.Join(baseDir, relativeAssetPath))
_, assetName := filepath.Split(assetPath)
destPath := filepath.Join(assetFolder, assetName)
err := copy(assetPath, destPath)
err := copy(appFS, assetPath, destPath)
if err != nil {
return err
}
Expand Down

0 comments on commit cf3bef4

Please sign in to comment.