From 70dc282b7dbab05b73bfc0b34d60d46c695697b5 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 24 Jan 2024 14:05:39 +0000 Subject: [PATCH] feat(cli): add favorite/unfavorite commands (#11793) --- cli/favorite.go | 64 +++++++++++++++++++++ cli/favorite_test.go | 45 +++++++++++++++ cli/root.go | 2 + cli/testdata/coder_--help.golden | 2 + cli/testdata/coder_favorite_--help.golden | 11 ++++ cli/testdata/coder_unfavorite_--help.golden | 11 ++++ docs/cli.md | 2 + docs/cli/favorite.md | 16 ++++++ docs/cli/unfavorite.md | 16 ++++++ docs/manifest.json | 10 ++++ 10 files changed, 179 insertions(+) create mode 100644 cli/favorite.go create mode 100644 cli/favorite_test.go create mode 100644 cli/testdata/coder_favorite_--help.golden create mode 100644 cli/testdata/coder_unfavorite_--help.golden create mode 100644 docs/cli/favorite.md create mode 100644 docs/cli/unfavorite.md diff --git a/cli/favorite.go b/cli/favorite.go new file mode 100644 index 0000000000..3853929c59 --- /dev/null +++ b/cli/favorite.go @@ -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 ", + 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 ", + 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 +} diff --git a/cli/favorite_test.go b/cli/favorite_test.go new file mode 100644 index 0000000000..5cdf5e765c --- /dev/null +++ b/cli/favorite_test.go @@ -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) + 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) +} diff --git a/cli/root.go b/cli/root.go index 8b8784735d..56ee8dba94 100644 --- a/cli/root.go +++ b/cli/root.go @@ -100,6 +100,7 @@ func (r *RootCmd) Core() []*clibase.Cmd { r.configSSH(), r.create(), r.deleteWorkspace(), + r.favorite(), r.list(), r.open(), r.ping(), @@ -112,6 +113,7 @@ func (r *RootCmd) Core() []*clibase.Cmd { r.start(), r.stat(), r.stop(), + r.unfavorite(), r.update(), // Hidden diff --git a/cli/testdata/coder_--help.golden b/cli/testdata/coder_--help.golden index e33d7ec7d2..501a336915 100644 --- a/cli/testdata/coder_--help.golden +++ b/cli/testdata/coder_--help.golden @@ -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 @@ -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 diff --git a/cli/testdata/coder_favorite_--help.golden b/cli/testdata/coder_favorite_--help.golden new file mode 100644 index 0000000000..ef83b207b6 --- /dev/null +++ b/cli/testdata/coder_favorite_--help.golden @@ -0,0 +1,11 @@ +coder v0.0.0-devel + +USAGE: + coder favorite + + Add a workspace to your favorites + + Aliases: fav, favourite + +——— +Run `coder --help` for a list of global options. diff --git a/cli/testdata/coder_unfavorite_--help.golden b/cli/testdata/coder_unfavorite_--help.golden new file mode 100644 index 0000000000..087b32b414 --- /dev/null +++ b/cli/testdata/coder_unfavorite_--help.golden @@ -0,0 +1,11 @@ +coder v0.0.0-devel + +USAGE: + coder unfavorite + + Remove a workspace from your favorites + + Aliases: unfav, unfavourite + +——— +Run `coder --help` for a list of global options. diff --git a/docs/cli.md b/docs/cli.md index 9fa787d6a6..8e1d2c56c7 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -31,6 +31,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr | [delete](./cli/delete.md) | Delete a workspace | | [dotfiles](./cli/dotfiles.md) | Personalize your workspace by applying a canonical dotfiles repository | | [external-auth](./cli/external-auth.md) | Manage external authentication | +| [favorite](./cli/favorite.md) | Add a workspace to your favorites | | [features](./cli/features.md) | List Enterprise features | | [groups](./cli/groups.md) | Manage groups | | [licenses](./cli/licenses.md) | Add, delete, and list licenses | @@ -57,6 +58,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr | [stop](./cli/stop.md) | Stop a workspace | | [templates](./cli/templates.md) | Manage templates | | [tokens](./cli/tokens.md) | Manage personal access tokens | +| [unfavorite](./cli/unfavorite.md) | Remove a workspace from your favorites | | [update](./cli/update.md) | Will update and start a given workspace if it is out of date | | [users](./cli/users.md) | Manage users | | [version](./cli/version.md) | Show coder version | diff --git a/docs/cli/favorite.md b/docs/cli/favorite.md new file mode 100644 index 0000000000..93b5027367 --- /dev/null +++ b/docs/cli/favorite.md @@ -0,0 +1,16 @@ + + +# favorite + +Add a workspace to your favorites + +Aliases: + +- fav +- favourite + +## Usage + +```console +coder favorite +``` diff --git a/docs/cli/unfavorite.md b/docs/cli/unfavorite.md new file mode 100644 index 0000000000..b1dca7a397 --- /dev/null +++ b/docs/cli/unfavorite.md @@ -0,0 +1,16 @@ + + +# unfavorite + +Remove a workspace from your favorites + +Aliases: + +- unfav +- unfavourite + +## Usage + +```console +coder unfavorite +``` diff --git a/docs/manifest.json b/docs/manifest.json index e863e27572..daef0fd2ca 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -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", @@ -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",