Skip to content

Commit a03c669

Browse files
committed
Allow custom GitLab server
1 parent 1e24f77 commit a03c669

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Configuration values can be supplied as environment variables, via a JSON config
7575
| `GBM_PROVIDER` | Git provider to use (`github` or `gitlab`). Defaults to `github`. |
7676
| `GBM_NAMESPACE` | Optional suffix added to the bookmarks repository name. |
7777
| `GBM_TITLE` | Overrides the page title shown in the browser. |
78+
| `GITLAB_SERVER` | Base URL for self-hosted GitLab, e.g. `https://gitlab.example.com` |
7879
| `GOBM_ENV_FILE` | Path to a file of `KEY=VALUE` pairs loaded before the environment. Defaults to `/etc/gobookmarks/gobookmarks.env`. |
7980
| `GOBM_CONFIG_FILE` | Path to the JSON config file. If unset the program uses `$XDG_CONFIG_HOME/gobookmarks/config.json` or `$HOME/.config/gobookmarks/config.json` for normal users and `/etc/gobookmarks/config.json` when run as root. |
8081

@@ -85,6 +86,7 @@ Use `--config <path>` or set `GOBM_CONFIG_FILE` to control which configuration f
8586

8687
The `--provider` command line flag or `GBM_PROVIDER` environment variable selects which git provider to use. By default the binary includes both GitHub and GitLab support. Use build tags `nogithub` or `nogitlab` to exclude either provider when building from source. If you specify an unknown provider the program will exit with an error listing the available options.
8788
The `--title` flag or `GBM_TITLE` environment variable sets the browser page title.
89+
Use `--gitlab-server` or `GITLAB_SERVER` to point the application at a self-hosted GitLab instance.
8890

8991
Running `gobookmarks --version` will print the version information along with the list of compiled-in providers.
9092
Use `--dump-config` to print the final configuration after merging the environment,

cmd/gobookmarks/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func main() {
5959
Namespace: os.Getenv("GBM_NAMESPACE"),
6060
Title: os.Getenv("GBM_TITLE"),
6161
Provider: os.Getenv("GBM_PROVIDER"),
62+
GitLabServer: os.Getenv("GITLAB_SERVER"),
6263
}
6364

6465
configPath := DefaultConfigPath()
@@ -70,6 +71,7 @@ func main() {
7071
var nsFlag stringFlag
7172
var titleFlag stringFlag
7273
var providerFlag stringFlag
74+
var gitlabServerFlag stringFlag
7375
var columnFlag boolFlag
7476
var versionFlag bool
7577
var dumpConfig bool
@@ -80,6 +82,7 @@ func main() {
8082
flag.Var(&nsFlag, "namespace", "repository namespace")
8183
flag.Var(&titleFlag, "title", "site title")
8284
flag.Var(&providerFlag, "provider", fmt.Sprintf("git provider (%s)", strings.Join(ProviderNames(), ", ")))
85+
flag.Var(&gitlabServerFlag, "gitlab-server", "GitLab server base URL")
8386
flag.Var(&columnFlag, "css-columns", "use CSS columns")
8487
flag.BoolVar(&versionFlag, "version", false, "show version")
8588
flag.BoolVar(&dumpConfig, "dump-config", false, "print merged config and exit")
@@ -118,6 +121,9 @@ func main() {
118121
if columnFlag.set {
119122
cfg.CssColumns = columnFlag.value
120123
}
124+
if gitlabServerFlag.set {
125+
cfg.GitLabServer = gitlabServerFlag.value
126+
}
121127
if providerFlag.set {
122128
cfg.Provider = providerFlag.value
123129
}
@@ -131,6 +137,9 @@ func main() {
131137
UseCssColumns = cfg.CssColumns
132138
Namespace = cfg.Namespace
133139
SiteTitle = cfg.Title
140+
if cfg.GitLabServer != "" {
141+
GitLabServer = cfg.GitLabServer
142+
}
134143
clientID = cfg.Oauth2ClientID
135144
clientSecret = cfg.Oauth2Secret
136145
externalUrl = cfg.ExternalURL

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
CssColumns bool `json:"css_columns"`
1919
Namespace string `json:"namespace"`
2020
Title string `json:"title"`
21+
GitLabServer string `json:"gitlab_server"`
2122
}
2223

2324
// LoadConfigFile loads configuration from the given path if it exists.
@@ -54,6 +55,9 @@ func MergeConfig(dst *Config, src Config) {
5455
if src.Title != "" {
5556
dst.Title = src.Title
5657
}
58+
if src.GitLabServer != "" {
59+
dst.GitLabServer = src.GitLabServer
60+
}
5761

5862
if src.Provider != "" {
5963
dst.Provider = src.Provider

packaging/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"external_url": "http://localhost:8080",
55
"css_columns": false,
66
"namespace": "",
7-
"title": ""
7+
"title": "",
8+
"gitlab_server": "https://gitlab.com"
89
}

provider_gitlab.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,45 @@ import (
66
"context"
77
"encoding/base64"
88
"fmt"
9+
"strings"
910

1011
gitlab "github.com/xanzy/go-gitlab"
1112
"golang.org/x/oauth2"
1213
)
1314

15+
// GitLabProvider implements Provider for GitLab.
16+
//
17+
// The GitLab server URL can be overridden using the GitLabServer variable
18+
// defined in settings.go.
1419
type GitLabProvider struct{}
1520

1621
func init() { RegisterProvider(GitLabProvider{}) }
1722

1823
func (GitLabProvider) Name() string { return "gitlab" }
1924

2025
func (GitLabProvider) OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config {
26+
server := strings.TrimRight(GitLabServer, "/")
27+
if server == "" {
28+
server = "https://gitlab.com"
29+
}
2130
return &oauth2.Config{
2231
ClientID: clientID,
2332
ClientSecret: clientSecret,
2433
RedirectURL: redirectURL,
2534
Scopes: []string{"api"},
2635
Endpoint: oauth2.Endpoint{
27-
AuthURL: "https://gitlab.com/oauth/authorize",
28-
TokenURL: "https://gitlab.com/oauth/token",
36+
AuthURL: server + "/oauth/authorize",
37+
TokenURL: server + "/oauth/token",
2938
},
3039
}
3140
}
3241

3342
func (GitLabProvider) client(token *oauth2.Token) (*gitlab.Client, error) {
34-
return gitlab.NewOAuthClient(token.AccessToken)
43+
server := GitLabServer
44+
if server == "" {
45+
server = "https://gitlab.com"
46+
}
47+
return gitlab.NewOAuthClient(token.AccessToken, gitlab.WithBaseURL(server))
3548
}
3649

3750
func (GitLabProvider) CurrentUser(ctx context.Context, token *oauth2.Token) (*User, error) {

settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ var (
44
UseCssColumns bool
55
Namespace string
66
SiteTitle string
7+
GitLabServer string = "https://gitlab.com"
78
)

0 commit comments

Comments
 (0)