forked from toddproject/todd
/
objects.go
77 lines (60 loc) · 1.62 KB
/
objects.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
/*
ToDD Client API Calls for "todd objects"
Copyright 2016 Matt Oswalt. Use or modification of this
source code is governed by the license provided here:
https://github.com/toddproject/todd/blob/master/LICENSE
*/
package api
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"text/tabwriter"
"github.com/toddproject/todd/server/objects"
)
// Objects will query ToDD for all objects, with the type requested in the sub-arguments, and then display a list of those
// objects to the user.
func (capi ClientAPI) Objects(conf map[string]string, objType string) error {
// If no subarg was provided, instruct the user to provide the object type
if objType == "" {
return errors.New("Please provide the object type")
}
url := fmt.Sprintf("http://%s:%s/v1/object/%s", conf["host"], conf["port"], objType)
// Build the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
// Send the request via a client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
// Defer the closing of the body
defer resp.Body.Close()
// Read the content into a byte array
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
parsedObjects := objects.ParseToddObjects(body)
w := new(tabwriter.Writer)
// Format in tab-separated columns with a tab stop of 8.
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintln(w, "LABEL\tTYPE\tSPEC\t")
for i := range parsedObjects {
fmt.Fprintf(
w,
"%s\t%s\t%s\n",
parsedObjects[i].GetLabel(),
parsedObjects[i].GetType(),
parsedObjects[i].GetSpec(),
)
}
fmt.Fprintln(w)
w.Flush()
return nil
}