detached fc7b2c6

Push command working

commit
fc7b2c60bf6722e3a3cabfeeb82f7a68f2baefb5
author
bnema · · 2023-10-27 08:32 UTC
parent
9572ee4
signature
unsigned
5 files · +19 -13
M internal/cli/cmd/ping.go +1 -1
@@ -36,7 +36,7 @@ func NewPingCommand(a *cli.App) *cobra.Command {
3636 Payload: payload,
3737 }
3838
39- resp, err := handler.SendHTTPRequest(a, &reqPayload, "/ping")
39+ resp, err := handler.SendHTTPRequest(a, &reqPayload, "GET", "/ping")
4040 if err != nil {
4141 fmt.Println("Error sending HTTP request:", err)
4242 return
@@ -36,7 +36,7 @@ func NewPingCommand(a *cli.App) *cobra.Command {
36 Payload: payload,36 Payload: payload,
37 }37 }
38 38
39- resp, err := handler.SendHTTPRequest(a, &reqPayload, "/ping")39+ resp, err := handler.SendHTTPRequest(a, &reqPayload, "GET", "/ping")
40 if err != nil {40 if err != nil {
41 fmt.Println("Error sending HTTP request:", err)41 fmt.Println("Error sending HTTP request:", err)
42 return42 return
M internal/cli/cmd/push.go +2 -2
@@ -52,7 +52,7 @@ func NewPushCommand(a *cli.App) *cobra.Command {
5252 }
5353 fmt.Println("Image exported successfully")
5454 // Create a RequestPayload and populate it
55- payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App
55+ reqPayload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App
5656 Type: "push",
5757 Payload: common.PushPayload{ // <- Same
5858 Ports: port,
@@ -63,7 +63,7 @@ func NewPushCommand(a *cli.App) *cobra.Command {
6363 }
6464 fmt.Println("Sending image to backend")
6565 // Send the request to the backend
66- resp, err := handler.SendHTTPRequest(a, &payload, "/push")
66+ resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push")
6767 if err != nil {
6868 fmt.Println("Error sending HTTP request:", err)
6969 return
@@ -52,7 +52,7 @@ func NewPushCommand(a *cli.App) *cobra.Command {
52 }52 }
53 fmt.Println("Image exported successfully")53 fmt.Println("Image exported successfully")
54 // Create a RequestPayload and populate it54 // Create a RequestPayload and populate it
55- payload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App55+ reqPayload := common.RequestPayload{ // <- TODO : Load RequestPayLoad from cli.App
56 Type: "push",56 Type: "push",
57 Payload: common.PushPayload{ // <- Same57 Payload: common.PushPayload{ // <- Same
58 Ports: port,58 Ports: port,
@@ -63,7 +63,7 @@ func NewPushCommand(a *cli.App) *cobra.Command {
63 }63 }
64 fmt.Println("Sending image to backend")64 fmt.Println("Sending image to backend")
65 // Send the request to the backend65 // Send the request to the backend
66- resp, err := handler.SendHTTPRequest(a, &payload, "/push")66+ resp, err := handler.SendHTTPRequest(a, &reqPayload, "POST", "/push")
67 if err != nil {67 if err != nil {
68 fmt.Println("Error sending HTTP request:", err)68 fmt.Println("Error sending HTTP request:", err)
69 return69 return
M internal/cli/handler/http.go +4 -3
@@ -39,21 +39,22 @@ type Response struct {
3939 }
4040
4141 // SendHTTPRequest sends the HTTP request
42-func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, endpoint string) (*Response, error) {
42+func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, method string, endpoint string) (*Response, error) {
4343 apiUrl := a.Config.Http.BackendURL + "/api"
4444 token := a.Config.General.Token
4545
46+ fmt.Printf("Final url:", apiUrl+endpoint)
47+
4648 // Prepare the entire RequestPayload, not just the inner Payload
4749 jsonPayload, err := json.Marshal(rp)
4850 if err != nil {
4951 return nil, fmt.Errorf("failed to marshal payload: %w", err)
5052 }
5153 // Create a new request
52- req, err := CreateNewRequest("GET", apiUrl+endpoint, jsonPayload)
54+ req, err := CreateNewRequest(method, apiUrl+endpoint, jsonPayload)
5355 if err != nil {
5456 return nil, fmt.Errorf("failed to create new request: %w", err)
5557 }
56-
5758 // Set the request headers
5859 SetRequestHeaders(req, token)
5960
@@ -39,21 +39,22 @@ type Response struct {
39 }39 }
40 40
41 // SendHTTPRequest sends the HTTP request41 // SendHTTPRequest sends the HTTP request
42-func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, endpoint string) (*Response, error) {42+func SendHTTPRequest(a *cli.App, rp *common.RequestPayload, method string, endpoint string) (*Response, error) {
43 apiUrl := a.Config.Http.BackendURL + "/api"43 apiUrl := a.Config.Http.BackendURL + "/api"
44 token := a.Config.General.Token44 token := a.Config.General.Token
45 45
46+ fmt.Printf("Final url:", apiUrl+endpoint)
47+
46 // Prepare the entire RequestPayload, not just the inner Payload48 // Prepare the entire RequestPayload, not just the inner Payload
47 jsonPayload, err := json.Marshal(rp)49 jsonPayload, err := json.Marshal(rp)
48 if err != nil {50 if err != nil {
49 return nil, fmt.Errorf("failed to marshal payload: %w", err)51 return nil, fmt.Errorf("failed to marshal payload: %w", err)
50 }52 }
51 // Create a new request53 // Create a new request
52- req, err := CreateNewRequest("GET", apiUrl+endpoint, jsonPayload)54+ req, err := CreateNewRequest(method, apiUrl+endpoint, jsonPayload)
53 if err != nil {55 if err != nil {
54 return nil, fmt.Errorf("failed to create new request: %w", err)56 return nil, fmt.Errorf("failed to create new request: %w", err)
55 }57 }
56-
57 // Set the request headers58 // Set the request headers
58 SetRequestHeaders(req, token)59 SetRequestHeaders(req, token)
59 60
R internal/httpserve/handler/endpoints.go → internal/httpserve/handler/clientendpoins.go +9 -7 renamed, 90% similar
@@ -61,7 +61,7 @@ func GetInfos(c echo.Context, a *server.App) error {
6161 func PostPush(c echo.Context, a *server.App) error {
6262 body, _ := io.ReadAll(c.Request().Body)
6363 c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) // Reset the body
64-
64+ fmt.Print(body)
6565 payload := new(common.RequestPayload)
6666 if err := c.Bind(payload); err != nil {
6767 fmt.Println("Bind Error:", err)
@@ -101,15 +101,17 @@ func PostPush(c echo.Context, a *server.App) error {
101101 return c.JSON(http.StatusBadRequest, "Image name cannot be empty")
102102 }
103103
104- // 1 - Import the image tar
105- imageId, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar))
104+ // 1 - Save the image tar in the storage
105+ imagePath, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar))
106106 if err != nil {
107107 return c.JSON(http.StatusBadRequest, err.Error())
108108 }
109- // 2 - Create the container with traefik labels
110- fmt.Println("Image ID:", imageId)
111- // 3 - Start the container
112- // 4 - Return :
109+ fmt.Printf("Saved in %s", imagePath)
110+ // 2 - Import the tar in docker
111+
112+ // 3 - Create the container with traefik labels
113+
114+ // 4 - Start the container
113115
114116 return c.JSON(http.StatusOK, "OK")
115117 }
@@ -61,7 +61,7 @@ func GetInfos(c echo.Context, a *server.App) error {
61 func PostPush(c echo.Context, a *server.App) error {61 func PostPush(c echo.Context, a *server.App) error {
62 body, _ := io.ReadAll(c.Request().Body)62 body, _ := io.ReadAll(c.Request().Body)
63 c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) // Reset the body63 c.Request().Body = io.NopCloser(bytes.NewBuffer(body)) // Reset the body
64-64+ fmt.Print(body)
65 payload := new(common.RequestPayload)65 payload := new(common.RequestPayload)
66 if err := c.Bind(payload); err != nil {66 if err := c.Bind(payload); err != nil {
67 fmt.Println("Bind Error:", err)67 fmt.Println("Bind Error:", err)
@@ -101,15 +101,17 @@ func PostPush(c echo.Context, a *server.App) error {
101 return c.JSON(http.StatusBadRequest, "Image name cannot be empty")101 return c.JSON(http.StatusBadRequest, "Image name cannot be empty")
102 }102 }
103 103
104- // 1 - Import the image tar104+ // 1 - Save the image tar in the storage
105- imageId, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar))105+ imagePath, err := store.SaveImageToStorage(&a.Config, imageName, bytes.NewBuffer(imageTar))
106 if err != nil {106 if err != nil {
107 return c.JSON(http.StatusBadRequest, err.Error())107 return c.JSON(http.StatusBadRequest, err.Error())
108 }108 }
109- // 2 - Create the container with traefik labels109+ fmt.Printf("Saved in %s", imagePath)
110- fmt.Println("Image ID:", imageId)110+ // 2 - Import the tar in docker
111- // 3 - Start the container111+
112- // 4 - Return :112+ // 3 - Create the container with traefik labels
113+
114+ // 4 - Start the container
113 115
114 return c.JSON(http.StatusOK, "OK")116 return c.JSON(http.StatusOK, "OK")
115 }117 }
M internal/httpserve/router.go +3 -0
@@ -51,6 +51,9 @@ func bindAPIEndpoints(e *echo.Echo, a *server.App) {
5151 apiGroup.GET("/ping", func(c echo.Context) error {
5252 return handler.GetInfos(c, a)
5353 })
54+ apiGroup.POST("/push", func(c echo.Context) error {
55+ return handler.PostPush(c, a)
56+ })
5457 }
5558
5659 // bindStaticRoute bind static path
@@ -51,6 +51,9 @@ func bindAPIEndpoints(e *echo.Echo, a *server.App) {
51 apiGroup.GET("/ping", func(c echo.Context) error {51 apiGroup.GET("/ping", func(c echo.Context) error {
52 return handler.GetInfos(c, a)52 return handler.GetInfos(c, a)
53 })53 })
54+ apiGroup.POST("/push", func(c echo.Context) error {
55+ return handler.PostPush(c, a)
56+ })
54 }57 }
55 58
56 // bindStaticRoute bind static path59 // bindStaticRoute bind static path

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close