Skip to content

Commit 2c77719

Browse files
committed
modules: Add GOAUTH to module config
Closes #13385
1 parent d89b9d8 commit 2c77719

File tree

4 files changed

+83
-19
lines changed

4 files changed

+83
-19
lines changed

docs/content/en/hugo-modules/configuration.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ proxy = 'direct'
2222
replacements = ''
2323
vendorClosest = false
2424
workspace = 'off'
25+
auth = ''
2526
{{< /code-toggle >}}
2627

2728
noProxy
@@ -36,6 +37,9 @@ private
3637
proxy
3738
: (`string`) Defines the proxy server to use to download remote modules. Default is `direct`, which means "git clone" and similar.
3839

40+
auth
41+
: (`string`) {{< new-in 0.144.0 >}} Configures `GOAUTH` when running the Go command for module operations. This is a semicolon-separated list of authentication commands for go-import and HTTPS module mirror interactions. This is useful for private repositories. See `go help goauth` for more information.
42+
3943
vendorClosest
4044
: (`bool`) When enabled, we will pick the vendored module closest to the module using it. The default behavior is to pick the first. Note that there can still be only one dependency of a given module path, so once it is in use it cannot be redefined. Default is `false`.
4145

modules/client.go

+34-19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/gohugoio/hugo/common/herrors"
3333
"github.com/gohugoio/hugo/common/hexec"
3434
"github.com/gohugoio/hugo/common/loggers"
35+
"github.com/gohugoio/hugo/config"
3536

3637
hglob "github.com/gohugoio/hugo/hugofs/glob"
3738

@@ -41,8 +42,6 @@ import (
4142

4243
"github.com/gohugoio/hugo/hugofs/files"
4344

44-
"github.com/gohugoio/hugo/config"
45-
4645
"golang.org/x/mod/module"
4746

4847
"github.com/gohugoio/hugo/common/hugio"
@@ -79,21 +78,6 @@ func NewClient(cfg ClientConfig) *Client {
7978
goModFilename = n
8079
}
8180

82-
var env []string
83-
mcfg := cfg.ModuleConfig
84-
85-
config.SetEnvVars(&env,
86-
"PWD", cfg.WorkingDir,
87-
"GO111MODULE", "on",
88-
"GOPROXY", mcfg.Proxy,
89-
"GOPRIVATE", mcfg.Private,
90-
"GONOPROXY", mcfg.NoProxy,
91-
"GOPATH", cfg.CacheDir,
92-
"GOWORK", mcfg.Workspace, // Requires Go 1.18, see https://tip.golang.org/doc/go1.18
93-
// GOCACHE was introduced in Go 1.15. This matches the location derived from GOPATH above.
94-
"GOCACHE", filepath.Join(cfg.CacheDir, "pkg", "mod"),
95-
)
96-
9781
logger := cfg.Logger
9882
if logger == nil {
9983
logger = loggers.NewDefault()
@@ -109,8 +93,8 @@ func NewClient(cfg ClientConfig) *Client {
10993
ccfg: cfg,
11094
logger: logger,
11195
noVendor: noVendor,
112-
moduleConfig: mcfg,
113-
environ: env,
96+
moduleConfig: cfg.ModuleConfig,
97+
environ: cfg.toEnv(),
11498
GoModulesFilename: goModFilename,
11599
}
116100
}
@@ -785,6 +769,37 @@ func (c ClientConfig) shouldIgnoreVendor(path string) bool {
785769
return c.IgnoreVendor != nil && c.IgnoreVendor.Match(path)
786770
}
787771

772+
func (cfg ClientConfig) toEnv() []string {
773+
mcfg := cfg.ModuleConfig
774+
var env []string
775+
keyVals := []string{
776+
"PWD", cfg.WorkingDir,
777+
"GO111MODULE", "on",
778+
"GOPATH", cfg.CacheDir,
779+
"GOWORK", mcfg.Workspace, // Requires Go 1.18, see https://tip.golang.org/doc/go1.18
780+
// GOCACHE was introduced in Go 1.15. This matches the location derived from GOPATH above.
781+
"GOCACHE", filepath.Join(cfg.CacheDir, "pkg", "mod"),
782+
}
783+
784+
if mcfg.Proxy != "" {
785+
keyVals = append(keyVals, "GOPROXY", mcfg.Proxy)
786+
}
787+
if mcfg.Private != "" {
788+
keyVals = append(keyVals, "GOPRIVATE", mcfg.Private)
789+
}
790+
if mcfg.NoProxy != "" {
791+
keyVals = append(keyVals, "GONOPROXY", mcfg.NoProxy)
792+
}
793+
if mcfg.Auth != "" {
794+
// GOAUTH was introduced in Go 1.24, see https://tip.golang.org/doc/go1.24.
795+
keyVals = append(keyVals, "GOAUTH", mcfg.Auth)
796+
}
797+
798+
config.SetEnvVars(&env, keyVals...)
799+
800+
return env
801+
}
802+
788803
type goBinaryStatus int
789804

790805
type goModule struct {

modules/client_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,42 @@ func TestGetModlineSplitter(t *testing.T) {
216216
gosumSplitter := getModlineSplitter(false)
217217
c.Assert(gosumSplitter("github.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
218218
}
219+
220+
func TestClientConfigToEnv(t *testing.T) {
221+
c := qt.New(t)
222+
223+
ccfg := ClientConfig{
224+
WorkingDir: "/mywork",
225+
CacheDir: "/mycache",
226+
}
227+
228+
env := ccfg.toEnv()
229+
230+
c.Assert(env, qt.DeepEquals, []string{"PWD=/mywork", "GO111MODULE=on", "GOPATH=/mycache", "GOWORK=", filepath.FromSlash("GOCACHE=/mycache/pkg/mod")})
231+
232+
ccfg = ClientConfig{
233+
WorkingDir: "/mywork",
234+
CacheDir: "/mycache",
235+
ModuleConfig: Config{
236+
Proxy: "https://proxy.example.org",
237+
Private: "myprivate",
238+
NoProxy: "mynoproxy",
239+
Workspace: "myworkspace",
240+
Auth: "myauth",
241+
},
242+
}
243+
244+
env = ccfg.toEnv()
245+
246+
c.Assert(env, qt.DeepEquals, []string{
247+
"PWD=/mywork",
248+
"GO111MODULE=on",
249+
"GOPATH=/mycache",
250+
"GOWORK=myworkspace",
251+
filepath.FromSlash("GOCACHE=/mycache/pkg/mod"),
252+
"GOPROXY=https://proxy.example.org",
253+
"GOPRIVATE=myprivate",
254+
"GONOPROXY=mynoproxy",
255+
"GOAUTH=myauth",
256+
})
257+
}

modules/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ type Config struct {
295295
// Configures GOPRIVATE when running the Go command for module operations.
296296
Private string
297297

298+
// Configures GOAUTH when running the Go command for module operations.
299+
// This is a semicolon-separated list of authentication commands for go-import and HTTPS module mirror interactions.
300+
// This is useful for private repositories.
301+
// See `go help goauth` for more information.
302+
Auth string
303+
298304
// Defaults to "off".
299305
// Set to a work file, e.g. hugo.work, to enable Go "Workspace" mode.
300306
// Can be relative to the working directory or absolute.

0 commit comments

Comments
 (0)