enable to have enterprise base URL and upload URL

This commit is contained in:
rhysd 2018-01-19 17:47:21 +09:00
parent 996452a5ed
commit d0de60fba1
5 changed files with 61 additions and 17 deletions

View File

@ -107,5 +107,5 @@ func (up *Updater) DetectLatest(slug string) (release *Release, found bool, err
// DetectLatest detects the latest release of the slug (owner/repo).
func DetectLatest(slug string) (*Release, bool, error) {
return NewUpdater(Config{}).DetectLatest(slug)
return DefaultUpdater().DetectLatest(slug)
}

View File

@ -128,7 +128,7 @@ func TestDetectNoRelease(t *testing.T) {
}
func TestInvalidSlug(t *testing.T) {
up := NewUpdater(Config{})
up := DefaultUpdater()
for _, slug := range []string{
"foo",

View File

@ -124,7 +124,7 @@ func (up *Updater) UpdateSelf(current semver.Version, slug string) (*Release, er
// this function is not available to update a release for private repositories.
// cmdPath is a file path to command executable.
func UpdateTo(assetURL, cmdPath string) error {
up := NewUpdater(Config{})
up := DefaultUpdater()
src, err := up.downloadDirectlyFromURL(assetURL)
if err != nil {
return err
@ -135,11 +135,11 @@ func UpdateTo(assetURL, cmdPath string) error {
// UpdateCommand updates a given command binary to the latest version.
// This function is a shortcut version of updater.UpdateCommand.
func UpdateCommand(cmdPath string, current semver.Version, slug string) (*Release, error) {
return NewUpdater(Config{}).UpdateCommand(cmdPath, current, slug)
return DefaultUpdater().UpdateCommand(cmdPath, current, slug)
}
// UpdateSelf updates the running executable itself to the latest version.
// This function is a shortcut version of updater.UpdateSelf.
func UpdateSelf(current semver.Version, slug string) (*Release, error) {
return NewUpdater(Config{}).UpdateSelf(current, slug)
return DefaultUpdater().UpdateSelf(current, slug)
}

View File

@ -2,6 +2,7 @@ package selfupdate
import (
"context"
"errors"
"net/http"
"os"
@ -22,11 +23,25 @@ type Updater struct {
type Config struct {
// APIToken represents GitHub API token. If it's not empty, it will be used for authentication of GitHub API
APIToken string
// TODO: Add host URL for API endpoint
// EnterpriseBaseURL is a base URL of GitHub API. If you want to use this library with GitHub Enterprise,
// please set "https://{your-organization-address}/api/v3/" to this field.
EnterpriseBaseURL string
// EnterpriseUploadURL is a URL to upload stuffs to GitHub Enterprise instance. This is often the same as an API base URL.
// So if this field is not set and EnterpriseBaseURL is set, EnterpriseBaseURL is also set to this field.
EnterpriseUploadURL string
}
// NewUpdater crates a new detector instance. It initializes GitHub API client.
func NewUpdater(config Config) *Updater {
func newHTTPClient(ctx context.Context, token string) *http.Client {
if token == "" {
return http.DefaultClient
}
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
return oauth2.NewClient(ctx, src)
}
// NewUpdater creates a new updater instance. It initializes GitHub API client.
// If you set your API token to $GITHUB_TOKEN, the client will use it.
func NewUpdater(config Config) (*Updater, error) {
token := config.APIToken
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
@ -35,13 +50,37 @@ func NewUpdater(config Config) *Updater {
token, _ = gitconfig.GithubToken()
}
ctx := context.Background()
hc := newHTTPClient(ctx, token)
auth := http.DefaultClient
if token != "" {
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
auth = oauth2.NewClient(ctx, src)
var client *github.Client
if config.EnterpriseBaseURL == "" {
client = github.NewClient(hc)
} else {
if token == "" {
return nil, errors.New("GitHub API token cannot be empty when releases are hosted on GitHub Enterprise instance")
}
u := config.EnterpriseUploadURL
if u == "" {
u = config.EnterpriseBaseURL
}
var err error
client, err = github.NewEnterpriseClient(config.EnterpriseBaseURL, u, hc)
if err != nil {
return nil, err
}
}
client := github.NewClient(auth)
return &Updater{client, ctx, auth}
return &Updater{client, ctx, hc}, nil
}
// DefaultUpdater creates a new updater instance with default configuration.
// It initializes GitHub API client with default API base URL.
// If you set your API token to $GITHUB_TOKEN, the client will use it.
func DefaultUpdater() *Updater {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
token, _ = gitconfig.GithubToken()
}
ctx := context.Background()
client := newHTTPClient(ctx, token)
return &Updater{github.NewClient(client), ctx, client}
}

View File

@ -10,6 +10,11 @@ func TestGitHubTokenEnv(t *testing.T) {
if token == "" {
t.Skip("because $GITHUB_TOKEN is not set")
}
_ = NewUpdater(Config{})
_ = NewUpdater(Config{APIToken: token})
_ = DefaultUpdater()
if _, err := NewUpdater(Config{}); err != nil {
t.Error("Failed to initialize updater with empty config")
}
if _, err := NewUpdater(Config{APIToken: token}); err != nil {
t.Error("Failed to initialize updater with API token config")
}
}