mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
018e10ea66
``` $ fleetctl config set address https://localhost:8080 [+] Set the "address" config key to "https://localhost:8080" in the "default" context $ fleetctl config set ignore_tls true [+] Set the "ignore_tls" config key to "true" in the "default" context $ fleetctl setup --email mike@arpaia.co --password "abc123" [+] Fleet setup successful and context configured! $ cat ~/.fleet/config contexts: default: address: https://localhost:8080 email: mike@arpaia.co ignore_tls: true token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6IlUvdm05Vk9wSG0xUlA4SUtjQnBhb2ovWlo1TXppSEVXcFRCNFNPb2tHQnNLUFpDQXFieVpWWnpJb0UvczQzcWkyd1pHZXJOa29SNFVIQ2hNZUc0K09RPT0ifQ.rHawSN8JvD4jjWAPTYX2Ep9ZpMt3u4mSIQcu920C-_s $ fleetctl logout [+] Fleet logout successful and local token cleared! $ cat ~/.fleet/config contexts: default: address: https://localhost:8080 email: mike@arpaia.co ignore_tls: true token: "" ```
98 lines
2.0 KiB
Go
98 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Client struct {
|
|
addr string
|
|
baseURL *url.URL
|
|
token string
|
|
http *http.Client
|
|
}
|
|
|
|
func NewClient(addr string, insecureSkipVerify bool) (*Client, error) {
|
|
if !strings.HasPrefix(addr, "https://") {
|
|
return nil, errors.New("Addrress must start with https://")
|
|
}
|
|
|
|
baseURL, err := url.Parse(addr)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error parsing URL")
|
|
}
|
|
|
|
httpClient := &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
|
|
},
|
|
}
|
|
|
|
return &Client{
|
|
addr: addr,
|
|
baseURL: baseURL,
|
|
http: httpClient,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) doWithHeaders(verb, path string, params interface{}, headers map[string]string) (*http.Response, error) {
|
|
b, err := json.Marshal(params)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error marshaling json")
|
|
}
|
|
|
|
request, err := http.NewRequest(
|
|
verb,
|
|
c.url(path).String(),
|
|
bytes.NewBuffer(b),
|
|
)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error creating request object")
|
|
}
|
|
for k, v := range headers {
|
|
request.Header.Set(k, v)
|
|
}
|
|
|
|
return c.http.Do(request)
|
|
}
|
|
|
|
func (c *Client) Do(verb, path string, params interface{}) (*http.Response, error) {
|
|
headers := map[string]string{
|
|
"Content-type": "application/json",
|
|
"Accept": "application/json",
|
|
}
|
|
|
|
return c.doWithHeaders(verb, path, params, headers)
|
|
}
|
|
|
|
func (c *Client) AuthenticatedDo(verb, path string, params interface{}) (*http.Response, error) {
|
|
if c.token == "" {
|
|
return nil, errors.New("authentication token is empty")
|
|
}
|
|
|
|
headers := map[string]string{
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
"Authorization": fmt.Sprintf("Bearer %s", c.token),
|
|
}
|
|
|
|
return c.doWithHeaders(verb, path, params, headers)
|
|
}
|
|
|
|
func (c *Client) SetToken(t string) {
|
|
c.token = t
|
|
}
|
|
|
|
func (c *Client) url(path string) *url.URL {
|
|
u := *c.baseURL
|
|
u.Path = path
|
|
return &u
|
|
}
|