Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cli/favorite.go
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
}
45 changes: 45 additions & 0 deletions cli/favorite_test.go
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)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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)
}
2 changes: 2 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (r *RootCmd) Core() []*clibase.Cmd {
r.configSSH(),
r.create(),
r.deleteWorkspace(),
r.favorite(),
r.list(),
r.open(),
r.ping(),
Expand All @@ -112,6 +113,7 @@ func (r *RootCmd) Core() []*clibase.Cmd {
r.start(),
r.stat(),
r.stop(),
r.unfavorite(),
r.update(),

// Hidden
Expand Down
2 changes: 2 additions & 0 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SUBCOMMANDS:
dotfiles Personalize your workspace by applying a canonical
dotfiles repository
external-auth Manage external authentication
favorite Add a workspace to your favorites
list List workspaces
login Authenticate with Coder deployment
logout Unauthenticate your local session
Expand All @@ -47,6 +48,7 @@ SUBCOMMANDS:
stop Stop a workspace
templates Manage templates
tokens Manage personal access tokens
unfavorite Remove a workspace from your favorites
update Will update and start a given workspace if it is out of
date
users Manage users
Expand Down
11 changes: 11 additions & 0 deletions cli/testdata/coder_favorite_--help.golden
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.
11 changes: 11 additions & 0 deletions cli/testdata/coder_unfavorite_--help.golden
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.
2 changes: 2 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr
| [<code>delete</code>](./cli/delete.md) | Delete a workspace |
| [<code>dotfiles</code>](./cli/dotfiles.md) | Personalize your workspace by applying a canonical dotfiles repository |
| [<code>external-auth</code>](./cli/external-auth.md) | Manage external authentication |
| [<code>favorite</code>](./cli/favorite.md) | Add a workspace to your favorites |
| [<code>features</code>](./cli/features.md) | List Enterprise features |
| [<code>groups</code>](./cli/groups.md) | Manage groups |
| [<code>licenses</code>](./cli/licenses.md) | Add, delete, and list licenses |
Expand All @@ -57,6 +58,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr
| [<code>stop</code>](./cli/stop.md) | Stop a workspace |
| [<code>templates</code>](./cli/templates.md) | Manage templates |
| [<code>tokens</code>](./cli/tokens.md) | Manage personal access tokens |
| [<code>unfavorite</code>](./cli/unfavorite.md) | Remove a workspace from your favorites |
| [<code>update</code>](./cli/update.md) | Will update and start a given workspace if it is out of date |
| [<code>users</code>](./cli/users.md) | Manage users |
| [<code>version</code>](./cli/version.md) | Show coder version |
Expand Down
16 changes: 16 additions & 0 deletions docs/cli/favorite.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions docs/cli/unfavorite.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,11 @@
"description": "Print auth for an external provider",
"path": "cli/external-auth_access-token.md"
},
{
"title": "favorite",
"description": "Add a workspace to your favorites",
"path": "cli/favorite.md"
},
{
"title": "features",
"description": "List Enterprise features",
Expand Down Expand Up @@ -951,6 +956,11 @@
"description": "Delete a token",
"path": "cli/tokens_remove.md"
},
{
"title": "unfavorite",
"description": "Remove a workspace from your favorites",
"path": "cli/unfavorite.md"
},
{
"title": "update",
"description": "Will update and start a given workspace if it is out of date",
Expand Down