Skip to content

Commit

Permalink
Changed user struct to hold a slice of groups too.
Browse files Browse the repository at this point in the history
Groups that the user is member of now shows in the detail view.
Styles still need a lot of work.
  • Loading branch information
ariasmn committed Jul 18, 2022
1 parent 5be54b5 commit a82e41b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
6 changes: 3 additions & 3 deletions internal/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

type item userparser.User

func (i item) Title() string { return i.Username }
func (i item) Description() string { return fmt.Sprintf("UID: %s, GID: %s", i.Uid, i.Gid) }
func (i item) FilterValue() string { return i.Username }
func (i item) Title() string { return i.Details.Username }
func (i item) Description() string { return fmt.Sprintf("UID: %s, GID: %s", i.Details.Uid, i.Details.Gid) }
func (i item) FilterValue() string { return i.Details.Username }

type BubbleUser struct {
list list.Model
Expand Down
4 changes: 3 additions & 1 deletion internal/tui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import "github.com/charmbracelet/lipgloss"

var (
listStyle = lipgloss.NewStyle().
Width(28).
Width(35).
PaddingRight(3).
MarginRight(3).
Border(lipgloss.RoundedBorder(), false, true, false, false)
detailStyle = lipgloss.NewStyle().
PaddingTop(2)
dividerStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#9B9B9B", Dark: "#5C5C5C"})
)
19 changes: 14 additions & 5 deletions internal/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@ func (bu BubbleUser) detailView() string {
divider := dividerStyle.Render(strings.Repeat("-", bu.viewport.Width)) + "\n"

if it := bu.list.SelectedItem(); it != nil {
username := fmt.Sprintf("Username: %s\n", it.(item).Username)
fullname := fmt.Sprintf("Fullname: %s\n", it.(item).Name)
identificators := fmt.Sprintf("UID: %s, GID: %s\n", it.(item).Uid, it.(item).Gid)
homeDirectory := fmt.Sprintf("Home directory: %s\n", it.(item).HomeDir)
username := fmt.Sprintf("Username: %s\n", it.(item).Details.Username)
fullname := fmt.Sprintf("Fullname: %s\n", it.(item).Details.Name)
identificators := fmt.Sprintf("UID: %s, GID: %s\n", it.(item).Details.Uid, it.(item).Details.Gid)
homeDirectory := fmt.Sprintf("Home directory: %s\n", it.(item).Details.HomeDir)

builder.WriteString(username)
builder.WriteString(fullname)
builder.WriteString(identificators)
builder.WriteString(homeDirectory)

builder.WriteString(divider)

builder.WriteString("Member of: \n")
for _, group := range it.(item).Groups {
builder.WriteString(fmt.Sprintf("GID: %s --- Name: %s\n", group.Gid, group.Name))
}

}

details := wordwrap.String(builder.String(), bu.viewport.Width)

return wordwrap.String(builder.String(), bu.viewport.Width)
return detailStyle.Render(details)
}

func (bu BubbleUser) View() string {
Expand Down
38 changes: 30 additions & 8 deletions userparser/userparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"strings"
)

type User = user.User
type User struct {
Details user.User
Groups []*user.Group
}

var parsedUsers []User

Expand Down Expand Up @@ -52,11 +55,30 @@ func parseLine(line string) (User, error) {
//Parse the GECOS field
gecos := strings.Split(fs[4], ",")

return User{
Uid: fs[2],
Gid: fs[3],
Username: fs[0],
Name: gecos[0],
HomeDir: fs[5],
}, nil
user := User{}
user.Details.Uid = fs[2]
user.Details.Gid = fs[3]
user.Details.Username = fs[0]
user.Details.Name = gecos[0]
user.Details.HomeDir = fs[5]
user.Groups = parseGroups(user.Details)

return user, nil
}

func parseGroups(currentUser user.User) ([]*user.Group) {
groupsIds, err:= currentUser.GroupIds()
if err != nil {
fmt.Errorf("%v", err)
}

groups := []*user.Group{}

for _, groupId := range groupsIds {
foundGroup, _ := user.LookupGroupId(groupId)

groups = append(groups, foundGroup)
}

return groups
}

0 comments on commit a82e41b

Please sign in to comment.