Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate bytes #79

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,45 @@ import (
// Generate Go code that statically implements input filesystem,
// write the output to a file specified in opt.
func Generate(input http.FileSystem, opt Options) error {
// Write output file (all at once).
fmt.Println("writing", opt.Filename)
data, err := GenerateBytes(input, opt)
if err != nil {
return err
}
return ioutil.WriteFile(opt.Filename, data, 0644)
}

// Generate Go code that statically implements input filesystem,
// return output bytes.
func GenerateBytes(input http.FileSystem, opt Options) ([]byte, error) {
opt.fillMissing()

// Use an in-memory buffer to generate the entire output.
buf := new(bytes.Buffer)

err := t.ExecuteTemplate(buf, "Header", opt)
if err != nil {
return err
return nil, err
}

var toc toc
err = findAndWriteFiles(buf, input, &toc)
if err != nil {
return err
return nil, err
}

err = t.ExecuteTemplate(buf, "DirEntries", toc.dirs)
if err != nil {
return err
return nil, err
}

err = t.ExecuteTemplate(buf, "Trailer", toc)
if err != nil {
return err
return nil, err
}

// Write output file (all at once).
fmt.Println("writing", opt.Filename)
err = ioutil.WriteFile(opt.Filename, buf.Bytes(), 0644)
return err
return buf.Bytes(), nil
}

type toc struct {
Expand Down