This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package integration | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
@@ -11,6 +12,8 @@ import ( | |
"time" | ||
|
||
"cdr.dev/coder-cli/ci/tcli" | ||
"cdr.dev/coder-cli/internal/entclient" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogtest/assert" | ||
) | ||
|
||
|
@@ -110,6 +113,19 @@ func TestCoderCLI(t *testing.T) { | |
tcli.Error(), | ||
) | ||
|
||
var user entclient.User | ||
c.Run(ctx, `coder users ls -o json | jq -c '.[] | select( .username == "charlie")'`).Assert(t, | ||
tcli.Success(), | ||
jsonUnmarshals(&user), | ||
) | ||
assert.Equal(t, "user email is as expected", "[email protected]", user.Email) | ||
assert.Equal(t, "username is as expected", "Charlie", user.Name) | ||
|
||
c.Run(ctx, "coder users ls -o human | grep charlie").Assert(t, | ||
tcli.Success(), | ||
tcli.StdoutMatches("charlie"), | ||
) | ||
|
||
c.Run(ctx, "coder logout").Assert(t, | ||
tcli.Success(), | ||
) | ||
|
@@ -118,3 +134,11 @@ func TestCoderCLI(t *testing.T) { | |
tcli.Error(), | ||
) | ||
} | ||
|
||
func jsonUnmarshals(target interface{}) tcli.Assertion { | ||
return func(t *testing.T, r *tcli.CommandResult) { | ||
slog.Helper() | ||
err := json.Unmarshal(r.Stdout, target) | ||
assert.Success(t, "json unmarshals", err) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"go.coder.com/cli" | ||
"go.coder.com/flog" | ||
) | ||
|
||
type usersCmd struct { | ||
} | ||
|
||
func (cmd usersCmd) Spec() cli.CommandSpec { | ||
return cli.CommandSpec{ | ||
Name: "users", | ||
Usage: "[subcommand] <flags>", | ||
Desc: "interact with user accounts", | ||
} | ||
} | ||
|
||
func (cmd usersCmd) Run(fl *pflag.FlagSet) { | ||
exitUsage(fl) | ||
} | ||
|
||
func (cmd *usersCmd) Subcommands() []cli.Command { | ||
return []cli.Command{ | ||
&listCmd{}, | ||
} | ||
} | ||
|
||
type listCmd struct { | ||
outputFmt string | ||
} | ||
|
||
func tabDelimited(data interface{}) string { | ||
v := reflect.ValueOf(data) | ||
s := &strings.Builder{} | ||
for i := 0; i < v.NumField(); i++ { | ||
s.WriteString(fmt.Sprintf("%s\t", v.Field(i).Interface())) | ||
} | ||
return s.String() | ||
} | ||
|
||
func (cmd *listCmd) Run(fl *pflag.FlagSet) { | ||
entClient := requireAuth() | ||
|
||
users, err := entClient.Users() | ||
if err != nil { | ||
flog.Fatal("failed to get users: %v", err) | ||
} | ||
|
||
switch cmd.outputFmt { | ||
case "human": | ||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) | ||
for _, u := range users { | ||
_, err = fmt.Fprintln(w, tabDelimited(u)) | ||
if err != nil { | ||
flog.Fatal("failed to write: %v", err) | ||
} | ||
} | ||
err = w.Flush() | ||
if err != nil { | ||
flog.Fatal("failed to flush writer: %v", err) | ||
} | ||
case "json": | ||
err = json.NewEncoder(os.Stdout).Encode(users) | ||
if err != nil { | ||
flog.Fatal("failed to encode users to json: %v", err) | ||
} | ||
default: | ||
exitUsage(fl) | ||
} | ||
|
||
} | ||
|
||
func (cmd *listCmd) RegisterFlags(fl *pflag.FlagSet) { | ||
fl.StringVarP(&cmd.outputFmt, "output", "o", "human", "output format (human | json)") | ||
} | ||
|
||
func (cmd *listCmd) Spec() cli.CommandSpec { | ||
return cli.CommandSpec{ | ||
Name: "ls", | ||
Usage: "<flags>", | ||
Desc: "list all users", | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package entclient | ||
|
||
// Users gets the list of user accounts | ||
func (c Client) Users() ([]User, error) { | ||
var u []User | ||
err := c.requestBody("GET", "/api/users", nil, &u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return u, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.