(groupName string)
| 562 | } |
| 563 | |
| 564 | func removeCurrentUserFromGroup(groupName string) error { |
| 565 | currentUser, err := currentUserForGroup() |
| 566 | if err != nil { |
| 567 | return err |
| 568 | } |
| 569 | |
| 570 | groupExists := true |
| 571 | if _, err := lookupGroup(groupName); err != nil { |
| 572 | var unknownGroupError user.UnknownGroupError |
| 573 | if !errors.As(err, &unknownGroupError) { |
| 574 | return fmt.Errorf("failed to lookup %s group -> %w", groupName, err) |
| 575 | } |
| 576 | groupExists = false |
| 577 | } |
| 578 | if !groupExists { |
| 579 | return nil |
| 580 | } |
| 581 | |
| 582 | groups, err := cmd.NewExecutor("", "id", "-nG", currentUser).ExecuteOutput() |
| 583 | if err != nil { |
| 584 | return fmt.Errorf("failed to get groups for %q -> %w", currentUser, err) |
| 585 | } |
| 586 | userInGroup := slices.Contains(strings.Fields(groups), groupName) |
| 587 | if !userInGroup { |
| 588 | return nil |
| 589 | } |
| 590 | |
| 591 | if output, err := cmd.NewExecutor("", "gpasswd", "-d", currentUser, groupName).ExecuteOutput(); err != nil { |
| 592 | return fmt.Errorf("failed to remove %q from %q group -> %s -> %w", currentUser, groupName, output, err) |
| 593 | } |
| 594 | color.PrintHint("✔ remove %s from group %s", currentUser, groupName) |
| 595 | return nil |
| 596 | } |
| 597 | |
| 598 | func currentUserForGroup() (string, error) { |
| 599 | candidate := os.Getenv("SUDO_USER") |
no test coverage detected