Skip to content

Commit fa71f85

Browse files
authored
Merge pull request #21 from timperrett/blueprints/list
Add support for listing blueprints
2 parents 301be83 + 8a777b6 commit fa71f85

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/github.com/getnelson/nelson/blueprint.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func ProofBlueprint(req ProofBlueprintWire, http *gorequest.SuperAgent, cfg *Con
7272
* "template": "<base64 encoded template>"
7373
* }
7474
*/
75-
type CreateBlueprintResponse struct {
75+
type BlueprintResponse struct {
7676
Name string `json:"name"`
7777
Description string `json:"description"`
7878
Revision string `json:"revision"`
@@ -88,12 +88,12 @@ type CreateBlueprintRequest struct {
8888
Template string `json:"template"`
8989
}
9090

91-
func CreateBlueprint(req CreateBlueprintRequest, http *gorequest.SuperAgent, cfg *Config) (out CreateBlueprintResponse, err []error) {
91+
func CreateBlueprint(req CreateBlueprintRequest, http *gorequest.SuperAgent, cfg *Config) (out BlueprintResponse, err []error) {
9292

9393
r, body, errs := AugmentRequest(
9494
http.Post(cfg.Endpoint+"/v1/blueprints"), cfg).Send(req).EndBytes()
9595

96-
var result CreateBlueprintResponse
96+
var result BlueprintResponse
9797

9898
if errs != nil {
9999
return result, errs
@@ -112,16 +112,43 @@ func CreateBlueprint(req CreateBlueprintRequest, http *gorequest.SuperAgent, cfg
112112

113113
/////////////////// LISTING BLUEPRINTS ///////////////////
114114

115-
// func ListBlueprints(http *gorequest.SuperAgent, cfg *Config) (out CreateBlueprintResponse, err []error) {
116-
// }
115+
func ListBlueprints(http *gorequest.SuperAgent, cfg *Config) (list []BlueprintResponse, err []error) {
116+
uri := cfg.Endpoint + "/v1/blueprints"
117+
r, body, errs := AugmentRequest(http.Get(uri), cfg).EndBytes()
118+
119+
if errs != nil {
120+
return nil, errs
121+
}
122+
123+
if r.StatusCode/100 != 2 {
124+
errs = append(errs, errors.New("Unexpected response from Nelson server"))
125+
return nil, errs
126+
} else {
127+
var list []BlueprintResponse
128+
if err := json.Unmarshal(body, &list); err != nil {
129+
panic(err)
130+
}
131+
return list, errs
132+
}
133+
}
134+
135+
func PrintListBlueprints(bps []BlueprintResponse) {
136+
var tabulized = [][]string{}
137+
for _, r := range bps {
138+
name := r.Name + "@" + r.Revision
139+
tabulized = append(tabulized, []string{name, r.Description, r.Sha256, javaEpochToHumanizedTime(r.CreatedAt)})
140+
}
141+
142+
RenderTableToStdout([]string{"Reference", "Description", "Sha256", "Created"}, tabulized)
143+
}
117144

118145
/////////////////// INSPECTING BLUEPRINTS ///////////////////
119146

120-
func InspectBlueprint(namedRevision string, http *gorequest.SuperAgent, cfg *Config) (out CreateBlueprintResponse, err []error) {
147+
func InspectBlueprint(namedRevision string, http *gorequest.SuperAgent, cfg *Config) (out BlueprintResponse, err []error) {
121148
r, body, errs := AugmentRequest(
122149
http.Get(cfg.Endpoint+"/v1/blueprints/"+namedRevision), cfg).EndBytes()
123150

124-
var result CreateBlueprintResponse
151+
var result BlueprintResponse
125152

126153
if errs != nil {
127154
return result, errs

src/github.com/getnelson/nelson/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ func main() {
259259
return nil
260260
},
261261
},
262+
{
263+
Name: "list",
264+
Usage: "List all the available blueprints",
265+
Action: func(c *cli.Context) error {
266+
pi.Start()
267+
cfg := LoadDefaultConfigOrExit(http)
268+
r, e := ListBlueprints(http, cfg)
269+
pi.Stop()
270+
if e != nil {
271+
return cli.NewExitError("Unable to list blueprints.", 1)
272+
} else {
273+
PrintListBlueprints(r)
274+
}
275+
return nil
276+
},
277+
},
262278
},
263279
},
264280
////////////////////////////// DATACENTER //////////////////////////////////

0 commit comments

Comments
 (0)