2017-04-29 17:03:45 +00:00
|
|
|
package internal
|
2016-04-23 11:27:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/drone/drone/client"
|
|
|
|
|
|
|
|
"github.com/jackspirou/syscerts"
|
2017-03-16 10:14:02 +00:00
|
|
|
"github.com/urfave/cli"
|
2016-04-23 11:27:28 +00:00
|
|
|
)
|
|
|
|
|
2017-04-29 17:03:45 +00:00
|
|
|
// NewClient returns a new client from the CLI context.
|
|
|
|
func NewClient(c *cli.Context) (client.Client, error) {
|
2016-04-23 11:27:28 +00:00
|
|
|
var token = c.GlobalString("token")
|
2016-11-12 19:13:52 +00:00
|
|
|
var server = strings.TrimRight(c.GlobalString("server"), "/")
|
2016-04-23 11:27:28 +00:00
|
|
|
|
|
|
|
// if no server url is provided we can default
|
|
|
|
// to the hosted Drone service.
|
|
|
|
if len(server) == 0 {
|
|
|
|
return nil, fmt.Errorf("Error: you must provide the Drone server address.")
|
|
|
|
}
|
|
|
|
if len(token) == 0 {
|
|
|
|
return nil, fmt.Errorf("Error: you must provide your Drone access token.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to find system CA certs
|
|
|
|
certs := syscerts.SystemRootsPool()
|
|
|
|
tlsConfig := &tls.Config{RootCAs: certs}
|
|
|
|
|
|
|
|
// create the drone client with TLS options
|
2017-01-18 15:26:46 +00:00
|
|
|
return client.NewClientTokenTLS(server, token, tlsConfig)
|
2016-04-23 11:27:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 17:03:45 +00:00
|
|
|
// ParseRepo parses the repository owner and name from a string.
|
|
|
|
func ParseRepo(str string) (user, repo string, err error) {
|
2016-04-23 11:27:28 +00:00
|
|
|
var parts = strings.Split(str, "/")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
err = fmt.Errorf("Error: Invalid or missing repository. eg octocat/hello-world.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user = parts[0]
|
|
|
|
repo = parts[1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func readInput(in string) ([]byte, error) {
|
|
|
|
if in == "-" {
|
|
|
|
return ioutil.ReadAll(os.Stdin)
|
|
|
|
}
|
|
|
|
return ioutil.ReadFile(in)
|
|
|
|
}
|
2016-06-27 21:06:02 +00:00
|
|
|
|
|
|
|
func stringInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2016-07-27 21:11:54 +00:00
|
|
|
|
2017-04-29 17:03:45 +00:00
|
|
|
// ParseKeyPair parses a key=value pair.
|
|
|
|
func ParseKeyPair(p []string) map[string]string {
|
2016-07-27 21:11:54 +00:00
|
|
|
params := map[string]string{}
|
|
|
|
for _, i := range p {
|
|
|
|
parts := strings.Split(i, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
params[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|