News for hackers. Stuff that matters.
Re:Go Modules Cheat Sheet by HumblyTossed on Monday May 03, 2021 @01:34PM (#27028085)
Question: Why is go so integrated(?) with github? I mean, why can't I (or can I?) go get from my own personal gitlab server?
Re:Go Modules Cheat Sheet by q3k on Monday May 03, 2021 @01:38PM (#27028129)
You can. If you have a `.git` in your import path, it should just work OOTB, using Git as expected. Otherwise, whatever code hosting service has to implement support for returning a proprietary meta tag when serving a ?go-get=1 query - and Gitlab already does that, IIRC.

https://golang.org/cmd/go/#hdr-Remote_import_paths

Re:Go Modules Cheat Sheet by Steltek on Monday May 03, 2021 @01:47PM (#27028253)
For a truly private server, you'll need two needles of that mountainous haystack: set GOPRIVATE and tell git to use ssh first:

    git config --global url.git@HOSTNAME:.insteadOf https://HOSTNAME/
Re:Go Modules Cheat Sheet by gbear0 on Monday May 03, 2021 @03:01PM (#27029283)
Is there not a better solution for this? We have this baked into our docker builds and it irks me that we have to copy personal credentials into a docker build so we can use git to pull modules and build. Is everyone actually doing this, or do you setup read-only tokens per private module, or anything else?
Re:Go Modules Cheat Sheet by morelisp on Monday May 03, 2021 @03:56PM (#27029912)
We set up an internal Athens[0] caching proxy with credentials to pull from our GitLab. So far it seems to be win/win/win

- We don't need this dance in our Dockerfiles / build scripts / dev machines anymore. We have a baked GOPROXY / GONOSUMDB in our build image and developers configure the same proxy (via `go env -w`) locally.

- We pull all packages over the proxy, so builds are faster / use less bandwidth / still mostly work when GitHub goes down.

- SCAs get more difficult; the credentials the proxy has are more limited than any individual developer's GitLab tokens / credentials, and owning the proxy is going to be harder than a single developer's laptop.

[0] https://docs.gomods.io/

Re:Go Modules Cheat Sheet by ramonrue on Monday May 03, 2021 @03:56PM (#27029914)
At least for Gitlab, you can write a .netrc in the user‘s home dir like so:

machine <your GitLab domain> (e.g. gitlab.com) login <your GitLab id> password <your GitLab personal access token>

For the access token, you can also leverage Gitlab‘s CI Job Token.

What we do is an „echo $(netrc contents with $CI_JOB_TOKEN) > ~/.netrc“ in the pre-script in CI.

(https://stackoverflow.com/a/61257782 and https://docs.gitlab.com/ee/security/token_overview.html)

Re:Go Modules Cheat Sheet by morelisp on Monday May 03, 2021 @04:06PM (#27030006)
If you do take this approach, writing your access token in plaintext into a well-known file in your home directory seems like a strictly worse idea than the (also more broadly-compatible) Git insteadof rule to pull via SSH and your local agent.
Re:Go Modules Cheat Sheet by ramonrue on Tuesday May 04, 2021 @03:55AM (#27035002)
Yes, though it should be noted that the access token is only valid for the duration of the Job.
Re:Go Modules Cheat Sheet by travisd on Monday May 03, 2021 @07:11PM (#27032090)
For Go, I also take the vendor approach.

But for other stuff (namely private npm packages), (relatively) modern versions of Docker support build-time-only secrets: https://docs.docker.com/develop/develop-images/build_enhance.... Pass the secret as part of the docker build command and then access it inside RUN --mount=... stages.

Re:Go Modules Cheat Sheet by BigGreenTurtle on Monday May 03, 2021 @06:51PM (#27031923)
What I've done on a recent project is to vendor the dependencies before build(which are excluded from git, you could cache these if you're using a CICD system like circle), copy everything into the build container, build with -mod=vendor and then copy the resulting binary into a new container.

Don't know how that'll work out for a larger project but it works well enough in this small API I wrote.

Re:Go Modules Cheat Sheet by HumblyTossed on Monday May 03, 2021 @01:42PM (#27028177)
Thank you for the link. I have tried in the past to find this exact information and for some reason it just never came up. User error, I suppose.
Re:Go Modules Cheat Sheet by nzach on Monday May 03, 2021 @02:20PM (#27028763)
As others have already pointed out you don't need to use Github. Some people call it vanity imports[0]. In my experience 'vanity imports' give better results when googling stuff about custom import paths.

[0] - https://blog.bramp.net/post/2017/10/02/vanity-go-import-path...

Re:Go Modules Cheat Sheet by trianglesphere on Monday May 03, 2021 @01:42PM (#27028187)
All you need to do is have a webpage that has a meta tag with name="go-import" and a content tag with content="URL src-control-method source-control link"

See xyzc.dev/go/ppgen and inspect the source for an example.

Re:Go Modules Cheat Sheet by whateveracct on Monday May 03, 2021 @01:38PM (#27028128)
I think you can? The urls in package names are just urls. So if you have a url to a git repo, it'll work.
Re:Go Modules Cheat Sheet by HumblyTossed on Monday May 03, 2021 @01:41PM (#27028164)
Okay, interesting. I tried looking that up, but I never once found an example and, well, I simply never thought to try it.

Thank you.

Re:Go Modules Cheat Sheet by tsimionescu on Monday May 03, 2021 @01:58PM (#27028418)
It isn't, but it is limited to a few source control solutions. If you use something other than Git, Bazaar, SVN, Mercurial, or Fossil, then you're not going to be able to publish Go modules.

Also, Go modules are dependent on DNS, so any domain name change for your repo will have to result in code (import path) changes.

Re:Go Modules Cheat Sheet by gary-kim on Monday May 03, 2021 @02:13PM (#27028660)
A change in your repo does not need to result in an import path change if you set up vanity URLs for your import path. For instance, all my go libraries use the import path of gomod.garykim.dev even though the repos are actually hosted on GitHub. That means if I ever change to self-hosting, for example, I just need to update some of the meta tags on the gomod.garykim.dev website to change the repo location.
Re:Go Modules Cheat Sheet by tsimionescu on Monday May 03, 2021 @02:21PM (#27028773)
That still means that if you lose your domain name (gomod.garykim.dev) for whatever reason, you have to modify your code.
Re:Go Modules Cheat Sheet by morelisp on Monday May 03, 2021 @04:16PM (#27030101)
You can use a replace directive to handle cases like this without updating the code.

https://golang.org/ref/mod#go-mod-file-replace

Re:Go Modules Cheat Sheet by tsimionescu on Tuesday May 04, 2021 @02:18AM (#27034560)
Only if you're the final consumer of that module - if you publish it so others use your module, the replace directive will be ignored and they will get a failed build (or have to add their own replace directive for your sub-dependency).
Re:Go Modules Cheat Sheet by morelisp on Tuesday May 04, 2021 @02:31AM (#27034631)
Yes, but this is roughly the same as any packaging system - the ones that don't use DNS as a default namespace require you to add explicit package repositories, which is no more or less onerous than the replace directive.