Skip to content

Commit

Permalink
feat: rework in rest client and create a new file .squareignore
Browse files Browse the repository at this point in the history
  • Loading branch information
richaardev committed Feb 7, 2024
1 parent 657a290 commit 4e85985
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 29 deletions.
13 changes: 7 additions & 6 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import (
)

type SquareCli struct {
Rest *rest.RestClient
Config *config.Config
}

func NewSquareCli() (squareCli *SquareCli) {
func NewSquareCli() *SquareCli {
config, err := config.Load()
if err != nil {
panic("could not load config file")
}

restClient := rest.NewClient(config.AuthToken)
squareCli = &SquareCli{
squareCli := &SquareCli{
Config: config,
Rest: restClient,
}

return
return squareCli
}

func (squareCli *SquareCli) Rest() *rest.RestClient {
return rest.NewClient(squareCli.Config.AuthToken)
}

func (squareCli *SquareCli) Err() io.Writer {
Expand Down
83 changes: 83 additions & 0 deletions internal/command/app/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package app

import (
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/spf13/cobra"
"github.com/squarecloudofc/cli/internal/cli"
"github.com/squarecloudofc/cli/internal/ui"
)

func NewBackupCommand(squareCli *cli.SquareCli) *cobra.Command {
cmd := &cobra.Command{
Use: "backup",
Short: "Create a backup of you application",
RunE: runBackupCommand(squareCli),
}

return cmd
}

func runBackupCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
}

if len(args) < 1 {
id, err := CreateApplicationSelection(squareCli)
if err != nil {
return err
}

appId = id
}

result, err := rest.ApplicationBackup(appId)
if err != nil {
return err
}

if result.DownloadURL == "" {
fmt.Fprintf(squareCli.Out(), "%s It's not possible to download your backup, please try again later...\n", ui.XMark)
}

fmt.Fprintln(squareCli.Out(), "Downloading your backup...")

time := time.Now().Format("2006-01-02 15:04:05")
filename := fmt.Sprintf("Square Cloud - Backup %s.zip", time)

err = downloadBackup(filename, result.DownloadURL)
if err != nil {
fmt.Fprintf(squareCli.Out(), "%s It's not possible to download your backup, please try again later...\n", ui.XMark)
return
}

fmt.Fprintf(squareCli.Out(), "%s Your backup is successfuly downloaded to %s\n", ui.CheckMark, filename)
return nil
}
}

func downloadBackup(destination string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

out, err := os.Create(destination)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
return err
}
3 changes: 2 additions & 1 deletion internal/command/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewDeleteCommand(squareCli *cli.SquareCli) *cobra.Command {
func runDeleteCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
Expand All @@ -35,7 +36,7 @@ func runDeleteCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []
appId = id
}

success, err := squareCli.Rest.ApplicationDelete(appId)
success, err := rest.ApplicationDelete(appId)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/app/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewLogsCommand(squareCli *cli.SquareCli) *cobra.Command {
func runLogsCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
Expand All @@ -34,7 +35,7 @@ func runLogsCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []st
appId = id
}

result, err := squareCli.Rest.ApplicationLogs(appId)
result, err := rest.ApplicationLogs(appId)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/app/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewRestartCommand(squareCli *cli.SquareCli) *cobra.Command {
func runRestartCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
Expand All @@ -35,7 +36,7 @@ func runRestartCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args [
appId = id
}

success, err := squareCli.Rest.ApplicationRestart(appId)
success, err := rest.ApplicationRestart(appId)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/command/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
type RunEFunc func(cmd *cobra.Command, args []string) error

func CreateApplicationSelection(squareCli *cli.SquareCli) (string, error) {
rapps, err := squareCli.Rest.SelfUser()
rest := squareCli.Rest()
rapps, err := rest.SelfUser()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -42,6 +43,7 @@ func NewAppCommand(squareCli *cli.SquareCli) *cobra.Command {
}

cmd.AddCommand(
NewBackupCommand(squareCli),
NewDeleteCommand(squareCli),
NewLogsCommand(squareCli),
NewStartCommand(squareCli),
Expand Down
3 changes: 2 additions & 1 deletion internal/command/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewStartCommand(squareCli *cli.SquareCli) *cobra.Command {
func runStartCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
Expand All @@ -35,7 +36,7 @@ func runStartCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []s
appId = id
}

success, err := squareCli.Rest.ApplicationStart(appId)
success, err := rest.ApplicationStart(appId)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/app/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewStatusCommand(squareCli *cli.SquareCli) *cobra.Command {
func runStatusCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
Expand All @@ -39,7 +40,7 @@ func runStatusCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []
appId = id
}

data, err := squareCli.Rest.ApplicationStatus(appId)
data, err := rest.ApplicationStatus(appId)

if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/command/app/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewStopCommand(squareCli *cli.SquareCli) *cobra.Command {
func runStopCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var appId string
rest := squareCli.Rest()

if len(args) > 0 {
appId = args[0]
Expand All @@ -35,7 +36,7 @@ func runStopCommand(squareCli *cli.SquareCli) func(cmd *cobra.Command, args []st
appId = id
}

success, err := squareCli.Rest.ApplicationStop(appId)
success, err := rest.ApplicationStop(appId)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func NewAppsCommand(squareCli *cli.SquareCli) *cobra.Command {

func runAppsCommand(squareCli *cli.SquareCli) RunEFunc {
return func(cmd *cobra.Command, args []string) (err error) {
self, err := squareCli.Rest.SelfUser()
rest := squareCli.Rest()
self, err := rest.SelfUser()
if err != nil {
return
}
Expand Down
18 changes: 13 additions & 5 deletions internal/command/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/squarecloudofc/cli/internal/cli"
"github.com/squarecloudofc/cli/internal/squareconfig"
"github.com/squarecloudofc/cli/internal/squareignore"
"github.com/squarecloudofc/cli/internal/ui"
"github.com/squarecloudofc/cli/pkg/zipper"
)
Expand All @@ -25,7 +26,9 @@ func NewCommitCommand(squareCli *cli.SquareCli) *cobra.Command {

func runCommitCommand(squareCli *cli.SquareCli) RunEFunc {
return func(cmd *cobra.Command, args []string) (err error) {
self, err := squareCli.Rest.SelfUser()
rest := squareCli.Rest()

self, err := rest.SelfUser()
if err != nil {
return err
}
Expand All @@ -41,12 +44,12 @@ func runCommitCommand(squareCli *cli.SquareCli) RunEFunc {
}

if config.IsCreated() {
fmt.Fprintln(squareCli.Out(), "seems you don't have a squarecloud.config file, please create one")
fmt.Fprintln(squareCli.Out(), "Seems you don't have a squarecloud.config file, please create one")
return
}

if config.ID == "" {
fmt.Fprintln(squareCli.Out(), "your squarecloud.config file don't have ID property")
fmt.Fprintln(squareCli.Out(), "Your squarecloud.config file don't have ID property")
}

workDir, err := os.Getwd()
Expand All @@ -62,12 +65,17 @@ func runCommitCommand(squareCli *cli.SquareCli) RunEFunc {
defer file.Close()
defer os.Remove(file.Name())

err = zipper.ZipFolder(workDir, file)
ignoreFiles, err := squareignore.Load()
if err != nil {
ignoreFiles = []string{}
}

err = zipper.ZipFolder(workDir, file, ignoreFiles)
if err != nil {
return err
}

success, err := squareCli.Rest.ApplicationCommit(config.ID, file.Name())
success, err := rest.ApplicationCommit(config.ID, file.Name())
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/command/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func NewWhoamiCommand(squareCli *cli.SquareCli) *cobra.Command {

func runWhoamiCommand(squareCli *cli.SquareCli) RunEFunc {
return func(cmd *cobra.Command, args []string) (err error) {
self, err := squareCli.Rest.SelfUser()
rest := squareCli.Rest()
self, err := rest.SelfUser()
if err != nil {
return err
}
Expand Down
24 changes: 20 additions & 4 deletions internal/command/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/spf13/cobra"
"github.com/squarecloudofc/cli/internal/cli"
"github.com/squarecloudofc/cli/internal/squareignore"
"github.com/squarecloudofc/cli/internal/ui"
"github.com/squarecloudofc/cli/pkg/zipper"
)

Expand All @@ -22,27 +25,40 @@ func NewZipCommand(squareCli *cli.SquareCli) *cobra.Command {

func runZipCommand(squareCli *cli.SquareCli) RunEFunc {
return func(cmd *cobra.Command, args []string) (err error) {
fmt.Fprintln(squareCli.Out(), "zipping your aplication")
workDir, err := os.Getwd()
if err != nil {
return err
}

zipfilename := path.Join(workDir, "source.zip")
workDirName := filepath.Base(workDir)

zipfilename := path.Join(workDir, workDirName+".zip")
if _, err := os.Lstat(zipfilename); err == nil {
err := os.Remove(zipfilename)
if err != nil {
fmt.Fprintln(squareCli.Err(), "source.zip already exists and its not possible to delete it")
}
}

file, err := os.CreateTemp("", "*.zip")
if err != nil {
return err
}
defer file.Close()

err = zipper.ZipFolder(workDir, file)
ignoreFiles, err := squareignore.Load()
if err != nil {
ignoreFiles = []string{}
}

err = zipper.ZipFolder(workDir, file, ignoreFiles)
if err != nil {
return err
}

os.Rename(file.Name(), zipfilename)

fmt.Fprintf(squareCli.Out(), "your source has successfuly zipped")
fmt.Fprintf(squareCli.Out(), "%s Your source has successfuly zipped to %s.zip\n", ui.CheckMark, workDirName)
return nil
}
}
Loading

0 comments on commit 4e85985

Please sign in to comment.