2019-04-06 21:32:14 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-10-28 09:14:16 +02:00
|
|
|
"crypto/x509"
|
2019-04-06 21:32:14 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-28 09:14:16 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-27 21:03:14 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-04-06 21:32:14 +02:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2021-10-03 00:27:43 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
2019-04-06 21:32:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient returns a new client from the CLI context.
|
2021-10-03 00:27:43 +02:00
|
|
|
func NewClient(c *cli.Context) (woodpecker.Client, error) {
|
2019-04-06 21:32:14 +02:00
|
|
|
var (
|
2021-10-27 21:03:14 +02:00
|
|
|
skip = c.Bool("skip-verify")
|
|
|
|
socks = c.String("socks-proxy")
|
|
|
|
socksoff = c.Bool("socks-proxy-off")
|
|
|
|
token = c.String("token")
|
|
|
|
server = c.String("server")
|
2019-04-06 21:32:14 +02:00
|
|
|
)
|
|
|
|
server = strings.TrimRight(server, "/")
|
|
|
|
|
|
|
|
// if no server url is provided we can default
|
2021-10-03 00:27:43 +02:00
|
|
|
// to the hosted Woodpecker service.
|
2019-04-06 21:32:14 +02:00
|
|
|
if len(server) == 0 {
|
2021-12-01 15:22:06 +02:00
|
|
|
return nil, fmt.Errorf("Error: you must provide the Woodpecker server address")
|
2019-04-06 21:32:14 +02:00
|
|
|
}
|
|
|
|
if len(token) == 0 {
|
2021-12-01 15:22:06 +02:00
|
|
|
return nil, fmt.Errorf("Error: you must provide your Woodpecker access token")
|
2019-04-06 21:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to find system CA certs
|
2021-10-28 09:14:16 +02:00
|
|
|
certs, err := x509.SystemCertPool()
|
2022-04-05 14:37:02 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msgf("failed to find system CA certs: %v", err)
|
|
|
|
}
|
2019-04-06 21:32:14 +02:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
RootCAs: certs,
|
|
|
|
InsecureSkipVerify: skip,
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(oauth2.Config)
|
2021-10-08 18:35:56 +02:00
|
|
|
client := config.Client(
|
2021-10-28 09:14:16 +02:00
|
|
|
c.Context,
|
2019-04-06 21:32:14 +02:00
|
|
|
&oauth2.Token{
|
|
|
|
AccessToken: token,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-10-08 18:35:56 +02:00
|
|
|
trans, _ := client.Transport.(*oauth2.Transport)
|
2019-04-06 21:32:14 +02:00
|
|
|
|
|
|
|
if len(socks) != 0 && !socksoff {
|
|
|
|
dialer, err := proxy.SOCKS5("tcp", socks, nil, proxy.Direct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trans.Base = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: dialer.Dial,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trans.Base = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:35:56 +02:00
|
|
|
return woodpecker.NewClient(server, client), nil
|
2019-04-06 21:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRepo parses the repository owner and name from a string.
|
|
|
|
func ParseRepo(str string) (user, repo string, err error) {
|
2022-01-05 22:50:23 +02:00
|
|
|
parts := strings.Split(str, "/")
|
2019-04-06 21:32:14 +02:00
|
|
|
if len(parts) != 2 {
|
2021-12-01 15:22:06 +02:00
|
|
|
err = fmt.Errorf("Error: Invalid or missing repository. eg octocat/hello-world")
|
2019-04-06 21:32:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user = parts[0]
|
|
|
|
repo = parts[1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseKeyPair parses a key=value pair.
|
|
|
|
func ParseKeyPair(p []string) map[string]string {
|
|
|
|
params := map[string]string{}
|
|
|
|
for _, i := range p {
|
|
|
|
parts := strings.SplitN(i, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
params[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|