Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: marceloboeira/confirm-before
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.1.0
Choose a base ref
...
head repository: marceloboeira/confirm-before
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 8 commits
  • 5 files changed
  • 1 contributor

Commits on Feb 15, 2018

  1. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    4691f53 View commit details
  2. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    ad308c2 View commit details
  3. Answer the question on the same line

    There is a small hack, we need to flush stdout since its usually updated
    based on newline frequency.
    marceloboeira committed Feb 15, 2018

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    a770865 View commit details
  4. Fix delayed commands issue

    Instead of spawning the command (which 'fails' for long running commands),
    wait for the command but pipe its stdout to the shell.
    
    If not, the bin finishes before the spawned command, which leads to a
    weird shell behavior, printing stuff after finishing.
    marceloboeira committed Feb 15, 2018

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    c23bf3c View commit details
  5. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    fb5293b View commit details
  6. Remove redundant test

    marceloboeira committed Feb 15, 2018

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    5c1c7ba View commit details

Commits on Feb 16, 2018

  1. Pipe stderr too

    marceloboeira committed Feb 16, 2018

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    bce848f View commit details

Commits on Jul 25, 2018

  1. Fix #3 by printing to tty and not stdout

    When you do something like
    
    >  confirm-before echo "fooo" | grep foo
    
    Nothing was happening (apparently).
    
    Under the hood the grep commands consumes the stdout (where we print the
    "Are you sure?(y/n)"), then it keeps waiting for the stdin flush.
    
    Therefore, we need to write to tty instead of writing to stdout, otherwise
    grep suppress the output... it waits silently for the stdin input.
    
    We use termion to do that, simple and works fine.
    
    Unfortunately, I couldn't find an easy way to test for that... I accept
    suggestions.
    marceloboeira committed Jul 25, 2018

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    372415f View commit details
Showing with 87 additions and 12 deletions.
  1. +36 −0 Cargo.lock
  2. +3 −0 Cargo.toml
  3. +27 −0 README.md
  4. +15 −6 src/main.rs
  5. +6 −6 tests/integration.sh
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -2,3 +2,6 @@
name = "confirm-before"
version = "0.1.0"
authors = ["Marcelo Boeira <me@marceloboeira.com>"]

[dependencies]
termion = "1"
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,13 @@
<p align="center">Sanity check for your shell commands<p>
</p>

## Installing

```
brew tap marceloboeira/formulas
brew install marceloboeira/formulas/confirm-before
```

## Motivation

I have recently started working with a lot of DevOps CLIs that interact with staging and production servers.
@@ -27,3 +34,23 @@ Then, ...
$ kp apply -F *
Are you sure? (y/n)
```

## Future

I might create other sanity check levels/modes:

```
$ alias dangerous=`confirm-before --math dangerous --production`
$ dangerous get foo
How much is 19 - 3?
```

Conditionals, because it can become very annoying to confirm read-only commands...

```
$ alias dangerous=`confirm-before --matching delete|d|rm|apply dangerous --production`
$ dangerous delete foo
Are you sure? (y/n)
...
$ dangerous get foo -> doesn't match the regexp
```
21 changes: 15 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
use std::io;
use std::io::{self, Write};
use std::env;
use std::process::Command;
use std::process::{Command, Stdio};

extern crate termion;

fn main() {
// Use the tty to prevent writing to stdout
let mut tty = termion::get_tty().unwrap();

// Collect the command from the input
let args: Vec<String> = env::args().collect();
let command: String = args.iter()
.skip(1)
.fold(String::new(), |acc, item| { acc + " " + item });

// Confirm
println!("Are you sure? (y/n)");
// Confirm message
writeln!(tty, "Are you sure? (y/n)").unwrap();
tty.flush().unwrap();

let mut answer = String::new();
match io::stdin().read_line(&mut answer) {
Ok(_) => {
if answer == "y\n" {
Command::new("sh")
.arg("-c")
.arg(command)
.spawn()
.expect("foo");
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.expect("Something went wrong");
}
else {
println!("Okay, not running!")
12 changes: 6 additions & 6 deletions tests/integration.sh
Original file line number Diff line number Diff line change
@@ -3,13 +3,13 @@
printf "Running 'confirm-before' tests: \n"

printf " when a 'y' is given it runs the command - "
echo "y\n" | confirm-before echo result | grep -q result; if [ $? != 0 ]; then echo "FAIL"; exit 1; else echo "PASS"; fi
echo "y\n" | confirm-before 'echo result' | grep -q result; if [ $? != 0 ]; then echo "FAIL"; exit 1; else echo "PASS"; fi

printf " when a 'n' is given it does not run the command - "
echo "n\n" | confirm-before echo result | grep -q result; if [ $? != 0 ]; then echo "PASS"; else echo "FAIL"; exit 1; fi
echo "n\n" | confirm-before 'echo result' | grep -q result; if [ $? != 0 ]; then echo "PASS"; else echo "FAIL"; exit 1; fi

printf " when a 'p' is given it does not run the command - "
echo "p\n" | confirm-before echo result | grep -q result; if [ $? != 0 ]; then echo "PASS"; else echo "FAIL"; exit 1; fi
printf " when a complex command is given it gets the output - "
echo "y\n" | confirm-before 'cat ./tests/resource.txt' | grep -q content; if [ $? != 0 ]; then echo "FAIL"; exit 1; else echo "PASS"; fi

printf " when a complex command is given - "
echo "y\n" | confirm-before cat ./tests/resource.txt | grep -q content; if [ $? != 0 ]; then echo "FAIL"; exit 1; else echo "PASS"; fi
printf " when a slow command is given it waits for it to finish - "
echo "y\n" | confirm-before 'sleep 1; echo content' | grep -q content; if [ $? != 0 ]; then echo "FAIL"; exit 1; else echo "PASS"; fi