Use Hugo to render content from in-memory Go-Git's billy.Filesystem in bare Git repo

A little bit of context. I am new to go and still not good in debugging stuff after I get the code to compile. This is a learning exercise.

I have Hugo’s stuff in bare Git repository so there is no path to the system directory until it is git-cloned. Go-git clone is much slower than go worktree add --detach which I found the fastest way to get the directory structure needed for Hugo to render the web site.

As Go-git is able to provide me in-memory virtual file system I thought of converting that to afero.Fs and let Hugo render it further.

I got stuck already at the first step of trying to prepare the config and afero Fs for Hugo to render it from the regular directory.

func main() {
aFs := afero.NewBasePathFs(afero.NewOsFs(), "/tmp/barfoo")
cfg, err := config.FromFile(aFs, "config.toml")
if err != nil {
	return
}
osFs := hugofs.Os

cfg.Set("workingDir", "/tmp/barfoo")
l := langs.NewDefaultLanguage(cfg)
cfg.Set("languagesSorted", langs.NewLanguages(l))
fs := hugofs.NewFrom(osFs, cfg)
dd := deps.DepsCfg{}
dd.Fs = fs
dd.Cfg = cfg

sites, err := hugolib.NewHugoSites(dd)
if err != nil {
	log.Fatal("Could not load Hugo site(s)", err)
}
hh := hugolib.BuildCfg{SkipRender: true}
err = sites.Build(hh)
if err != nil {
	log.Fatal("Could not run render", err)
}
for _, p := range sites.Pages() {
	fmt.Println(p)
}
}

This is the last iteration I ended up with. There were few which compiled and looked more elegant (fewer lines of code). This one compiles too but than crashes in the runtime with this error message:
panic: runtime error: invalid memory address or nil pointer dereference

and in the rest of the log I can see it comes from afero complaining from here:

github.com/spf13/afero@v1.5.1/basepath.go:183

I respect the decision of not “opening” Hugo to be used as a library as that would probably allow for too many attempts which would make it slower and I don’t think it should be ever the direction for Hugo development.

I felt this would happen “in the spirit” of Hugo as it would all be in-memory and fast and that’s why I don’t feel embarrassed in my attempt but only in my code written so far.

Any help or hint would be highly appreciated…

Thanks…

EDIT: in /tmp/barfoo I have some minimal Hugo site which renders just fine when hugo is run regularly there.

It’s not possible to help you without the full errormessage/stack.

Here’s the full error message/stack: Use Hugo to render content from in-memory Go-Git’s billy.Filesystem in bare Git repo - Pastebin.com

I was following in the documentation what hugolib.NewHugoSites would need in order for sites.Build to pass.

I was playing a lot with deps.DepsCfg (pointer and like in this example not) and with different ways how t get the config.toml parsed and then used for hugofs.Os

I couldn’t find if I have to bring more to deps.DepsCfg struct or maybe to set more in config.Provider. Or if those structs are failing because I am not able to figure out what should be the pointer and what not.

Well, the error indicate that your afero fs is nil, which I cannot understand from your code (which looks right – I have certainly created enough tests that follow similar pattern).