-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rework in rest client and create a new file
.squareignore
- Loading branch information
1 parent
657a290
commit 4e85985
Showing
15 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.