Commit 59433d7a authored by Jonathan Maw's avatar Jonathan Maw
Browse files

cli: Interactively warn if the user is trying to close the workspace they're...

cli: Interactively warn if the user is trying to close the workspace they're using to load the project

This involves changes in:
* _stream.py:
  * Add the helper Stream.workspace_is_required()
* cli.py:
  * If buildstream is invoked interactively, prompt the user to confirm
    that they want to close the workspace they're using to load this
    project.
parent 14405073
Loading
Loading
Loading
Loading
+8 −1
Original line number Original line Diff line number Diff line
@@ -726,11 +726,18 @@ def workspace_close(app, remove_dir, all_, elements):


        elements = app.stream.redirect_element_names(elements)
        elements = app.stream.redirect_element_names(elements)


        # Check that the workspaces in question exist
        # Check that the workspaces in question exist, and that it's safe to
        # remove them.
        nonexisting = []
        nonexisting = []
        for element_name in elements:
        for element_name in elements:
            if not app.stream.workspace_exists(element_name):
            if not app.stream.workspace_exists(element_name):
                nonexisting.append(element_name)
                nonexisting.append(element_name)
            if app.stream.workspace_is_required(element_name):
                if app.interactive:
                    click.echo("Removing '{}' will prevent you from running buildstream commands".format(element_name))
                    if not click.confirm('Are you sure you want to close this workspace?'):
                        click.echo('Aborting', err=True)
                        sys.exit(-1)
        if nonexisting:
        if nonexisting:
            raise AppError("Workspace does not exist", detail="\n".join(nonexisting))
            raise AppError("Workspace does not exist", detail="\n".join(nonexisting))


+14 −0
Original line number Original line Diff line number Diff line
@@ -643,6 +643,20 @@ class Stream():


        return False
        return False


    # workspace_is_required()
    #
    # Checks whether the workspace belonging to element_name is required to
    # load the project
    #
    # Args:
    #    element_name (str): The element whose workspace may be required
    #
    # Returns:
    #    (bool): True if the workspace is required
    def workspace_is_required(self, element_name):
        required_elm = self._project.required_workspace_element()
        return required_elm == element_name

    # workspace_list
    # workspace_list
    #
    #
    # Serializes the workspaces and dumps them in YAML to stdout.
    # Serializes the workspaces and dumps them in YAML to stdout.