Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User all Flag #8

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions api/apiuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ import (

type UserResponse struct {
Data struct {
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
ID string `json:"id"`
Pin string `json:"pin"`
Subject struct {
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Role string `json:"role"`
IsStaff bool `json:"is_staff"`
IsSuperUser bool `json:"is_superuser"`
DateJoined string `json:"date_joined"`
ID string `json:"id"`
IsActive bool `json:"is_active"`
LastLogin string `json:"last_login"`
Pin string `json:"pin"`
Subject struct {
ID string `json:"id"`
} `json:"subject"`
} `json:"data"`
Expand Down
52 changes: 37 additions & 15 deletions cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/spf13/cobra"
)

var all bool

// ----------------------------------------------
// user command
// ----------------------------------------------
Expand All @@ -34,23 +36,42 @@ func user() {
}

if userResponse != nil {
data := []string{
userResponse.Data.Username,
userResponse.Data.Email,
userResponse.Data.FirstName,
userResponse.Data.LastName,
userResponse.Data.ID,
userResponse.Data.Pin,
userResponse.Data.Subject.ID,
}
switch {
case all:
allUserDataFmt := fmt.Sprintf("username: %s\nemail: %s\nfirst name: %s\nlast name: %s\nrole: %s\nis staff: %t\nis superuser: %t\ndate joined: %s\nid: %s\nisactive: %t\nlast login: %s\npin: %s\nsubject id: %s",
userResponse.Data.Username,
userResponse.Data.Email,
userResponse.Data.FirstName,
userResponse.Data.LastName,
userResponse.Data.Role,
userResponse.Data.IsStaff,
userResponse.Data.IsSuperUser,
userResponse.Data.DateJoined,
userResponse.Data.ID,
userResponse.Data.IsActive,
userResponse.Data.LastLogin,
userResponse.Data.Pin,
userResponse.Data.Subject.ID)
fmt.Println(allUserDataFmt)
default:
userData := []string{
userResponse.Data.Username,
userResponse.Data.Email,
userResponse.Data.FirstName,
userResponse.Data.LastName,
userResponse.Data.ID,
userResponse.Data.Pin,
userResponse.Data.Subject.ID,
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Username", "Email", "First Name", "Last Name", "ID", "Pin", "Subject ID"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.Append(data)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Username", "Email", "First Name", "Last Name", "ID", "Pin", "Subject ID"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.Append(userData)

table.Render()
table.Render()
}
}
}

Expand All @@ -60,4 +81,5 @@ func user() {

func init() {
rootCmd.AddCommand(userCmd)
userCmd.Flags().BoolVarP(&all, "all", "a", false, "list all user parameters")
}