@@ -3,6 +3,7 @@ package enaptercli
33import (
44 "bytes"
55 "context"
6+ "encoding/json"
67 "fmt"
78 "net/http"
89 "os"
@@ -41,25 +42,54 @@ func (c *cmdBlueprintsUpload) Flags() []cli.Flag {
4142}
4243
4344func (c * cmdBlueprintsUpload ) upload (ctx context.Context ) error {
44- fi , err := os .Stat (c .blueprintPath )
45+ return uploadBlueprint (ctx , c .blueprintPath , c .doHTTPRequest )
46+ }
47+
48+ func uploadBlueprintAndReturnBlueprintID (ctx context.Context , blueprintPath string ,
49+ doHTTPRequest func (context.Context , doHTTPRequestParams ) error ,
50+ ) (string , error ) {
51+ var blueprintID string
52+ err := uploadBlueprint (ctx , blueprintPath , func (ctx context.Context , reqParams doHTTPRequestParams ) error {
53+ reqParams .RespProcessor = func (resp * http.Response ) error {
54+ var respBlueprint struct {
55+ Blueprint struct {
56+ ID string `json:"id"`
57+ } `json:"blueprint"`
58+ }
59+ if err := json .NewDecoder (resp .Body ).Decode (& respBlueprint ); err != nil {
60+ return fmt .Errorf ("decode blueprint response: %w" , err )
61+ }
62+ blueprintID = respBlueprint .Blueprint .ID
63+ return nil
64+ }
65+ return doHTTPRequest (ctx , reqParams )
66+ })
67+ return blueprintID , err
68+ }
69+
70+ func uploadBlueprint (
71+ ctx context.Context , blueprintPath string ,
72+ doHTTPRequest func (context.Context , doHTTPRequestParams ) error ,
73+ ) error {
74+ fi , err := os .Stat (blueprintPath )
4575 if err != nil {
4676 return fmt .Errorf ("check blueprint path: %w" , err )
4777 }
4878
4979 var data []byte
5080 if fi .IsDir () {
51- data , err = zipDir (c . blueprintPath )
81+ data , err = zipDir (blueprintPath )
5282 if err != nil {
5383 return fmt .Errorf ("zip blueprint directory: %w" , err )
5484 }
5585 } else {
56- data , err = os .ReadFile (c . blueprintPath )
86+ data , err = os .ReadFile (blueprintPath )
5787 if err != nil {
5888 return fmt .Errorf ("read blueprint zip file: %w" , err )
5989 }
6090 }
6191
62- return c . doHTTPRequest (ctx , doHTTPRequestParams {
92+ return doHTTPRequest (ctx , doHTTPRequestParams {
6393 Method : http .MethodPost ,
6494 Path : "/blueprints/upload" ,
6595 Body : bytes .NewReader (data ),
0 commit comments