go.path is a small utility that allows Makefiles and scripts to easily pull the correct folder for cloning, downloading or building go projects.
Three providers are included in the project, you can implement your own if necessary.
Simply implement the
PathProvider interface and set the global
variable in the
gopath subpackage, then call the CLI.
The included providers are executed in this order:
--disable flag allows one to disable certain providers.
Note that if no provider can return a good path, the binary fails with exit code 5.
This provider accepts one parameter:
If this flag is present, the provider will prefer a second
gopath in the environment variable
GOPATH if it is present.
The config provider will check if any of the following files is present:
The first file from this list that exists is read and return on stdout, no filtering, formatting or parsing.
This provider simply returns the parameter
--gopath if it was
The provider may be used for fallbacks if no gopath is set and no config provided either, for example if go hasn't been installed and configured.
--allow-defpath is set or given then it will default to
as a gopath. Beware that this won't allow the tool to fail since
this provider always has a value. Note that this tool also may
fail due to other reasons.
If possible, use
go.path --env-proj --gopath=~/go/
If the GOPATH environment variable is set to
then this will return
~/projects, if no GOPATH is set it will
return whatever is written in one of the config files and if those
aren't present it will deault to
Actual Real Life Examples
Consider the following script:
git clone email@example.com:user/repo $GOPATH/src/git.example.org/user/repo
This line is problematic. It is not exactly portable, it reacts badly to dual GOPATHs and lastly if no gopath is present, the repo will be cloned into the root fs.
go.path fixes that:
git clone firstname.lastname@example.org:user/repo $(go.path --allow-defpath)/src/git.example.org/user/repo
This will always return a sane gopath to use.