mirror of
https://github.com/thegeeklab/drone-admin.git
synced 2024-06-03 03:49:40 +02:00
49 lines
904 B
Go
49 lines
904 B
Go
|
package client
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"crypto/tls"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"time"
|
||
|
|
||
|
"github.com/drone/drone-go/drone"
|
||
|
"github.com/jackspirou/syscerts"
|
||
|
"golang.org/x/oauth2"
|
||
|
)
|
||
|
|
||
|
func New(server string, token string) (drone.Client, error) {
|
||
|
s, err := url.Parse(server)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if len(s.Scheme) == 0 {
|
||
|
s.Scheme = "http"
|
||
|
}
|
||
|
|
||
|
// attempt to find system CA certs
|
||
|
certs := syscerts.SystemRootsPool()
|
||
|
tlsConfig := &tls.Config{
|
||
|
RootCAs: certs,
|
||
|
InsecureSkipVerify: false,
|
||
|
}
|
||
|
|
||
|
oauth := new(oauth2.Config)
|
||
|
auther := oauth.Client(
|
||
|
context.Background(),
|
||
|
&oauth2.Token{
|
||
|
AccessToken: token,
|
||
|
},
|
||
|
)
|
||
|
|
||
|
auther.Timeout, _ = time.ParseDuration("60s")
|
||
|
|
||
|
trans, _ := auther.Transport.(*oauth2.Transport)
|
||
|
trans.Base = &http.Transport{
|
||
|
TLSClientConfig: tlsConfig,
|
||
|
Proxy: http.ProxyFromEnvironment,
|
||
|
}
|
||
|
|
||
|
return drone.NewClient(s.String(), auther), nil
|
||
|
}
|