this borrows some code from github.com/gogs/gogs/pkg/tool to avoid pulling it in as a dependency, along with many other indirect deps.