mirror of https://github.com/helm/helm
commit
ca43497cd3
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/deis/helm-dm/dm"
|
||||
"github.com/deis/helm-dm/format"
|
||||
)
|
||||
|
||||
func listCmd() cli.Command {
|
||||
return cli.Command{
|
||||
Name: "list",
|
||||
Usage: "Lists the deployments in the cluster",
|
||||
Action: func(c *cli.Context) {
|
||||
if err := list(c.GlobalString("host")); err != nil {
|
||||
format.Error("%s (Is the cluster running?)", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func list(host string) error {
|
||||
client := dm.NewClient(host)
|
||||
client.Protocol = "http"
|
||||
return client.ListDeployments()
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package dm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type debugTransport struct {
|
||||
// Writer is the logging destination
|
||||
Writer io.Writer
|
||||
|
||||
http.RoundTripper
|
||||
}
|
||||
|
||||
func NewDebugTransport(rt http.RoundTripper) http.RoundTripper {
|
||||
return debugTransport{
|
||||
RoundTripper: rt,
|
||||
Writer: os.Stderr,
|
||||
}
|
||||
}
|
||||
|
||||
func (tr debugTransport) CancelRequest(req *http.Request) {
|
||||
type canceler interface {
|
||||
CancelRequest(*http.Request)
|
||||
}
|
||||
if cr, ok := tr.transport().(canceler); ok {
|
||||
cr.CancelRequest(req)
|
||||
}
|
||||
}
|
||||
|
||||
func (tr debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
tr.logRequest(req)
|
||||
resp, err := tr.transport().RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tr.logResponse(resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (tr debugTransport) transport() http.RoundTripper {
|
||||
if tr.RoundTripper != nil {
|
||||
return tr.RoundTripper
|
||||
}
|
||||
return http.DefaultTransport
|
||||
}
|
||||
|
||||
func (tr debugTransport) logRequest(req *http.Request) {
|
||||
dump, err := httputil.DumpRequestOut(req, true)
|
||||
if err != nil {
|
||||
fmt.Fprintf(tr.Writer, "%s: %s\n", "could not dump request", err)
|
||||
}
|
||||
fmt.Fprint(tr.Writer, string(dump))
|
||||
}
|
||||
|
||||
func (tr debugTransport) logResponse(resp *http.Response) {
|
||||
dump, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
fmt.Fprintf(tr.Writer, "%s: %s\n", "could not dump response", err)
|
||||
}
|
||||
fmt.Fprint(tr.Writer, string(dump))
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package dm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDebugTransport(t *testing.T) {
|
||||
handler := func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status":"awesome"}`))
|
||||
}
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(handler))
|
||||
defer server.Close()
|
||||
|
||||
var output bytes.Buffer
|
||||
|
||||
client := &http.Client{
|
||||
Transport: debugTransport{
|
||||
Writer: &output,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
expected := []string{
|
||||
"GET / HTTP/1.1",
|
||||
"Accept-Encoding: gzip",
|
||||
"HTTP/1.1 200 OK",
|
||||
"Content-Length: 20",
|
||||
"Content-Type: application/json",
|
||||
`{"status":"awesome"}`,
|
||||
}
|
||||
actual := output.String()
|
||||
|
||||
for _, match := range expected {
|
||||
if !strings.Contains(actual, match) {
|
||||
t.Errorf("Expected %s to contain %s", actual, match)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue