convertToUserIDs accepts a list of users in the form of IDs or email addresses and translates any emails to the matching user ID.
(userList []string, users []codersdk.User)
| 126 | // convertToUserIDs accepts a list of users in the form of IDs or email addresses |
| 127 | // and translates any emails to the matching user ID. |
| 128 | func convertToUserIDs(userList []string, users []codersdk.User) ([]string, error) { |
| 129 | converted := make([]string, 0, len(userList)) |
| 130 | |
| 131 | for _, user := range userList { |
| 132 | if _, err := uuid.Parse(user); err == nil { |
| 133 | converted = append(converted, user) |
| 134 | continue |
| 135 | } |
| 136 | if _, err := mail.ParseAddress(user); err == nil { |
| 137 | for _, u := range users { |
| 138 | if u.Email == user { |
| 139 | converted = append(converted, u.ID.String()) |
| 140 | break |
| 141 | } |
| 142 | } |
| 143 | continue |
| 144 | } |
| 145 | |
| 146 | return nil, xerrors.Errorf("%q must be a valid UUID or email address", user) |
| 147 | } |
| 148 | |
| 149 | return converted, nil |
| 150 | } |