- Notifications
You must be signed in to change notification settings - Fork925
feat(cli): add favorite/unfavorite commands#11793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cli | ||
import ( | ||
"fmt" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/cli/clibase" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
func (r *RootCmd) favorite() *clibase.Cmd { | ||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
Aliases: []string{"fav", "favou" + "rite"}, | ||
Annotations: workspaceCommand, | ||
Use: "favorite <workspace>", | ||
Short: "Add a workspace to your favorites", | ||
Middleware: clibase.Chain( | ||
clibase.RequireNArgs(1), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *clibase.Invocation) error { | ||
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
if err := client.FavoriteWorkspace(inv.Context(), ws.ID); err != nil { | ||
return xerrors.Errorf("favorite workspace: %w", err) | ||
} | ||
_, _ = fmt.Fprintf(inv.Stdout, "Workspace %q added to favorites.\n", ws.Name) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
func (r *RootCmd) unfavorite() *clibase.Cmd { | ||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
Aliases: []string{"unfav", "unfavou" + "rite"}, | ||
Annotations: workspaceCommand, | ||
Use: "unfavorite <workspace>", | ||
Short: "Remove a workspace from your favorites", | ||
Middleware: clibase.Chain( | ||
clibase.RequireNArgs(1), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *clibase.Invocation) error { | ||
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
if err := client.UnfavoriteWorkspace(inv.Context(), ws.ID); err != nil { | ||
return xerrors.Errorf("unfavorite workspace: %w", err) | ||
} | ||
_, _ = fmt.Fprintf(inv.Stdout, "Workspace %q removed from favorites.\n", ws.Name) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cli_test | ||
import ( | ||
"bytes" | ||
"testing" | ||
"github.com/coder/coder/v2/cli/clitest" | ||
"github.com/coder/coder/v2/coderd/coderdtest" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbfake" | ||
"github.com/stretchr/testify/require" | ||
) | ||
func TestFavoriteUnfavorite(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
client, db = coderdtest.NewWithDatabase(t, nil) | ||
owner = coderdtest.CreateFirstUser(t, client) | ||
memberClient, member = coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) | ||
ws = dbfake.WorkspaceBuild(t, db, database.Workspace{OwnerID: member.ID, OrganizationID: owner.OrganizationID}).Do() | ||
) | ||
inv, root := clitest.New(t, "favorite", ws.Workspace.Name) | ||
clitest.SetupConfig(t, memberClient, root) | ||
var buf bytes.Buffer | ||
inv.Stdout = &buf | ||
err := inv.Run() | ||
require.NoError(t, err) | ||
updated := coderdtest.MustWorkspace(t, memberClient, ws.Workspace.ID) | ||
require.True(t, updated.Favorite) | ||
buf.Reset() | ||
inv, root = clitest.New(t, "unfavorite", ws.Workspace.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What will happen if a user marks their workspace as "favorite" twice? Is it relevant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nothing interesting really will happen. It'll get updated again, which is essentially a no-op. We could check if the workspace is already favorited, but the overhead probably isn't worth the effort. | ||
clitest.SetupConfig(t, memberClient, root) | ||
inv.Stdout = &buf | ||
err = inv.Run() | ||
require.NoError(t, err) | ||
updated = coderdtest.MustWorkspace(t, memberClient, ws.Workspace.ID) | ||
require.False(t, updated.Favorite) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
coder v0.0.0-devel | ||
USAGE: | ||
coder favorite <workspace> | ||
Add a workspace to your favorites | ||
Aliases: fav, favourite | ||
——— | ||
Run `coder --help` for a list of global options. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
coder v0.0.0-devel | ||
USAGE: | ||
coder unfavorite <workspace> | ||
Remove a workspace from your favorites | ||
Aliases: unfav, unfavourite | ||
——— | ||
Run `coder --help` for a list of global options. |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.