mirror of
https://github.com/MedzikUser/go-github-selfupdate.git
synced 2024-08-15 03:25:29 +00:00
enable to have enterprise base URL and upload URL
This commit is contained in:
parent
996452a5ed
commit
d0de60fba1
5 changed files with 61 additions and 17 deletions
|
@ -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).
|
// DetectLatest detects the latest release of the slug (owner/repo).
|
||||||
func DetectLatest(slug string) (*Release, bool, error) {
|
func DetectLatest(slug string) (*Release, bool, error) {
|
||||||
return NewUpdater(Config{}).DetectLatest(slug)
|
return DefaultUpdater().DetectLatest(slug)
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,7 @@ func TestDetectNoRelease(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidSlug(t *testing.T) {
|
func TestInvalidSlug(t *testing.T) {
|
||||||
up := NewUpdater(Config{})
|
up := DefaultUpdater()
|
||||||
|
|
||||||
for _, slug := range []string{
|
for _, slug := range []string{
|
||||||
"foo",
|
"foo",
|
||||||
|
|
|
@ -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.
|
// this function is not available to update a release for private repositories.
|
||||||
// cmdPath is a file path to command executable.
|
// cmdPath is a file path to command executable.
|
||||||
func UpdateTo(assetURL, cmdPath string) error {
|
func UpdateTo(assetURL, cmdPath string) error {
|
||||||
up := NewUpdater(Config{})
|
up := DefaultUpdater()
|
||||||
src, err := up.downloadDirectlyFromURL(assetURL)
|
src, err := up.downloadDirectlyFromURL(assetURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -135,11 +135,11 @@ func UpdateTo(assetURL, cmdPath string) error {
|
||||||
// UpdateCommand updates a given command binary to the latest version.
|
// UpdateCommand updates a given command binary to the latest version.
|
||||||
// This function is a shortcut version of updater.UpdateCommand.
|
// This function is a shortcut version of updater.UpdateCommand.
|
||||||
func UpdateCommand(cmdPath string, current semver.Version, slug string) (*Release, error) {
|
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.
|
// UpdateSelf updates the running executable itself to the latest version.
|
||||||
// This function is a shortcut version of updater.UpdateSelf.
|
// This function is a shortcut version of updater.UpdateSelf.
|
||||||
func UpdateSelf(current semver.Version, slug string) (*Release, error) {
|
func UpdateSelf(current semver.Version, slug string) (*Release, error) {
|
||||||
return NewUpdater(Config{}).UpdateSelf(current, slug)
|
return DefaultUpdater().UpdateSelf(current, slug)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package selfupdate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -22,11 +23,25 @@ type Updater struct {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// APIToken represents GitHub API token. If it's not empty, it will be used for authentication of GitHub API
|
// APIToken represents GitHub API token. If it's not empty, it will be used for authentication of GitHub API
|
||||||
APIToken string
|
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 newHTTPClient(ctx context.Context, token string) *http.Client {
|
||||||
func NewUpdater(config Config) *Updater {
|
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
|
token := config.APIToken
|
||||||
if token == "" {
|
if token == "" {
|
||||||
token = os.Getenv("GITHUB_TOKEN")
|
token = os.Getenv("GITHUB_TOKEN")
|
||||||
|
@ -35,13 +50,37 @@ func NewUpdater(config Config) *Updater {
|
||||||
token, _ = gitconfig.GithubToken()
|
token, _ = gitconfig.GithubToken()
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
hc := newHTTPClient(ctx, token)
|
||||||
|
|
||||||
auth := http.DefaultClient
|
var client *github.Client
|
||||||
if token != "" {
|
if config.EnterpriseBaseURL == "" {
|
||||||
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
|
client = github.NewClient(hc)
|
||||||
auth = oauth2.NewClient(ctx, src)
|
} else {
|
||||||
|
if token == "" {
|
||||||
|
return nil, errors.New("GitHub API token cannot be empty when releases are hosted on GitHub Enterprise instance")
|
||||||
}
|
}
|
||||||
|
u := config.EnterpriseUploadURL
|
||||||
client := github.NewClient(auth)
|
if u == "" {
|
||||||
return &Updater{client, ctx, auth}
|
u = config.EnterpriseBaseURL
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
client, err = github.NewEnterpriseClient(config.EnterpriseBaseURL, u, hc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,11 @@ func TestGitHubTokenEnv(t *testing.T) {
|
||||||
if token == "" {
|
if token == "" {
|
||||||
t.Skip("because $GITHUB_TOKEN is not set")
|
t.Skip("because $GITHUB_TOKEN is not set")
|
||||||
}
|
}
|
||||||
_ = NewUpdater(Config{})
|
_ = DefaultUpdater()
|
||||||
_ = NewUpdater(Config{APIToken: token})
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue