-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
292 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 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,61 @@ | ||
package api | ||
|
||
import "github.com/shurcooL/graphql" | ||
|
||
type Users struct { | ||
client *Client | ||
} | ||
|
||
type User struct { | ||
Username string | ||
FullName string | ||
IsRoot bool | ||
CreatedAt string | ||
} | ||
|
||
type UserChangeSet struct { | ||
IsRoot *bool | ||
} | ||
|
||
func (c *Client) Users() *Users { return &Users{client: c} } | ||
|
||
func (c *Users) List() ([]User, error) { | ||
var q struct { | ||
Users []User `graphql:"accounts"` | ||
} | ||
|
||
variables := map[string]interface{}{} | ||
|
||
graphqlErr := c.client.Query(&q, variables) | ||
|
||
return q.Users, graphqlErr | ||
} | ||
|
||
func (c *Users) Get(username string) (User, error) { | ||
var q struct { | ||
User User `graphql:"account(username: $username)"` | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"username": graphql.String(username), | ||
} | ||
|
||
graphqlErr := c.client.Query(&q, variables) | ||
|
||
return q.User, graphqlErr | ||
} | ||
|
||
func (c *Users) Update(username string, changeset UserChangeSet) (User, error) { | ||
var mutation struct { | ||
Result struct{ User User } `graphql:"updateUser(input: {username: $username, isRoot: $isRoot})"` | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"username": graphql.String(username), | ||
"isRoot": optBoolArg(changeset.IsRoot), | ||
} | ||
|
||
graphqlErr := c.client.Mutate(&mutation, variables) | ||
|
||
return mutation.Result.User, graphqlErr | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -19,5 +19,5 @@ func yesNo(isTrue bool) string { | |
if isTrue { | ||
return "yes" | ||
} | ||
return "" | ||
return "no" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
package command | ||
|
||
import "strings" | ||
import ( | ||
"strings" | ||
|
||
type simpleAccount struct { | ||
Username string | ||
FullName string | ||
IsRoot bool | ||
CreatedAt string | ||
} | ||
"github.com/humio/cli/api" | ||
) | ||
|
||
func formatSimpleAccount(account simpleAccount) string { | ||
func formatSimpleAccount(account api.User) string { | ||
columns := []string{account.Username, account.FullName, yesNo(account.IsRoot), account.CreatedAt} | ||
return strings.Join(columns, " | ") | ||
} |
This file contains 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 |
---|---|---|
@@ -1,35 +1,76 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
|
||
cli "gopkg.in/urfave/cli.v2" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func UsersList(c *cli.Context) error { | ||
config, _ := getServerConfig(c) | ||
type UsersListCommand struct { | ||
Meta | ||
} | ||
|
||
func (f *UsersListCommand) Help() string { | ||
helpText := ` | ||
Usage: humio users list | ||
Lists all users. This command requires root permissions on your access token. | ||
To see members in a repository or view use: | ||
ensureToken(config) | ||
ensureURL(config) | ||
$ humio members list <repo> | ||
var q struct { | ||
Accounts []simpleAccount `graphql:"accounts"` | ||
General Options: | ||
` + generalOptionsUsage() + ` | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (f *UsersListCommand) Synopsis() string { | ||
return "List all user in the cluster." | ||
} | ||
|
||
func (f *UsersListCommand) Name() string { return "users list" } | ||
|
||
func (f *UsersListCommand) Run(args []string) int { | ||
flags := f.Meta.FlagSet(f.Name(), FlagSetClient) | ||
flags.Usage = func() { f.Ui.Output(f.Help()) } | ||
|
||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
// Check that we got one argument | ||
args = flags.Args() | ||
if l := len(args); l != 0 { | ||
f.Ui.Error("This command takes no arguments") | ||
f.Ui.Error(commandErrorText(f)) | ||
return 1 | ||
} | ||
|
||
// Get the HTTP client | ||
client, err := f.Meta.Client() | ||
if err != nil { | ||
f.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
variables := map[string]interface{}{} | ||
users, err := client.Users().List() | ||
|
||
graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables) | ||
check(graphqlErr) | ||
if err != nil { | ||
f.Ui.Error(fmt.Sprintf("Error fetching token list: %s", err)) | ||
return 1 | ||
} | ||
|
||
rows := make([]string, len(q.Accounts)) | ||
for i, account := range q.Accounts { | ||
rows[i] = formatSimpleAccount(account) | ||
rows := make([]string, len(users)) | ||
for i, user := range users { | ||
rows[i] = formatSimpleAccount(user) | ||
} | ||
|
||
printTable(append([]string{ | ||
"Username | Name | Root | Created"}, | ||
rows..., | ||
)) | ||
|
||
return nil | ||
return 0 | ||
} |
This file contains 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 |
---|---|---|
@@ -1,43 +1,81 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/shurcooL/graphql" | ||
cli "gopkg.in/urfave/cli.v2" | ||
"github.com/humio/cli/api" | ||
) | ||
|
||
func UsersShow(c *cli.Context) error { | ||
config, _ := getServerConfig(c) | ||
type UsersShowCommand struct { | ||
Meta | ||
} | ||
|
||
func (f *UsersShowCommand) Help() string { | ||
helpText := ` | ||
Usage: humio users show <username> | ||
Shows details about a users. This command requires root access. | ||
To see members in a repository or view use: | ||
$ humio members show <repo> <username> | ||
General Options: | ||
` + generalOptionsUsage() + ` | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (f *UsersShowCommand) Synopsis() string { | ||
return "Shows details about a user." | ||
} | ||
|
||
ensureToken(config) | ||
ensureURL(config) | ||
func (f *UsersShowCommand) Name() string { return "users show" } | ||
|
||
username := c.Args().First() | ||
func (f *UsersShowCommand) Run(args []string) int { | ||
flags := f.Meta.FlagSet(f.Name(), FlagSetClient) | ||
flags.Usage = func() { f.Ui.Output(f.Help()) } | ||
|
||
var q struct { | ||
Account struct { | ||
Username string | ||
FullName string | ||
IsRoot bool | ||
CreatedAt string | ||
} `graphql:"account(username: $username)"` | ||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"username": graphql.String(username), | ||
// Check that we got one argument | ||
args = flags.Args() | ||
if l := len(args); l != 1 { | ||
f.Ui.Error("This command takes one argument: <username>") | ||
f.Ui.Error(commandErrorText(f)) | ||
return 1 | ||
} | ||
|
||
graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables) | ||
check(graphqlErr) | ||
username := args[0] | ||
|
||
userData := []string{q.Account.Username, q.Account.FullName, q.Account.CreatedAt, yesNo(q.Account.IsRoot)} | ||
// Get the HTTP client | ||
client, err := f.Meta.Client() | ||
if err != nil { | ||
f.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
user, err := client.Users().Get(username) | ||
|
||
if err != nil { | ||
f.Ui.Error(fmt.Sprintf("Error fetching token list: %s", err)) | ||
return 1 | ||
} | ||
|
||
printUserTable(user) | ||
|
||
return 0 | ||
} | ||
|
||
func printUserTable(user api.User) { | ||
userData := []string{user.Username, user.FullName, user.CreatedAt, yesNo(user.IsRoot)} | ||
|
||
printTable([]string{ | ||
"Username | Name | Created At | Is Root", | ||
strings.Join(userData, "|"), | ||
}) | ||
|
||
return nil | ||
} |
Oops, something went wrong.