cli/pkg/cmd/repo/deploy-key/list/http.go
Nilesh Singh 47a6aff54a
Add repo deploy key commands (#4302)
Co-authored-by: Mislav Marohnić <mislav@github.com>
2022-01-25 17:48:24 +00:00

53 lines
1 KiB
Go

package list
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghinstance"
"github.com/cli/cli/v2/internal/ghrepo"
)
type deployKey struct {
ID int
Key string
Title string
CreatedAt time.Time `json:"created_at"`
ReadOnly bool `json:"read_only"`
}
func repoKeys(httpClient *http.Client, repo ghrepo.Interface) ([]deployKey, error) {
path := fmt.Sprintf("repos/%s/%s/keys?per_page=100", repo.RepoOwner(), repo.RepoName())
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
return nil, api.HandleHTTPError(resp)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var keys []deployKey
err = json.Unmarshal(b, &keys)
if err != nil {
return nil, err
}
return keys, nil
}