Skip to content

Commit

Permalink
Add the conditional prefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
Chikashi-Kato committed May 20, 2016
2 parents 6e44bc3 + ed63f55 commit 7e9272c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 22 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ usage: slacktee.sh [options]
-a, --attachment [color] Use attachment (richly-formatted message)
Color can be 'good','warning','danger' or any hex color code (eg. #439FE0)
See https://api.slack.com/docs/attachments for more details.
-o, --cond-color color pattern Change the attachment color if the specified Regex pattern is found in the input.
You can specify this multile times.
If more than one pattern matches, the latest matched pattern is used.
-e, --field title value Add a field to the attachment. You can specify this multiple times.
-s, --short-field title value Add a short field to the attachment. You can specify this multiple times.
-o, --cond-color color pattern Change the attachment color if the specified Regex pattern matches the input.
You can specify this multile times.
If more than one pattern matches, the latest matched pattern is used.
-d, --cond-prefix prefix pattern This prefix is added to the message, if the specified Regex pattern matches the input.
You can specify this multile times.
If more than one pattern matches, the latest matched pattern is used.
--config config_file Specify the location of the config file.
--setup Setup slacktee interactively.
```
Expand Down Expand Up @@ -127,12 +130,13 @@ Direct message to your teammate 'chuck'? Easy!
echo "Submit Your Expense Reimbursement Form By Friday!" | slacktee.sh -c "@chuck"
```

Conditional coloring helps you to notice important messages easier.
If a specified Regex pattern matches the input, its corresponding color is used for posting the message. In the example below, the message color is green (good) by default, but the color becomes yellow (warning) if an input log starts with "Warning:". Also, it becomes red (danger) if the log starts with "Error:".
Conditional coloring and prefix helps you to notice important messages easier.
If a specified Regex pattern matches the input, its corresponding color or prefix is used for posting the message. In the example below, the message color is green (good) by default, but the color becomes yellow (warning) if an input log starts with "Warning:". Also, it becomes red (danger) and the prefix `@channel` is added to the mssage if the log starts with "Error:".
It's pretty useful, isn't it?

```
tail app.log | slacktee.sh -n -a "good" -o "warning" "^Warning:" -o "danger" "^Error:"
# To enable @command, '-m link_names' must be specified
tail -f app.log | slacktee.sh -n -a "good" -o "warning" "^Warning:" -o "danger" "^Error:" -d "@channel" "^Error:" -m link_names
```

You can find more examples on [Course Hero blog](http://www.coursehero.com/blog/2015/04/09/why-we-built-slacktee-a-custom-slack-integration/).
Expand Down
97 changes: 82 additions & 15 deletions slacktee.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ textWrapper="\`\`\`"
parseMode=""
fields=()
# Since bash 3 doesn't support the associative array, we store colors and patterns separately
cond_colors=()
cond_patterns=()
cond_color_colors=()
cond_color_patterns=()
found_pattern_color=""
# This color is used when 'attachment' is used without color specification
internal_default_color="#C0C0C0"

# Since bash 3 doesn't support the associative array, we store prefixes and patterns separately
cond_prefix_prefixes=()
cond_prefix_patterns=()
found_title_prefix=""

function show_help()
{
echo "usage: $me [options]"
Expand All @@ -47,11 +52,14 @@ function show_help()
echo " -a, --attachment [color] Use attachment (richly-formatted message)"
echo " Color can be 'good','warning','danger' or any hex color code (eg. #439FE0)"
echo " See https://api.slack.com/docs/attachments for more details."
echo " -o, --cond-color color pattern Change the attachment color if the specified Regex pattern is found in the input."
echo " You can specify this multile times."
echo " If more than one pattern matches, the latest matched pattern is used."
echo " -e, --field title value Add a field to the attachment. You can specify this multiple times."
echo " -s, --short-field title value Add a short field to the attachment. You can specify this multiple times."
echo " -o, --cond-color color pattern Change the attachment color if the specified Regex pattern matches the input."
echo " You can specify this multile times."
echo " If more than one pattern matches, the latest matched pattern is used."
echo " -d, --cond-prefix prefix pattern This prefix is added to the message, if the specified Regex pattern matches the input."
echo " You can specify this multile times."
echo " If more than one pattern matches, the latest matched pattern is used."
echo " --config config_file Specify the location of the config file."
echo " --setup Setup slacktee interactively."
}
Expand All @@ -61,6 +69,14 @@ function show_help()
function send_message()
{
message="$1"

# Prepend the prefix to the message, if it's set
if [[ -z $attachment && -n $found_pattern_prefix ]]; then
message="$found_pattern_prefix$message"
# Clear conditional prefix for the nest send
found_pattern_prefix=""
fi

escaped_message=$(echo "$textWrapper\n$message\n$textWrapper" | sed 's/"/\\"/g' | sed "s/'/\\'/g" )
message_attr=""
if [[ $message != "" ]]; then
Expand All @@ -76,6 +92,12 @@ function send_message()

message_attr="\"attachments\": [{ \"color\": \"$message_color\", \"mrkdwn_in\": [\"text\", \"fields\"], \"text\": \"$escaped_message\" "

if [[ -n $found_pattern_prefix ]]; then
title="$found_pattern_prefix $title"
# Clear conditional prefix for the nest send
found_pattern_prefix=""
fi

if [[ -n $title ]]; then
message_attr="$message_attr, \"title\": \"$title\" "
fi
Expand Down Expand Up @@ -128,10 +150,27 @@ function process_line()

# Check the patterns of the conditional colors
# If more than one pattern matches, the latest pattern is used
if [[ ${#cond_patterns[@]} != 0 ]]; then
for i in "${!cond_patterns[@]}"; do
if [[ $line =~ ${cond_patterns[$i]} ]]; then
found_pattern_color=${cond_colors[$i]}
if [[ ${#cond_color_patterns[@]} != 0 ]]; then
for i in "${!cond_color_patterns[@]}"; do
if [[ $line =~ ${cond_color_patterns[$i]} ]]; then
found_pattern_color=${cond_color_colors[$i]}
fi
done
fi

# Check the patterns of the conditional titles
# If more than one pattern matches, the latest pattern is used
if [[ ${#cond_prefix_patterns[@]} != 0 ]]; then
for i in "${!cond_prefix_patterns[@]}"; do
if [[ $line =~ ${cond_prefix_patterns[$i]} ]]; then
found_pattern_prefix=${cond_prefix_prefixes[$i]}
if [[ -n $attachment || $mode != "no-buffering" ]]; then
# Append a line break to the prefix for better formatting
found_pattern_prefix="$found_pattern_prefix\n"
else
# Append a space to the prefix for better formatting
found_pattern_prefix="$found_pattern_prefix "
fi
fi
done
fi
Expand Down Expand Up @@ -261,6 +300,34 @@ while [[ $# -gt 0 ]]; do
title="$1"
shift
;;
-d|--cond-prefix)
case "$1" in
-*|'')
# Found next command line option or empty. Error.
echo "a prefix of the conditional title was not specified"
show_help
exit 1
;;
*)
# Prefix should be found
case "$2" in
-*|'')
# Found next command line option or empty. Error.
echo "a pattern of the conditional title was not specified"
show_help
exit 1
;;
*)
# Set the prefix and the pattern to arrays
cond_prefix_prefixes+=("$1")
cond_prefix_patterns+=("$2")
shift
shift
;;
esac
;;
esac
;;
-m|--message-formatting)
case "$1" in
none)
Expand Down Expand Up @@ -306,7 +373,7 @@ while [[ $# -gt 0 ]]; do
case "$1" in
-*|'')
# Found next command line option or empty. Error.
echo "conditional color was not specified"
echo "a color of the conditional color was not specified"
show_help
exit 1
;;
Expand All @@ -315,14 +382,14 @@ while [[ $# -gt 0 ]]; do
case "$2" in
-*|'')
# Found next command line option or empty. Error.
echo "conditional color pattern was not specified"
echo "a pattern of the conditional color was not specified"
show_help
exit 1
;;
*)
# Set the color and the pattern to arrays
cond_colors+=("$1")
cond_patterns+=("$2")
cond_color_colors+=("$1")
cond_color_patterns+=("$2")
shift
shift
;;
Expand Down Expand Up @@ -424,8 +491,8 @@ if [[ "$opt_attachment" != "" ]]; then
attachment="$opt_attachment"
fi

# Set the default color to attachment if it's still empty and the length of the cond_patterns is not 0
if [[ -z $attachment ]] && [[ ${#cond_patterns[@]} != 0 ]]; then
# Set the default color to attachment if it's still empty and the length of the cond_color_patterns is not 0
if [[ -z $attachment ]] && [[ ${#cond_color_patterns[@]} != 0 ]]; then
attachment="$internal_default_color"
fi

Expand Down
14 changes: 13 additions & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,19 @@ cat $DATA | $SLACKTEE '-n' '-t' 'Conditional Coloring (-o) - Multiple matches in
echo "-- Conditional Coloring (-o) - Multiple matches in file mode"
cat $DATA | $SLACKTEE '-f' '-t' 'Conditional Coloring (-o) - Multiple matches in file mode. This should be colored red (danger).' '-o' 'good' '^1st' '-o' 'warning' '2nd' '-o' 'danger' '3rd'

# Test 15: Check exit code
# Test 15: Conditional prefix
echo "-- Conditional prefix (-d) in buffering mode --"
cat $DATA | $SLACKTEE '-t' 'Conditional prefix (-d) in buffering mode' '-d' '[Matched]' '2nd'
echo "-- Conditional prefix (--cond-prefix) in buffering mode --"
cat $DATA | $SLACKTEE '-t' 'Conditional prefix (--cond-prefix) in buffering mode' '--cond-prefix' '[Matched]' '2nd'
echo "-- Conditional prefix (-d) in no-buffering mode - Prefix should be added to 2nd line --"
cat $DATA | $SLACKTEE '-t' 'Conditional prefix (-d) in no-buffering mode' '-d' '[Matched]' '2nd' '-n'
echo "-- Conditional prefix (-d) in file mode --"
cat $DATA | $SLACKTEE '-t' 'Conditional prefix (-d) in file mode' '-d' '[Matched]' '2nd' '-f'
echo "-- Conditional prefix (-d) with attachment --"
cat $DATA | $SLACKTEE '-t' 'Conditional prefix (-d) with attachment' '-d' '[Matched]' '2nd' '-a' 'good'

# Test 16: Check exit code
echo "-- Check exit code : Success 0 --"
echo "Check if the exit code is 0" | $SLACKTEE ; echo $?
echo "-- Check exit code : Failure 1 --"
Expand Down

0 comments on commit 7e9272c

Please sign in to comment.