Remove unused retries package (#326)

## Changes

The retries package used elsewhere in this repository originates from
https://github.com/databricks/databricks-sdk-go.

## Tests

No impact.
This commit is contained in:
Pieter Noordhuis 2023-04-12 13:51:33 +02:00 committed by GitHub
parent 42cd405eba
commit a390271cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 0 additions and 197 deletions

View File

@ -1,107 +0,0 @@
package github
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/atotto/clipboard"
"github.com/databricks/bricks/retries"
"github.com/pkg/browser"
)
// Bricks CLI GitHub OAuth App Client ID
const githubOauthClientID = "b91230382436c4592741"
func GithubGetPAT(ctx context.Context) (string, error) {
deviceRequest := url.Values{}
deviceRequest.Set("client_id", githubOauthClientID)
// TODO: scope
response, err := http.PostForm("https://github.com/login/device/code", deviceRequest)
if err != nil {
return "", err
}
raw, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
deviceResponse, err := url.ParseQuery(string(raw))
if err != nil {
return "", err
}
// TODO: give instructions to user and wait for the prompt
userCode := deviceResponse.Get("user_code")
err = clipboard.WriteAll(userCode)
if err != nil {
return "", fmt.Errorf("cannot copy to clipboard: %w", err)
}
verificationURL := deviceResponse.Get("verification_uri")
fmt.Printf("\nEnter the following code on %s: \n\n%s\n\n(it should be in your clipboard)", verificationURL, userCode)
err = browser.OpenURL(verificationURL)
if err != nil {
return "", fmt.Errorf("cannot open browser: %w", err)
}
var bearer string
err = retries.Wait(ctx, 15*time.Minute, func() *retries.Err {
form := url.Values{}
form.Set("client_id", githubOauthClientID)
form.Set("device_code", deviceResponse.Get("device_code"))
form.Set("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
response, err := http.PostForm("https://github.com/login/oauth/access_token", form)
if err != nil {
return retries.Halt(err)
}
raw, err := io.ReadAll(response.Body)
if err != nil {
return retries.Continuef("failed to read body: %w", err)
}
result, err := url.ParseQuery(string(raw))
if err != nil {
return retries.Continuef("failed to parse body: %w", err)
}
bearer = result.Get("access_token")
if bearer != "" {
return nil
}
if result.Get("error") == "slow_down" {
t, _ := strconv.Atoi(result.Get("interval"))
time.Sleep(time.Duration(t) * time.Second)
log.Printf("[WARN] Rate limited, sleeping for %d seconds", t)
}
reason := result.Get("error_description")
if reason == "" {
reason = "access token is not ready"
}
return retries.Continues(reason)
})
if err != nil {
return "", fmt.Errorf("failed to acquire access token: %w", err)
}
raw, err = json.Marshal(struct {
Note string `json:"note"`
Scopes []string `json:"scopes"`
}{"test token", []string{}})
if err != nil {
return "", err
}
request, err := http.NewRequest("POST", "https://api.github.com/api/v3/authorizations", bytes.NewReader(raw))
if err != nil {
return "", err
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearer))
response, err = http.DefaultClient.Do(request)
if err != nil {
return "", err
}
raw, _ = io.ReadAll(response.Body)
log.Printf("[INFO] %s", raw)
// TODO: convert to PAT
return bearer, nil
}

View File

@ -1,11 +0,0 @@
package github
import (
"testing"
)
func TestGithubGetPAT(t *testing.T) {
// pat, err := GithubGetPAT(context.Background())
// assert.NoError(t, err)
// assert.NotEqual(t, "..", pat)
}

1
go.mod
View File

@ -3,7 +3,6 @@ module github.com/databricks/bricks
go 1.18
require (
github.com/atotto/clipboard v0.1.4
github.com/databricks/databricks-sdk-go v0.7.0
github.com/ghodss/yaml v1.0.0 // MIT + NOTICE
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // BSD-2-Clause

2
go.sum
View File

@ -9,8 +9,6 @@ github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwF
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=

View File

@ -1,76 +0,0 @@
package retries
import (
"context"
"fmt"
"math/rand"
"strings"
"time"
"github.com/databricks/bricks/libs/log"
)
type Err struct {
Err error
Halt bool
}
func Halt(err error) *Err {
return &Err{err, true}
}
func Continue(err error) *Err {
return &Err{err, false}
}
func Continues(msg string) *Err {
return Continue(fmt.Errorf(msg))
}
func Continuef(format string, err error, args ...interface{}) *Err {
wrapped := fmt.Errorf(format, append([]interface{}{err}, args...))
return Continue(wrapped)
}
type WaitFn func() *Err
var maxWait = 10 * time.Second
var minJitter = 50 * time.Millisecond
var maxJitter = 750 * time.Millisecond
func Wait(pctx context.Context, timeout time.Duration, fn WaitFn) error {
ctx, cancel := context.WithTimeout(pctx, timeout)
defer cancel()
var attempt int
var lastErr error
for {
attempt++
res := fn()
if res == nil {
return nil
}
if res.Halt {
return res.Err
}
lastErr = res.Err
wait := time.Duration(attempt) * time.Second
if wait > maxWait {
wait = maxWait
}
// add some random jitter
rand.Seed(time.Now().UnixNano())
jitter := rand.Intn(int(maxJitter)-int(minJitter)+1) + int(minJitter)
wait += time.Duration(jitter)
timer := time.NewTimer(wait)
log.Tracef(ctx, "%s. Sleeping %s",
strings.TrimSuffix(res.Err.Error(), "."),
wait.Round(time.Millisecond))
select {
// stop when either this or parent context times out
case <-ctx.Done():
timer.Stop()
return fmt.Errorf("timed out: %w", lastErr)
case <-timer.C:
}
}
}