-
Notifications
You must be signed in to change notification settings - Fork 42
/
util.go
95 lines (76 loc) · 1.7 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package util
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/vorteil/direktiv/pkg/api"
)
// DirektivURL stroes the endpoint for direktiv
var DirektivURL string
// Content types for requests
const (
JSONCt = "application/json"
YAMLCt = "text/yaml"
NONECt = ""
)
// GenerateCmd is a basic cobra function
func GenerateCmd(use, short, long string, fn func(cmd *cobra.Command, args []string), c cobra.PositionalArgs) *cobra.Command {
return &cobra.Command{
Use: use,
Short: short,
Long: long,
Run: fn,
Args: c,
}
}
// DoRequest executes request against ingress
func DoRequest(method, path, ct string, body *string) ([]byte, error) {
var out []byte
d := time.Now().Add(10 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
var rd io.Reader
if body != nil {
rd = strings.NewReader(*body)
}
req, err := http.NewRequestWithContext(ctx, method,
fmt.Sprintf("%s/api%s", DirektivURL, path), rd)
if err != nil {
return out, err
}
if len(ct) > 0 {
req.Header.Set("Content-type", ct)
}
c := &http.Client{}
res, err := c.Do(req)
if err != nil {
return out, err
}
defer res.Body.Close()
out, err = ioutil.ReadAll(res.Body)
if err != nil {
return out, err
}
if res.StatusCode != 200 {
var eo api.ErrObject
err := json.Unmarshal(out, &eo)
if err != nil {
log.Fatalf("can not parse error response: %v", err)
}
return out, fmt.Errorf(eo.Message)
}
return out, nil
}
// CLILogWriter for using fatalf without date
type CLILogWriter struct {
}
func (writer CLILogWriter) Write(bytes []byte) (int, error) {
return fmt.Print(string(bytes))
}