Skip to content

Commit

Permalink
chore: update api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
richaardev committed Nov 11, 2024
1 parent 91bf0f1 commit 5450dd1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
37 changes: 18 additions & 19 deletions internal/command/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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())
Expand Down
18 changes: 9 additions & 9 deletions internal/rest/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
7 changes: 3 additions & 4 deletions internal/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5450dd1

Please sign in to comment.