Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand All @@ -86,7 +86,7 @@ Use `--config <path>` 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,
Expand Down
11 changes: 11 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand All @@ -67,5 +75,8 @@ func SetProviderByName(name string) bool {
func init() {
if p, ok := providers["github"]; ok {
ActiveProvider = p
if GitServer == "" {
GitServer = p.DefaultServer()
}
}
}
24 changes: 21 additions & 3 deletions provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions provider_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ var (
UseCssColumns bool
Namespace string
SiteTitle string
GitServer string = "https://gitlab.com"
GitServer string
)
Loading