Go get automation – no more remembering library names

Hello! When I started writing in Go, I started and abandoned about a dozen small pet projects. Most of them used fast http and his fasthttp/router. and package testify – necessarily. Later, it was time to create business services within your company, the number of libraries grew. And every time I created a new repository, the first thing I had to do was add the libraries that I would use. For this, it was used go getbecause I already have go 1.11 in my hands and I need to form go.mod. Usually the required libraries are copied from go.mod neighboring project. But copy-paste is not our method!

Yes, I remember exactly, that day I once again remembered how to spell github.com/valyala/fasthttp correctly and I almost didn’t make a mistake – I just missed the letter t in the word github. And I realized that it was time for automation… go get. Googling a similar tool at that time did not give anything, so I decided to do something of my own. Let’s call the tool gost – Go start

What I wanted in the first place:

  1. I do not want to remember the names of libraries, I want to have synonyms for them.

  2. There is a set of libraries that are installed immediately in a bundle, a bundle – for example, fasthttp and a router for it. Or gorm and its driver for PostgreSQL – I would like to put this as one synonym.

  3. I want to specify a set of aliases in one command, let him drag them in a crowd. For example: gost webserver gormpg prometheus cli.

  4. I want to configure in YAML.

  5. I will not bike anything, my task is to call go get.

  6. I want to have the default settings wired into the binary.

  7. For testing and debugging, I want to have a soft launch mode – when in fact the utility does nothing, but only shows a list of commands that it will call.

We will make everything as simple as possible – there is a config (since I started already at the time of go 1.16, it’s just embedded YAML), it has a list of aliases for libraries and bundles. We read the command line, if we find an alias, we substitute its value and run the usual go get.

If you turn on the soft launch mode, it may look like this, for example:

gost mod webserver -s
Use soft Launch

/usr/local/go/bin/go get -u github.com/valyala/fasthttp
/usr/local/go/bin/go get -u github.com/fasthttp/router
/usr/local/go/bin/go get -u github.com/stretchr/testify
/usr/local/go/bin/go get -u github.com/satmaelstorm/envviper
/usr/local/go/bin/go mod download

Ok, it worked for me and I, in general, like everything. But there are other people, my aliases, and even more so my sets of libraries (bundles) may not be suitable for them. It is necessary to give the opportunity to replace the wired config with an external one.

No sooner said than done. In the first iteration, for some reason, it occurred to me to replace the config from the command line:

gost mod --aliases=aliases.yaml webserver pgsql

Hm. Something is wrong here. It’s inconvenient! This parameter must be specified each time. Let’s define an environment variable that points to the desired file, which we will read every time the utility is launched:

export GOST_ALIASES="/home/user/gost.aliases.yaml"

Now it’s convenient.

Well, what if the basic settings are generally satisfactory, but you want to overwrite something or add something of your own? Then we define another environment variable that will point to the file with additions / replacements relative to the base configuration (both hardwired and overridden):

export GOST_ADD_ALIASES="/home/user/gost.aliases.yaml"

Now we have everything to show it to other developers on Go, it is enough for them to execute on their own:

go install github.com/satmaelstorm/gost@latest

Utility repository for those interested hereit can also be found wired alias configuration example. There is also a slightly more detailed README – since there are a little more possibilities than I described. For example, I showed you the mod sub-command, and there is also start.

PS Yes, I made the utility a long time ago and have been using it for a long time. Just now brought it to the community. Ready for revisions at the request of colleagues.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *