Skip to content

Instantly share code, notes, and snippets.

@soyuka
Forked from Pierstoval/confirm.Makefile
Created March 3, 2021 11:24
Show Gist options
  • Select an option

  • Save soyuka/d00d769b1bc9b5e2499d9975165593b7 to your computer and use it in GitHub Desktop.

Select an option

Save soyuka/d00d769b1bc9b5e2499d9975165593b7 to your computer and use it in GitHub Desktop.

Revisions

  1. @Pierstoval Pierstoval revised this gist Mar 2, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    _TITLE := "\033[32m[%s]\033[0m %s\n" # Green text for "printf"
    _ERROR := "\033[31m[%s]\033[0m %s\n" # Red text for "printf"

    # To use the "confirm" target inside another target,
    # use the " if $(MAKE) -s confirm ; " syntax.
    @@ -24,6 +22,8 @@ confirm:
    fi \
    fi
    .PHONY: confirm
    _TITLE := "\033[32m[%s]\033[0m %s\n" # Green text for "printf"
    _ERROR := "\033[31m[%s]\033[0m %s\n" # Red text for "printf"

    # Notes:
    #
  2. @Pierstoval Pierstoval revised this gist Mar 2, 2021. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,14 @@
    _TITLE := "\033[32m[%s]\033[0m %s\n" # Green text for "printf"
    _ERROR := "\033[31m[%s]\033[0m %s\n" # Red text for "printf"

    # To use the "confirm" target inside another target,
    # use the " if $(MAKE) -s confirm ; " syntax.
    mycommand:
    @if $(MAKE) -s confirm ; then \
    execute_your_command_here ; \
    fi
    .PHONY: mycommand

    # The CI environment variable can be set to a non-empty string,
    # it'll bypass this command that will "return true", as a "yes" answer.
    confirm:
    @@ -14,14 +25,6 @@ confirm:
    fi
    .PHONY: confirm

    # To use the "confirm" target inside another target,
    # use the " if $(MAKE) -s confirm ; " syntax.
    mycommand:
    @if $(MAKE) -s confirm ; then \
    execute_your_command_here ; \
    fi
    .PHONY: mycommand

    # Notes:
    #
    # As we're using an "if" statement,
  3. @Pierstoval Pierstoval revised this gist Mar 2, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ confirm:
    # use the " if $(MAKE) -s confirm ; " syntax.
    mycommand:
    @if $(MAKE) -s confirm ; then \
    execute_your_command_here ; \
    execute_your_command_here ; \
    fi
    .PHONY: mycommand

  4. @Pierstoval Pierstoval created this gist Mar 2, 2021.
    37 changes: 37 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # The CI environment variable can be set to a non-empty string,
    # it'll bypass this command that will "return true", as a "yes" answer.
    confirm:
    @if [[ -z "$(CI)" ]]; then \
    REPLY="" ; \
    read -p "⚠ Are you sure? [y/n] > " -r ; \
    if [[ ! $$REPLY =~ ^[Yy]$$ ]]; then \
    printf $(_ERROR) "KO" "Stopping" ; \
    exit 1 ; \
    else \
    printf $(_TITLE) "OK" "Continuing" ; \
    exit 0; \
    fi \
    fi
    .PHONY: confirm

    # To use the "confirm" target inside another target,
    # use the " if $(MAKE) -s confirm ; " syntax.
    mycommand:
    @if $(MAKE) -s confirm ; then \
    execute_your_command_here ; \
    fi
    .PHONY: mycommand

    # Notes:
    #
    # As we're using an "if" statement,
    # we need to use ";" and "\" at the end of lines,
    # else make would consider each line to be a separate command to call,
    # which doesn't work.
    # Using ";" and "\" at the end of lines helps make
    # interpret this "if" as one single statement/command.
    #
    # You can replace ";" with "&&" if you need to stop the
    # execution process before running next commands.
    #
    #