diff --git a/README.md b/README.md index 35e946e..e2de62b 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Configuration values can be supplied as environment variables, via a JSON config | `GBM_PROVIDER` | Git provider to use (`github` or `gitlab`). Defaults to `github`. | | `GBM_NAMESPACE` | Optional suffix added to the bookmarks repository name. | | `GBM_TITLE` | Overrides the page title shown in the browser. | -| `GIT_SERVER` | Base URL for a self-hosted git provider, e.g. `https://gitlab.example.com` | +| `GIT_SERVER` | Base URL for a self-hosted git provider, e.g. `https://gitlab.example.com`. Defaults to the public server for the selected provider. | | `GOBM_ENV_FILE` | Path to a file of `KEY=VALUE` pairs loaded before the environment. Defaults to `/etc/gobookmarks/gobookmarks.env`. | | `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. | @@ -86,7 +86,7 @@ Use `--config ` or set `GOBM_CONFIG_FILE` to control which configuration f 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. The `--title` flag or `GBM_TITLE` environment variable sets the browser page title. -Use `--git-server` or `GIT_SERVER` to point the application at a self-hosted git provider. +Use `--git-server` or `GIT_SERVER` to override the base URL of the git provider (defaults to the provider's public server). Running `gobookmarks --version` will print the version information along with the list of compiled-in providers. Use `--dump-config` to print the final configuration after merging the environment, diff --git a/provider.go b/provider.go index 711f58f..3d0058b 100644 --- a/provider.go +++ b/provider.go @@ -36,6 +36,7 @@ type Provider interface { GetBookmarks(ctx context.Context, user, ref string, token *oauth2.Token) (string, string, error) UpdateBookmarks(ctx context.Context, user string, token *oauth2.Token, sourceRef, branch, text, expectSHA string) error CreateBookmarks(ctx context.Context, user string, token *oauth2.Token, branch, text string) error + DefaultServer() string } var providers = map[string]Provider{} @@ -58,7 +59,14 @@ var ActiveProvider Provider // It returns true if the provider exists. func SetProviderByName(name string) bool { if p, ok := providers[name]; ok { + prevDefault := "" + if ActiveProvider != nil { + prevDefault = ActiveProvider.DefaultServer() + } ActiveProvider = p + if GitServer == "" || GitServer == prevDefault { + GitServer = p.DefaultServer() + } return true } return false @@ -67,5 +75,8 @@ func SetProviderByName(name string) bool { func init() { if p, ok := providers["github"]; ok { ActiveProvider = p + if GitServer == "" { + GitServer = p.DefaultServer() + } } } diff --git a/provider_github.go b/provider_github.go index 41d58f9..ccb91e9 100644 --- a/provider_github.go +++ b/provider_github.go @@ -6,10 +6,10 @@ import ( "context" "encoding/base64" "fmt" + "strings" "github.com/google/go-github/v55/github" "golang.org/x/oauth2" - "golang.org/x/oauth2/endpoints" ) // GitHubProvider implements Provider for GitHub. @@ -19,18 +19,36 @@ func init() { RegisterProvider(GitHubProvider{}) } func (GitHubProvider) Name() string { return "github" } +func (GitHubProvider) DefaultServer() string { return "https://github.com" } + func (GitHubProvider) OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config { + server := strings.TrimRight(GitServer, "/") + if server == "" { + server = "https://github.com" + } return &oauth2.Config{ ClientID: clientID, ClientSecret: clientSecret, RedirectURL: redirectURL, Scopes: []string{"repo", "read:user", "user:email"}, - Endpoint: endpoints.GitHub, + Endpoint: oauth2.Endpoint{ + AuthURL: server + "/login/oauth/authorize", + TokenURL: server + "/login/oauth/access_token", + }, } } func (GitHubProvider) client(ctx context.Context, token *oauth2.Token) *github.Client { - return github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(token))) + httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(token)) + server := strings.TrimRight(GitServer, "/") + if server == "" || server == "https://github.com" { + return github.NewClient(httpClient) + } + c, err := github.NewEnterpriseClient(server+"/api/v3/", server+"/upload/v3/", httpClient) + if err != nil { + return github.NewClient(httpClient) + } + return c } func (p GitHubProvider) CurrentUser(ctx context.Context, token *oauth2.Token) (*User, error) { diff --git a/provider_gitlab.go b/provider_gitlab.go index e84f23f..ff5417c 100644 --- a/provider_gitlab.go +++ b/provider_gitlab.go @@ -22,6 +22,8 @@ func init() { RegisterProvider(GitLabProvider{}) } func (GitLabProvider) Name() string { return "gitlab" } +func (GitLabProvider) DefaultServer() string { return "https://gitlab.com" } + func (GitLabProvider) OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config { server := strings.TrimRight(GitServer, "/") if server == "" { diff --git a/settings.go b/settings.go index dd6f0e1..eeb4bad 100644 --- a/settings.go +++ b/settings.go @@ -4,5 +4,5 @@ var ( UseCssColumns bool Namespace string SiteTitle string - GitServer string = "https://gitlab.com" + GitServer string )