/
shell.go
82 lines (70 loc) · 1.92 KB
/
shell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package plural
import (
"os"
"github.com/pluralsh/plural-cli/pkg/api"
"github.com/pluralsh/plural-cli/pkg/config"
"github.com/pluralsh/plural-cli/pkg/crypto"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/pluralsh/plural-cli/pkg/utils/git"
"github.com/urfave/cli"
)
func shellCommands() []cli.Command {
return []cli.Command{
{
Name: "sync",
Usage: "syncs the setup in your cloud shell locally",
Action: latestVersion(handleShellSync),
Flags: []cli.Flag{
cli.StringFlag{
Name: "endpoint",
Usage: "the endpoint for the plural installation you're working with",
},
cli.StringFlag{
Name: "service-account",
Usage: "email for the service account you'd like to use for this workspace",
},
},
},
{
Name: "purge",
Usage: "deletes your cloud shell",
Action: latestVersion(handleShellPurge),
},
}
}
func handleShellSync(c *cli.Context) error {
if !config.Exists() {
if err := handleLogin(c); err != nil {
return err
}
}
client := api.NewClient()
shell, err := client.GetShell()
if err != nil {
return api.GetErrorResponse(err, "GetShell")
}
if err := crypto.Setup(shell.AesKey); err != nil {
return err
}
utils.Highlight("Cloning your workspace repo locally:\n")
if err := utils.Exec("git", "clone", shell.GitUrl); err != nil {
return err
}
dir := git.RepoName(shell.GitUrl)
if err := os.Chdir(dir); err != nil {
return err
}
if err := cryptoInit(c); err != nil {
return err
}
return handleUnlock(c)
}
var destoryShellConfirm = "Are you sure you want to destroy your cloud shell (you should either `plural destroy` anything deployed or `plural shell sync` to sync the contents locally)?"
func handleShellPurge(c *cli.Context) error {
if ok := confirm(destoryShellConfirm, "PLURAL_SHELL_PURGE_CONFIRM"); !ok {
return nil
}
client := api.NewClient()
err := client.DeleteShell()
return api.GetErrorResponse(err, "DeleteShell")
}