// selectWorkspace gets a list of existing workspaces and then checks
// if the currently selected workspace is valid. If not, it will ask
// the user to select a workspace from the list.
selectWorkspace gets a list of existing workspaces and then checks
if the currently selected workspace is valid. If not, it will ask
the user to select a workspace from the list.
// selectWorkspace gets a list of existing workspaces and then checks // if the currently selected workspace is valid . If not it will ask // the user to select a workspace from the list .
func (m *Meta) selectWorkspace(b backend.Backend) error {
	workspaces, err := b.Workspaces()
	if err == backend.ErrWorkspacesNotSupported {
		return nil
	}
	if err != nil {
		return fmt.Errorf("Failed to get existing workspaces: %s", err)
	}
	if len(workspaces) == 0 {
		return fmt.Errorf(strings.TrimSpace(errBackendNoExistingWorkspaces))
	}
	workspace, err := m.Workspace()
	if err != nil {
		return err
	}
	var list strings.Builder
	for i, w := range workspaces {
		if w == workspace {
			return nil
		}
		fmt.Fprintf(&list, "%d. %s\n", i+1, w)
	}
	v, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{
		Id: "select-workspace",
		Query: fmt.Sprintf(
			"\n[reset][bold][yellow]The currently selected workspace (%s) does not exist.[reset]",
			workspace),
		Description: fmt.Sprintf(
			strings.TrimSpace(inputBackendSelectWorkspace), list.String()),
	})
	if err != nil {
		return fmt.Errorf("Failed to select workspace: %s", err)
	}
	idx, err := strconv.Atoi(v)
	if err != nil || (idx < 1 || idx > len(workspaces)) {
		return fmt.Errorf("Failed to select workspace: input not a valid number")
	}
	return m.SetWorkspace(workspaces[idx-1])
}
func ( m * Meta ) selectWorkspace ( b backend . Backend ) error { workspaces , err := b . Workspaces ( ) if err == backend . ErrWorkspacesNotSupported { return nil } if err != nil { return fmt . Errorf ( " " , err ) } if len ( workspaces ) == 0 { return fmt . Errorf ( strings . TrimSpace ( errBackendNoExistingWorkspaces ) ) } workspace , err := m . Workspace ( ) if err != nil { return err } var list strings . Builder for i , w := range workspaces { if w == workspace { return nil } fmt . Fprintf ( & list , " \n " , i + 1 , w ) } v , err := m . UIInput ( ) . Input ( context . Background ( ) , & terraform . InputOpts { Id : " " , Query : fmt . Sprintf ( " \n " , workspace ) , Description : fmt . Sprintf ( strings . TrimSpace ( inputBackendSelectWorkspace ) , list . String ( ) ) , } ) if err != nil { return fmt . Errorf ( " " , err ) } idx , err := strconv . Atoi ( v ) if err != nil || ( idx < 1 || idx > len ( workspaces ) ) { return fmt . Errorf ( " " ) } return m . SetWorkspace ( workspaces [ idx - 1 ] ) }
selectWorkspace gets a list of existing workspaces and then checks
if the currently selected workspace is valid.
selectWorkspace gets a list of existing workspaces and then checks if the currently selected workspace is valid .
// Get the currently selected workspace. // Check if any of the existing workspaces matches the selected // workspace and create a numbered list of existing workspaces. // If the selected workspace doesn't exist, ask the user to select // a workspace from the list of existing workspaces.