Commit 6aba014d 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()
* userconfig.yaml:
  * Add a default value for prompt.really-workspace-close-project-inaccessible
* _context.py:
  * Load the prompt 'really-workspace-close-project-inaccessible' from
    user config.
* 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.

This is a part of #222
parent 6fd97acd
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -122,6 +122,10 @@ class Context():
        # remove a workspace directory.
        self.prompt_workspace_close_remove_dir = None

        # Boolean, whether we double-check with the user that they meant to
        # close the workspace when they're using it to access the project.
        self.prompt_workspace_close_project_inaccessible = None

        # Boolean, whether we double-check with the user that they meant to do
        # a hard reset of a workspace, potentially losing changes.
        self.prompt_workspace_reset_hard = None
@@ -251,12 +255,15 @@ class Context():
            defaults, Mapping, 'prompt')
        _yaml.node_validate(prompt, [
            'auto-init', 'really-workspace-close-remove-dir',
            'really-workspace-close-project-inaccessible',
            'really-workspace-reset-hard',
        ])
        self.prompt_auto_init = _node_get_option_str(
            prompt, 'auto-init', ['ask', 'no']) == 'ask'
        self.prompt_workspace_close_remove_dir = _node_get_option_str(
            prompt, 'really-workspace-close-remove-dir', ['ask', 'yes']) == 'ask'
        self.prompt_workspace_close_project_inaccessible = _node_get_option_str(
            prompt, 'really-workspace-close-project-inaccessible', ['ask', 'yes']) == 'ask'
        self.prompt_workspace_reset_hard = _node_get_option_str(
            prompt, 'really-workspace-reset-hard', ['ask', 'yes']) == 'ask'

+8 −1
Original line number Diff line number Diff line
@@ -763,11 +763,18 @@ def workspace_close(app, remove_dir, all_, 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 = []
        for element_name in elements:
            if not app.stream.workspace_exists(element_name):
                nonexisting.append(element_name)
            if (app.stream.workspace_is_required(element_name) and app.interactive and
                    app.context.prompt_workspace_close_project_inaccessible):
                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:
            raise AppError("Workspace does not exist", detail="\n".join(nonexisting))

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

        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
    #
    # Serializes the workspaces and dumps them in YAML to stdout.
+8 −0
Original line number Diff line number Diff line
@@ -128,6 +128,14 @@ prompt:
  #
  really-workspace-close-remove-dir: ask

  # Whether to really proceed with 'bst workspace close' when doing so would
  # stop them from running bst commands in this workspace.
  #
  #  ask - Ask the user if they are sure.
  #  yes - Always close, without asking.
  #
  really-workspace-close-project-inaccessible: ask

  # Whether to really proceed with 'bst workspace reset' doing a hard reset of
  # a workspace, potentially losing changes.
  #