feat(cli): add favorite/unfavorite commands (#11793)

This commit is contained in:
Cian Johnston
2024-01-24 14:05:39 +00:00
committed by GitHub
parent f92336c4d5
commit 70dc282b7d
10 changed files with 179 additions and 0 deletions
+64
View File
@@ -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
View File
@@ -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)
}
+2
View File
@@ -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
+2
View File
@@ -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
+11
View File
@@ -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
View File
@@ -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.