plain git private go modules

someone complained it was too hard

SEAN K.H. LIAO

plain git private go modules

someone complained it was too hard

go modules

GOPATH is dying, better switch to modules soon, so how can you make it work with just a git (no http(s))?

ssh setup

make ssh work automatically, ie ssh arch.seankhliao.com. ~/.ssh/config:

1Host arch.seankhliao.com
2    User arccy                      # assuming you don't use a generic git user
3    Port 443                        # assuming you use nonstandard port
4    IdentityFile ~/.ssh/id_ecdsa_sk # ssh keyzzzz

git setup

make git always use ssh, and point it to your home directory. ~/.gitconfig:

1[url "arccy@arch.seankhliao.com:"]
2    insteadOf = "git://arch.seankhliao.com/"
3    insteadOf = "https://arch.seankhliao.com/"

dependency

create a dependency!

1$ mkdir -p ~/tmp/hello && cd ~/tmp/hello
2
3$ go mod init arch.seankhliao.com/hello.git
4
5$ cat << EOF > hello.go
6package hello
7
8var Hello = "world"
9EOF

create a git repo on the remote

1$ ssh arch.seankhliao.com git init --bare hello -b main
2Initialized empty Git repository in /home/arccy/hello/

create a git repo locally and push to remote

 1$ git init -b main
 2Initialized empty Git repository in /home/arccy/tmp/hello/.git/
 3
 4$ git add .
 5
 6$ git commit -m "init"
 7[main (root-commit) 02adee5] init
 8 1 file changed, 3 insertions(+)
 9 create mode 100644 hello.go
10
11$ git remote add origin https://arch.seankhliao.com/hello
12
13$ git push -u origin main
14Enumerating objects: 3, done.
15Counting objects: 100% (3/3), done.
16Writing objects: 100% (3/3), 240 bytes | 240.00 KiB/s, done.
17Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
18To arch.seankhliao.com:hello
19 * [new branch]      main -> main
20Branch 'main' set up to track remote branch 'main' from 'origin'.

main

Now to use the dependency

 1$ mkdir -p ~/tmp/world && cd ~/tmp/world
 2
 3# you may want to persist this setting with go env -w GOPRIVATE=...
 4$ export GOPRIVATE=arch.seankhliao.com
 5
 6$ go mod init example/world
 7go: creating new go.mod: module example/world
 8
 9$ cat << EOF > main.go
10package main
11
12import (
13        "fmt"
14
15        "arch.seankhliao.com/hello.git" // use .git to tell Go about repo root
16)
17
18func main() {
19        fmt.Println(hello.Hello)
20}
21EOF
22
23$ go mod tidy
24go: finding module for package arch.seankhliao.com/hello.git
25go: downloading arch.seankhliao.com/hello.git v0.0.0-20210329200831-02adee55f661
26go: found arch.seankhliao.com/hello.git in arch.seankhliao.com/hello.git v0.0.0-20210329200831-02adee55f661
27
28$ go run .
29world