Skip to content

Commit

Permalink
add logic to fetch the details of a selected user
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya18101 committed Nov 21, 2024
1 parent f3147e3 commit 616d8e0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 33 deletions.
24 changes: 23 additions & 1 deletion internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,28 @@ func searchUsersCmd(cli *cli) *cobra.Command {

cli.renderer.UserSearch(foundUsers)

if cli.renderer.ID != "" {
a := &management.User{ID: &cli.renderer.ID}

if err := ansi.Waiting(func() error {
var err error
a, err = cli.api.User.Read(cmd.Context(), cli.renderer.ID)
return err
}); err != nil {
return fmt.Errorf("failed to load user with ID %q: %w", cli.renderer.ID, err)
}

// Get the current connection.
conn := stringSliceToCommaSeparatedString(cli.getUserConnection(a))
a.Connection = auth0.String(conn)

// Parse the connection name to get the requireUsername status.
u := cli.getConnReqUsername(cmd.Context(), auth0.StringValue(a.Connection))
requireUsername := auth0.BoolValue(u)
cli.renderer.Format = "json"
cli.renderer.UserShow(a, requireUsername)
}

return nil
},
}
Expand Down Expand Up @@ -483,6 +505,7 @@ func showUserCmd(cli *cli) *cobra.Command {
requireUsername := auth0.BoolValue(u)

cli.renderer.UserShow(a, requireUsername)

return nil
},
}
Expand Down Expand Up @@ -887,7 +910,6 @@ func (c *cli) getConnReqUsername(ctx context.Context, s string) *bool {
if err := json.Unmarshal([]byte(res), &opts); err != nil {
fmt.Println(err)
}

return opts.RequiresUsername
}

Expand Down
89 changes: 57 additions & 32 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/manifoldco/promptui"
"io"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -40,6 +41,8 @@ type Renderer struct {

// Format indicates how the results are rendered. Default (empty) will write as table.
Format OutputFormat

ID string
}

type View interface {
Expand Down Expand Up @@ -168,10 +171,59 @@ func (r *Renderer) Results(data []View) {
rows = append(rows, d.AsTableRow())
}

writeTable(data[0].AsTableHeader(), rows)
buffer := &bytes.Buffer{}
writeTable(buffer, data[0].AsTableHeader(), rows)

if len(data) < 25 {
// Split the rendered table into rows
rows := bytes.Split(buffer.Bytes(), []byte("\n"))

// Convert rows to a list of strings and remove empty rows
var formattedRows []string
for _, row := range rows {
if len(row) > 0 {
formattedRows = append(formattedRows, string(row))
}
}

// Use the rows as prompt items
prompt := promptui.Select{
Label: "Select a User",
Items: formattedRows[1:], // Skip the header row for selection
}

// Run the prompt
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed: %v\n", err)
return
}

r.ID, err = fetchId(result)
if err != nil {
return
}
} else {
r.ResultWriter = buffer
}

}

Check failure on line 210 in internal/display/display.go

View workflow job for this annotation

GitHub Actions / Checks

unnecessary trailing newline (whitespace)
}

func fetchId(inputString string) (string, error) {

Check failure on line 213 in internal/display/display.go

View workflow job for this annotation

GitHub Actions / Checks

var-naming: func fetchId should be fetchID (revive)
regex := regexp.MustCompile(`(sms|auth0|email)\|[a-zA-Z0-9]+`)

// Find the first match
match := regex.FindString(inputString)

// Check if a match was found
if match == "" {
return "", fmt.Errorf("no valid ID found in the input string")
}

return match, nil
}

func (r *Renderer) Result(data View) {
switch r.Format {
case OutputFormatJSON:
Expand All @@ -187,7 +239,8 @@ func (r *Renderer) Result(data View) {
v := pair[1]
kvs = append(kvs, []string{k, v})
}
writeTable(nil, kvs)
buffer := &bytes.Buffer{}
writeTable(buffer, nil, kvs)
}
}
}
Expand Down Expand Up @@ -257,9 +310,8 @@ func fprintfStr(w io.Writer, fmtStr string, argsStr ...string) {
fmt.Fprintf(w, fmtStr, args...)
}

func writeTable(header []string, data [][]string) {
var buffer bytes.Buffer
table := tablewriter.NewWriter(&buffer)
func writeTable(buffer *bytes.Buffer, header []string, data [][]string) {
table := tablewriter.NewWriter(buffer)

table.SetHeader(header)
table.SetAutoWrapText(false)
Expand All @@ -277,33 +329,6 @@ func writeTable(header []string, data [][]string) {
}
table.Render()

// Split the rendered table into rows
rows := bytes.Split(buffer.Bytes(), []byte("\n"))

// Convert rows to a list of strings and remove empty rows
var formattedRows []string
for _, row := range rows {
if len(row) > 0 {
formattedRows = append(formattedRows, string(row))
}
}

// Use the rows as prompt items
prompt := promptui.Select{
Label: "Select a User",
Items: formattedRows[1:], // Skip the header row for selection
}

// Run the prompt
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed: %v\n", err)
return
}

// Output the selected row
fmt.Printf("You selected: %s\n", result)

}

func writeCSV(w io.Writer, header []string, data [][]string) error {
Expand Down

0 comments on commit 616d8e0

Please sign in to comment.