Skip to content

Instantly share code, notes, and snippets.

@superboum
Last active February 19, 2019 13:39
Show Gist options
  • Save superboum/aacc8bd880435f9760c4940e30041a05 to your computer and use it in GitHub Desktop.
Save superboum/aacc8bd880435f9760c4940e30041a05 to your computer and use it in GitHub Desktop.
CoffeeSV

Install

On your machine :

wget https://gist.githubusercontent.com/superboum/aacc8bd880435f9760c4940e30041a05/raw/f1be7f8bfd8ca227f408dbafac6727e67d21e661/coffeesv
chmod +x coffeesv
sudo mv coffeesv /usr/local/bin
coffeesv Quentin
#!/bin/bash
CODE=`cat <<'EOF'
'
use strict;
use warnings;
print("coffeecsv\n");
binmode(STDOUT, ":utf8");
my $myname = $ARGV[0];
my $mypos = -1;
my $filename = "/opt/coffee/coffee.csv";
open(my $fh, "<:encoding(UTF-8)", $filename) or die "Could not open file $filename $!";
my $raw_headers = <$fh>;
chomp ($raw_headers);
my @headers = split(/,/,$raw_headers);
while (my ($i, $el) = each @headers) {
if ($myname eq $el) {
$mypos = $i;
}
}
if ($mypos == -1) {
print "User $myname not found\n";
exit 1;
}
my @data = ();
my $dirty = 0;
while (my $row = <$fh>) {
chomp($row);
my @values = split(/,/, $row);
if (!$values[$mypos]) {
continue;
}
if ($values[$mypos] eq "None") {
print("Enter rating (light, medium, strong, etc.) for coffee $values[1] $values[2]: ");
my $rating = <STDIN>;
chomp($rating);
$values[$mypos] = $rating;
$dirty = $dirty + 1;
}
my $raw_value = join(",", @values);
push(@data, "$raw_value\n");
}
close($fh);
if ($dirty <= 0) {
print("You have rated all your coffees!\n");
exit;
}
print("Do you want to save $dirty modifications? [y/N]: ");
my $answer = <STDIN>;
chomp($answer);
if ($answer ne "y" && $answer ne "yes") {
print("Modifications will not be saved\n");
exit 1;
}
open(my $fh2, ">:utf8", $filename) or die "Could not open file $filename $!";
print $fh2 "$raw_headers\n";
while (my ($i, $el) = each @data) {
print $fh2 $el;
}
close($fh2);
'
EOF
`
ssh -t widenclave2 perl -CA -E $CODE $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment