(requester Requester)
| 269 | } |
| 270 | |
| 271 | func (c *Client) GetSpacedClient(requester Requester) (*octopusApiClient.Client, error) { |
| 272 | if c.SpaceScopedClient != nil { |
| 273 | return c.SpaceScopedClient, nil |
| 274 | } |
| 275 | |
| 276 | // logic here is a bit fiddly: |
| 277 | // We could have been given either a space name, or a space ID, so we need to use the SystemClient to go look it up. |
| 278 | systemClient, err := c.GetSystemClient(requester) |
| 279 | if err != nil { |
| 280 | return nil, err |
| 281 | } |
| 282 | |
| 283 | // if the caller has not specified a space, prompt interactively |
| 284 | var foundSpaceID string |
| 285 | // if c.Ask is nil it means we're in automation mode. |
| 286 | if c.SpaceNameOrID == "" { |
| 287 | if !c.Ask.IsInteractive() { |
| 288 | return nil, errors.New("space must be specified when not running interactively; please set the OCTOPUS_SPACE environment variable or specify --space on the command line") |
| 289 | } |
| 290 | |
| 291 | allSpaces, err := systemClient.Spaces.GetAll() |
| 292 | if err != nil { |
| 293 | return nil, err |
| 294 | } |
| 295 | |
| 296 | switch len(allSpaces) { |
| 297 | case 0: |
| 298 | return nil, errors.New("no spaces found") |
| 299 | case 1: |
| 300 | selectedSpace := allSpaces[0] |
| 301 | c.ActiveSpace = selectedSpace |
| 302 | c.SpaceNameOrID = selectedSpace.ID |
| 303 | foundSpaceID = selectedSpace.ID |
| 304 | default: |
| 305 | selectedSpace, err := question.SelectMap( |
| 306 | c.Ask.Ask, |
| 307 | "You have not specified a Space. Please select one:", allSpaces, func(item *spaces.Space) string { return item.GetName() }) |
| 308 | |
| 309 | if err != nil { |
| 310 | return nil, err |
| 311 | } |
| 312 | c.ActiveSpace = selectedSpace |
| 313 | c.SpaceNameOrID = selectedSpace.ID |
| 314 | foundSpaceID = selectedSpace.ID |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if foundSpaceID == "" { |
| 319 | // https://github.com/OctopusDeploy/cli/issues/30 |
| 320 | // we prefer to match on Name first, and then fallback to ID; The server doesn't have direct support |
| 321 | // for that logic so the most pragmatic way to achieve that is to iterate the list of spaces client-side |
| 322 | allSpaces, err := systemClient.Spaces.GetAll() |
| 323 | if err != nil { |
| 324 | return nil, fmt.Errorf("cannot load spaces. Error: %v", err) |
| 325 | } |
| 326 | |
| 327 | var foundSpace *spaces.Space = nil |
| 328 | var foundSpaceByID *spaces.Space = nil // second-tier match, only use this if foundSpace is nilt |
nothing calls this directly
no test coverage detected