[hugo new] — How to overwrite existing content files?

I am looking into updating Hugo content files whenever a CSV is updated.

Currently each row in the CSV outputs a separate Hugo content file by making use of environment variables and the command hugo new

However the command hugo new does not overwrite existing files and it doesn’t seem like there is a flag that would force it to overwrite.

Should I look into third party tools to overwrite existing content files?
Or is there a way to do this in Hugo?

1 Like

My first thought: create the files from the CSV, but reference the data in it the CSV from `getCSV’; then you just keep the data file updated.

Of course doesn’t work if your items change their names or whichever key you use to create the content files… :thinking:

The CSV contains products. The quantity and the pricing will be changing from time to time and the respective content files in Hugo will need to be overwritten when needed automatically and not by hand.

Currently I am using the following workaround to first delete and then re-create a file that needs to be updated:

rm content/test/product-sku/index.md && env HUGO_TITLE="product-sku" hugo new "test/product-sku/index.md"

However I am kind of hoping there is another way.

LIke Maiki said, if the products themselves aren’t changing, just the details about the products, you could leave the sku123/index.md files that pulled data from getCVS from a specific template associated with a product kind and the current SKU.

That would avoid the overwriting problem, but not generating or deleting product content pages as they are added or removed.

Ok. I think that I will go with @maiki 's suggestion and try to do the updates from the template and not through archetypes.

1 Like

I’m using some yaml files with Data templates, and it works pretty smoothly. Biggest challenge was working through the data structure of maps and arrays, but I was dealing with potentially unknown keys. In your case with known keys for price, desc, etc, think you’ll find it’s a pretty clean solution.

You could also use an external script to make sure there’s a sku123/index.md file for every entry in the CSV file, which could also delete any that don’t exist. That script could use hugo’s archetype functionality to create the new files if you wanted.

It’s hard to say exactly how hugo will allow for content files to be created from data sources right now, so hard to choose the method that will require the least changes going forward. But the data template functionality isn’t going away, so even if you had to modify or change something that would allow hugo to create the content files as well as fill in the data, it’s probably mostly reusable.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.