72 lines
1.1 KiB
Go
72 lines
1.1 KiB
Go
|
package embed
|
||
|
|
||
|
import (
|
||
|
"embed"
|
||
|
"io"
|
||
|
"io/fs"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/charmbracelet/log"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
//go:embed cli/*
|
||
|
CLI embed.FS
|
||
|
|
||
|
//go:embed templates/*
|
||
|
Templates embed.FS
|
||
|
|
||
|
//go:embed public/*
|
||
|
Public embed.FS
|
||
|
|
||
|
//go:embed data/*
|
||
|
Data embed.FS
|
||
|
)
|
||
|
|
||
|
func CopyToWorkingDirectory(data embed.FS) error {
|
||
|
currentDir, err := os.Getwd()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := fs.WalkDir(data, ".", func(path string, info fs.DirEntry, err error) error {
|
||
|
if err != nil {
|
||
|
log.Info(err)
|
||
|
return err
|
||
|
}
|
||
|
fullDstPath := filepath.Join(currentDir, path)
|
||
|
|
||
|
if info.IsDir() {
|
||
|
log.Info("Creating folder", "path", path)
|
||
|
if err := os.MkdirAll(fullDstPath, 0755); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
srcFile, err := data.Open(path)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer srcFile.Close()
|
||
|
|
||
|
dstFile, err := os.Create(fullDstPath)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer dstFile.Close()
|
||
|
|
||
|
log.Info("Copying file", "path", path)
|
||
|
_, err = io.Copy(dstFile, srcFile)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|