From 5450dd168443742bd3f2505f6f26626d4e05f9ec Mon Sep 17 00:00:00 2001 From: Richard Martins Date: Mon, 11 Nov 2024 01:12:19 -0300 Subject: [PATCH] chore: update api endpoints --- internal/command/commit.go | 37 ++++++++++++++++++------------------- internal/rest/endpoints.go | 18 +++++++++--------- internal/rest/rest.go | 7 +++---- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/internal/command/commit.go b/internal/command/commit.go index e62772c..b258d39 100644 --- a/internal/command/commit.go +++ b/internal/command/commit.go @@ -3,21 +3,19 @@ package command import ( "fmt" "os" - "path" "path/filepath" "github.com/spf13/cobra" "github.com/squarecloudofc/cli/internal/cli" "github.com/squarecloudofc/cli/internal/ui" "github.com/squarecloudofc/cli/pkg/squareconfig" - "github.com/squarecloudofc/cli/pkg/squareignore" - "github.com/squarecloudofc/cli/pkg/zipper" ) type CommitOptions struct { ConfigFile *squareconfig.SquareConfig ApplicationID string Restart bool + File string } func NewCommitCommand(squareCli *cli.SquareCli) *cobra.Command { @@ -50,6 +48,7 @@ func NewCommitCommand(squareCli *cli.SquareCli) *cobra.Command { } cmd.Flags().BoolVarP(&options.Restart, "restart", "r", false, "Restart your application when commit") + cmd.Flags().StringVar(&options.File, "file", "", "File you want to upload to square cloud") return cmd } @@ -61,22 +60,22 @@ func runCommitCommand(squareCli *cli.SquareCli, options *CommitOptions) error { return err } - zipfilename := path.Join(workDir, "*.zip") - file, err := os.CreateTemp("", filepath.Base(zipfilename)) - if err != nil { - return err - } - defer file.Close() - defer os.Remove(file.Name()) - - ignoreFiles, err := squareignore.Load() - if err != nil { - ignoreFiles = []string{} - } - - err = zipper.ZipFolder(workDir, file, ignoreFiles) - if err != nil { - return err + var file *os.File + if options.File != "" { + file, err = os.Open(filepath.Join(workDir, options.File)) + if err != nil { + fmt.Fprintln(squareCli.Out(), "Unable to open the zip file") + return err + } + } else { + file, err = zipWorkdir(workDir) + if err != nil { + fmt.Fprintln(squareCli.Out(), "Unable to zip the working directory") + return err + } + + defer file.Close() + defer os.Remove(file.Name()) } success, err := rest.ApplicationCommit(options.ApplicationID, options.Restart, file.Name()) diff --git a/internal/rest/endpoints.go b/internal/rest/endpoints.go index f44a9b5..cee0eb4 100644 --- a/internal/rest/endpoints.go +++ b/internal/rest/endpoints.go @@ -18,19 +18,19 @@ var ( EndpointUser = func() string { return "/users/me" } // Application - EndpointApplication = func(appId string) string { return fmt.Sprintf("/apps/%s", appId) } - EndpointApplicationStatus = func(appId string) string { return fmt.Sprintf("/apps/%s/status", appId) } - EndpointApplicationLogs = func(appId string) string { return fmt.Sprintf("/apps/%s/logs", appId) } - EndpointApplicationStart = func(appId string) string { return fmt.Sprintf("/apps/%s/start", appId) } - EndpointApplicationRestart = func(appId string) string { return fmt.Sprintf("/apps/%s/restart", appId) } - EndpointApplicationStop = func(appId string) string { return fmt.Sprintf("/apps/%s/stop", appId) } - EndpointApplicationBackup = func(appId string) string { return fmt.Sprintf("/apps/%s/backups", appId) } - EndpointApplicationCommit = func(appId string, restart bool) string { + EndpointApplication = func() string { return "/apps" } + EndpointApplicationInformation = func(appId string) string { return fmt.Sprintf("/apps/%s", appId) } + EndpointApplicationStatus = func(appId string) string { return fmt.Sprintf("/apps/%s/status", appId) } + EndpointApplicationLogs = func(appId string) string { return fmt.Sprintf("/apps/%s/logs", appId) } + EndpointApplicationStart = func(appId string) string { return fmt.Sprintf("/apps/%s/start", appId) } + EndpointApplicationRestart = func(appId string) string { return fmt.Sprintf("/apps/%s/restart", appId) } + EndpointApplicationStop = func(appId string) string { return fmt.Sprintf("/apps/%s/stop", appId) } + EndpointApplicationBackup = func(appId string) string { return fmt.Sprintf("/apps/%s/backups", appId) } + EndpointApplicationCommit = func(appId string, restart bool) string { return fmt.Sprintf("/apps/%s/commit?restart=%s", appId, strconv.FormatBool(restart)) } EndpointApplicationDelete = func(appId string) string { return fmt.Sprintf("/apps/%s/delete", appId) } // ROUTE DEPRECATED USE -> DELETE AT /apps EndpointApplicationsStatus = func() string { return "/apps/status" } - EndpointApplicationUpload = func() string { return "/apps/upload" } // Application File Manager EndpointApplicationFiles = func(appId, path string) string { return fmt.Sprintf("/apps/%s/files?path=%s", appId, path) } diff --git a/internal/rest/rest.go b/internal/rest/rest.go index 428b775..8516b9f 100644 --- a/internal/rest/rest.go +++ b/internal/rest/rest.go @@ -62,7 +62,6 @@ func (c *RestClient) Request(method, url string, body []byte, respBody interface return fmt.Errorf("error reading response body: %w", err) } - println(string(rawResponse)) switch resp.StatusCode { case http.StatusOK, http.StatusCreated, http.StatusNoContent: if err := json.Unmarshal(rawResponse, respBody); err != nil { @@ -92,7 +91,7 @@ func (c *RestClient) SelfUser(options ...RequestOption) (*ResponseUser, error) { func (c *RestClient) Application(appId string, options ...RequestOption) (*ResponseApplicationInformation, error) { var r ApiResponse[ResponseApplicationInformation] - err := c.Request(http.MethodGet, MakeURL(EndpointApplication(appId)), nil, &r, options...) + err := c.Request(http.MethodGet, MakeURL(EndpointApplicationInformation(appId)), nil, &r, options...) return &r.Response, err } @@ -152,7 +151,7 @@ func (c *RestClient) ApplicationBackup(appId string, options ...RequestOption) ( func (c *RestClient) UploadApplication(options ...RequestOption) (*ResponseUploadApplication, error) { var r ApiResponse[ResponseUploadApplication] - err := c.Request(http.MethodGet, MakeURL(EndpointApplicationUpload()), nil, &r, options...) + err := c.Request(http.MethodGet, MakeURL(EndpointApplication()), nil, &r, options...) if err != nil { return nil, err } @@ -286,7 +285,7 @@ func (c *RestClient) ApplicationUpload(appId string, filep string, options ...Re options = append(options, WithHeader("Content-Type", writer.FormDataContentType())) var r ApiResponse[ResponseUploadApplication] - err = c.Request(http.MethodPost, MakeURL(EndpointApplicationUpload()), bodyBuffer.Bytes(), &r, options...) + err = c.Request(http.MethodPost, MakeURL(EndpointApplication()), bodyBuffer.Bytes(), &r, options...) if err != nil { return nil, err }