Skip to content

Commit

Permalink
feat: Allow to call BuildAndWriteFilesList on non-copied embedded dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Apr 19, 2024
1 parent d8b1f41 commit f337bba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions embed_util/file_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func buildFileListFromDir(dir string) (*fileList, error) {
return err
}

if relPath == "." {
if relPath == "." || relPath == "files.json" {
return nil
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func buildFileListFromFs(embedFs fs.FS) (*fileList, error) {
var fl fileList

err := fs.WalkDir(embedFs, ".", func(path string, d fs.DirEntry, err error) error {
if path == "." {
if path == "." || path == "files.json" {
return nil
}

Expand Down
41 changes: 32 additions & 9 deletions embed_util/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,33 @@ func CopyForEmbed(out string, dir string) error {
return err
}

return doWriteFilesList(out, fl)
}

func BuildAndWriteFilesList(dir string) error {
fl, err := buildFileListFromDir(dir)
if err != nil {
return err
}
return doWriteFilesList(dir, fl)
}

func doWriteFilesList(dir string, fl *fileList) error {
var err error
fl.ContentHash, err = calcContentHash(dir, fl)
if err != nil {
return err
}
b, err := json.MarshalIndent(fl, "", " ")
if err != nil {
return err
}

err = os.WriteFile(filepath.Join(out, "files.json"), b, 0o644)
return err
err = os.WriteFile(filepath.Join(dir, "files.json"), b, 0o644)
if err != nil {
return err
}
return nil
}

func WriteEmbedGoFile(targetDir string, goOs string, goArch string) error {
Expand Down Expand Up @@ -128,43 +148,46 @@ func copyFiles(out string, dir string, fl *fileList) error {
return err
}

return nil
}

func calcContentHash(dir string, fl *fileList) (string, error) {
hash := sha256.New()
for _, fle := range fl.Files {
path := filepath.Join(dir, fle.Name)
st, err := os.Lstat(path)
if err != nil {
return err
return "", err
}
if st.Mode().Type() == os.ModeSymlink {
sl, err := os.Readlink(path)
if err != nil {
return err
return "", err
}
_ = binary.Write(hash, binary.LittleEndian, "symlink")
_ = binary.Write(hash, binary.LittleEndian, sl)
} else if st.Mode().IsDir() {
err = os.MkdirAll(path, fle.Mode.Perm())
if err != nil {
return err
return "", err
}
_ = binary.Write(hash, binary.LittleEndian, "dir")
_ = binary.Write(hash, binary.LittleEndian, fle.Name)
} else if st.Mode().IsRegular() {
outPath := filepath.Join(out, fle.Name)
outPath := filepath.Join(dir, fle.Name)
if fle.Compressed {
outPath += ".gz"
}

data, err := os.ReadFile(outPath)
if err != nil {
return err
return "", err
}

_ = binary.Write(hash, binary.LittleEndian, "regular")
_ = binary.Write(hash, binary.LittleEndian, fle.Name)
_ = binary.Write(hash, binary.LittleEndian, data)
}
}
fl.ContentHash = hex.EncodeToString(hash.Sum(nil))
return nil
return hex.EncodeToString(hash.Sum(nil)), nil
}

0 comments on commit f337bba

Please sign in to comment.