-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete-workspace.go
More file actions
74 lines (59 loc) · 1.82 KB
/
delete-workspace.go
File metadata and controls
74 lines (59 loc) · 1.82 KB
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
package cmd
import (
"errors"
"fmt"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)
type Prompt interface {
InputPrompt(prompt string) string
}
type DeleteWorkspaceCmd struct {
cmd *cobra.Command
Opts DeleteWorkspaceOpts
Prompt Prompt
}
type DeleteWorkspaceOpts struct {
GlobalOptions
Confirmed *bool
}
func (c *DeleteWorkspaceCmd) RunE(_ *cobra.Command, args []string) error {
wsId, err := c.Opts.GetWorkspaceId()
if err != nil {
return fmt.Errorf("failed to get workspace ID: %w", err)
}
client, err := NewClient(c.Opts.GlobalOptions)
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %w", err)
}
return c.DeleteWorkspace(client, wsId)
}
func AddDeleteWorkspaceCmd(delete *cobra.Command, opts GlobalOptions) {
workspace := DeleteWorkspaceCmd{
cmd: &cobra.Command{
Use: "workspace",
Short: "Delete workspace",
Long: io.Long(`Delete workspace after confirmation.
Confirmation can be given interactively or with the --yes flag`),
},
Opts: DeleteWorkspaceOpts{GlobalOptions: opts},
Prompt: &io.Prompt{},
}
workspace.Opts.Confirmed = workspace.cmd.Flags().Bool("yes", false, "Confirm deletion of workspace")
delete.AddCommand(workspace.cmd)
workspace.cmd.RunE = workspace.RunE
}
func (c *DeleteWorkspaceCmd) DeleteWorkspace(client Client, wsId int) error {
workspace, err := client.GetWorkspace(wsId)
if err != nil {
return fmt.Errorf("failed to get workspace %d: %w", wsId, err)
}
if !*c.Opts.Confirmed {
fmt.Printf("Please confirm deletion of workspace '%s', ID %d, in team %d by entering its name:\n", workspace.Name, workspace.Id, workspace.TeamId)
confirmation := c.Prompt.InputPrompt("Confirmation delete")
if confirmation != workspace.Name {
return errors.New("confirmation failed")
}
}
return client.DeleteWorkspace(wsId)
}