()
| 174 | } |
| 175 | |
| 176 | func (r *RootCmd) unshareWorkspace() *serpent.Command { |
| 177 | var ( |
| 178 | users []string |
| 179 | groups []string |
| 180 | ) |
| 181 | |
| 182 | cmd := &serpent.Command{ |
| 183 | Use: "remove <workspace> --user <user> --group <group>", |
| 184 | Aliases: []string{"unshare"}, |
| 185 | Short: "Remove shared access for users or groups from a workspace.", |
| 186 | Options: serpent.OptionSet{ |
| 187 | { |
| 188 | Name: "user", |
| 189 | Description: "A comma separated list of users to share the workspace with.", |
| 190 | Flag: "user", |
| 191 | Value: serpent.StringArrayOf(&users), |
| 192 | }, { |
| 193 | Name: "group", |
| 194 | Description: "A comma separated list of groups to share the workspace with.", |
| 195 | Flag: "group", |
| 196 | Value: serpent.StringArrayOf(&groups), |
| 197 | }, |
| 198 | }, |
| 199 | Middleware: serpent.Chain( |
| 200 | serpent.RequireNArgs(1), |
| 201 | ), |
| 202 | Handler: func(inv *serpent.Invocation) error { |
| 203 | if len(users) == 0 && len(groups) == 0 { |
| 204 | return xerrors.New("at least one user or group must be provided") |
| 205 | } |
| 206 | client, err := r.InitClient(inv) |
| 207 | if err != nil { |
| 208 | return err |
| 209 | } |
| 210 | |
| 211 | workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0]) |
| 212 | if err != nil { |
| 213 | return xerrors.Errorf("could not fetch the workspace %s: %w", inv.Args[0], err) |
| 214 | } |
| 215 | |
| 216 | userRoleStrings := make([][2]string, len(users)) |
| 217 | for index, user := range users { |
| 218 | if !codersdk.UsernameValidRegex.MatchString(user) { |
| 219 | return xerrors.Errorf("invalid username") |
| 220 | } |
| 221 | |
| 222 | userRoleStrings[index] = [2]string{user, ""} |
| 223 | } |
| 224 | |
| 225 | groupRoleStrings := make([][2]string, len(groups)) |
| 226 | for index, group := range groups { |
| 227 | if !codersdk.UsernameValidRegex.MatchString(group) { |
| 228 | return xerrors.Errorf("invalid group name") |
| 229 | } |
| 230 | |
| 231 | groupRoleStrings[index] = [2]string{group, ""} |
| 232 | } |
| 233 |
no test coverage detected