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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 179 additions and 0 deletions

64
cli/favorite.go Normal file
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
cli/favorite_test.go Normal file
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)
}

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

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

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.

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.

View File

@ -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 |
@ -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 |

16
docs/cli/favorite.md generated Normal file
View File

@ -0,0 +1,16 @@
<!-- DO NOT EDIT | GENERATED CONTENT -->
# favorite
Add a workspace to your favorites
Aliases:
- fav
- favourite
## Usage
```console
coder favorite <workspace>
```

16
docs/cli/unfavorite.md generated Normal file
View File

@ -0,0 +1,16 @@
<!-- DO NOT EDIT | GENERATED CONTENT -->
# unfavorite
Remove a workspace from your favorites
Aliases:
- unfav
- unfavourite
## Usage
```console
coder unfavorite <workspace>
```

View File

@ -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",