Skip to content

Commit

Permalink
fu command
Browse files Browse the repository at this point in the history
  • Loading branch information
naderkhalil committed Dec 19, 2023
1 parent c924e5b commit 18543b3
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/brevdev/brev-cli/pkg/cmd/delete"
"github.com/brevdev/brev-cli/pkg/cmd/envsetup"
"github.com/brevdev/brev-cli/pkg/cmd/envvars"
"github.com/brevdev/brev-cli/pkg/cmd/fu"
"github.com/brevdev/brev-cli/pkg/cmd/healthcheck"
"github.com/brevdev/brev-cli/pkg/cmd/hello"
"github.com/brevdev/brev-cli/pkg/cmd/importideconfig"
Expand Down Expand Up @@ -249,6 +250,7 @@ func createCmdTree(cmd *cobra.Command, t *terminal.Terminal, loginCmdStore *stor
cmd.AddCommand(clipboard.ForwardPort(t, loginCmdStore))
cmd.AddCommand(envvars.NewCmdEnvVars(t, loginCmdStore))
cmd.AddCommand(connect.NewCmdConnect(t, noLoginCmdStore))
cmd.AddCommand(fu.NewCmdFu(t, loginCmdStore, noLoginCmdStore))
} else {
_ = 0 // noop
}
Expand Down
107 changes: 107 additions & 0 deletions pkg/cmd/fu/fu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package fu

import (
_ "embed"

Check failure on line 4 in pkg/cmd/fu/fu.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04)

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 4 in pkg/cmd/fu/fu.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04)

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)

Check failure on line 4 in pkg/cmd/fu/fu.go

View workflow job for this annotation

GitHub Actions / goreleaser

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
"fmt"
"time"

"github.com/brevdev/brev-cli/pkg/cmd/completions"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
stripmd "github.com/writeas/go-strip-markdown"
)

var (
fuLong string
fuExample = "brev fu <user_id>"
)

type FuStore interface {
completions.CompletionStore
DeleteWorkspace(workspaceID string) (*entity.Workspace, error)
GetWorkspaces(organizationID string, options *store.GetWorkspacesOptions) ([]entity.Workspace, error)
BanUser(userID string) error
GetWorkspaceByNameOrID(orgID string, nameOrID string) ([]entity.Workspace, error)
GetAllOrgsAsAdmin(userID string) ([]entity.Organization, error)
}

func NewCmdFu(t *terminal.Terminal, loginFuStore FuStore, noLoginFuStore FuStore) *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{"workspace": ""},
Use: "fu",
DisableFlagsInUseLine: true,
Short: "Fetch all workspaces for a user and delete them",
Long: stripmd.Strip(fuLong),
Example: fuExample,
ValidArgsFunction: completions.GetAllWorkspaceNameCompletionHandler(noLoginFuStore, t),
RunE: func(cmd *cobra.Command, args []string) error {
var allError error
for _, userID := range args {
err := fuUser(userID, t, loginFuStore)
if err != nil {
allError = multierror.Append(allError, err)
}
}
if allError != nil {
return breverrors.WrapAndTrace(allError)
}
return nil
},
}

return cmd
}

func fuUser(userID string, t *terminal.Terminal, fuStore FuStore) error {
orgs, err := fuStore.GetAllOrgsAsAdmin(userID)
if err != nil {
return breverrors.WrapAndTrace(err)
}

var allWorkspaces []entity.Workspace
for _, org := range orgs {
workspaces, err := fuStore.GetWorkspaces(org.ID, nil)

Check failure on line 67 in pkg/cmd/fu/fu.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04)

shadow: declaration of "err" shadows declaration at line 60 (govet)

Check failure on line 67 in pkg/cmd/fu/fu.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04)

shadow: declaration of "err" shadows declaration at line 60 (govet)

Check failure on line 67 in pkg/cmd/fu/fu.go

View workflow job for this annotation

GitHub Actions / goreleaser

shadow: declaration of "err" shadows declaration at line 60 (govet)
if err != nil {
return breverrors.WrapAndTrace(err)
}
allWorkspaces = append(allWorkspaces, workspaces...)
}

s := t.NewSpinner()
s.Suffix = " Fetching workspaces for user " + userID
s.Start()
time.Sleep(5 * time.Second)
s.Stop()

confirm := terminal.PromptGetInput(terminal.PromptContent{
Label: fmt.Sprintf("Are you sure you want to delete all %d workspaces for user %s? (y/n)", len(allWorkspaces), userID),
ErrorMsg: "You must confirm to proceed.",
AllowEmpty: false,
})
if confirm != "y" {
return nil
}

for _, workspace := range allWorkspaces {
_, err2 := fuStore.DeleteWorkspace(workspace.ID)
if err2 != nil {
t.Vprintf(t.Red("Failed to delete workspace with ID: %s\n", workspace.ID))
t.Vprintf(t.Red("Error: %s\n", err.Error()))
continue
}
t.Vprintf("✅ Deleted workspace %s\n", workspace.Name)
}

err = fuStore.BanUser(userID)
if err != nil {
t.Vprintf(t.Red("Failed to ban user with ID: %s\n", userID))
t.Vprintf(t.Red("Error: %s\n", err.Error()))
}
t.Vprintf("✅ Banned user %s\n", userID)

return nil
}
1 change: 1 addition & 0 deletions pkg/cmd/fu/fu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package fu
29 changes: 29 additions & 0 deletions pkg/store/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,35 @@ func (s AuthHTTPStore) DeleteWorkspace(workspaceID string) (*entity.Workspace, e
return &result, nil
}

func (s AuthHTTPStore) BanUser(userID string) error {
res, err := s.authHTTPClient.restyClient.R().
SetHeader("Content-Type", "application/json").
Put(fmt.Sprintf("/api/users/%s/block", userID))
if err != nil {
return breverrors.WrapAndTrace(err)
}
if res.IsError() {
return NewHTTPResponseError(res)
}
return nil
}

func (s AuthHTTPStore) GetAllOrgsAsAdmin(userID string) ([]entity.Organization, error) {
var result []entity.Organization
res, err := s.authHTTPClient.restyClient.R().
SetHeader("Content-Type", "application/json").
SetQueryParam("user_id", userID).
SetResult(&result).
Get("/api/organizations")
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
if res.IsError() {
return nil, NewHTTPResponseError(res)
}
return result, nil
}

var (
workspaceMetadataPathPattern = fmt.Sprintf("%s/metadata", workspacePathPattern)
workspaceMetadataPath = fmt.Sprintf(workspaceMetadataPathPattern, fmt.Sprintf("{%s}", workspaceIDParamName))
Expand Down

0 comments on commit 18543b3

Please sign in to comment.