source: trunk/cereal/fs/usr/bin/cereal @ 651

Last change on this file since 651 was 651, checked in by jrollins, 6 years ago

cereal: a bunch of clean up/simplification of error handling and list
function. added notes on log limits

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1#!/bin/bash
2
3# cereal: command line interface to cereal screen serial loggers
4#
5# The cereal scripts were written by
6# Jameson Rollins <jrollins@fifthhorseman.net>
7# and
8# Daniel Kahn Gillmor <dkg-debian.org@fifthhorseman.net>.
9#
10# They are Copyright 2007, and are all released under the GPL, version 3
11# or later.
12
13##################################################
14CMD=$(basename $0)
15
16SHAREDIR=${SHAREDIR:-"/usr/share/cereal"}
17export SHAREDIR
18source "$SHAREDIR/common"
19##################################################
20
21function usage {
22cat <<EOF
23Usage: $CMD <subcommand> [options] [args]
24Cereal client program.
25
26subcommands:
27  attach (a) SESSION                      attach to session
28  follow (f) [options] SESSION            follow session log
29    -c (--cat)                              cat log instead of follow
30    -p (--path)                             output just log file path
31  list (l) [SESSION]...                   list session(s)
32  help (h,?)                              this help
33
34EOF
35}
36
37attach() {
38    SESSION="$1"
39
40    if [ -z "$SESSION" ] ; then
41        failure "Not enough input arguments.  Type '$CMD help' for more info."
42    elif ! is_session "$SESSION" ; then
43        failure "Session '$SESSION' not found.
44Type 'cereal list' to list sessions."
45    elif ! is_running "$SESSION" ; then
46        failure "Session '$SESSION' not running."
47    elif ! can_attach "$SESSION" "$USER" ; then
48        failure "You are not allowed to attach to session '$SESSION'."
49    fi
50
51    log_write "$SESSION" "user '$USER' attaching to session..."
52    screen -r -S "cereal:$SESSION" || failure "Could not reattach screen."
53    log_write "$SESSION" "user '$USER' detached from session."
54}
55
56log_follow() {
57    tail -n 100 -F "$LOG"
58}
59export -f log_follow
60
61log_cat() {
62    cat "$LOG"
63}
64export -f log_cat
65
66log_pwd() {
67    echo "$LOG"
68}
69export -f log_pwd
70
71follow() {
72    local SESSION TEMP
73    local OUTPUT="log_follow"
74
75    TEMP=$(getopt -o cp --long cat,path -n "$CMD follow" -- "$@")
76    if (( $? != 0 )) ; then
77        failure "Invalid options."
78    fi
79
80    # Note the quotes around `$TEMP': they are essential!
81    eval set -- "$TEMP"
82
83    while true ; do
84        case "$1" in
85            -c|--cat) OUTPUT="log_cat" ; shift 1 ;;
86            -p|--path) OUTPUT="log_pwd" ; shift 1 ;;
87            --) shift ;;
88            *)
89                if (( $# < 1 )) ; then
90                    failure "Not enough input arguments.
91Type '$CMD help' for more info."
92                fi
93                SESSION="$1"
94                break
95                ;;
96        esac
97    done
98   
99    if ! is_session "$SESSION" ; then
100        failure "Session '$SESSION' not found.
101Type 'cereal list' to list sessions."
102    elif ! can_follow "$SESSION" "$USER" ; then
103        failure "You are not allowed to follow session '$SESSION'."
104    fi
105
106    LOG="$SESSIONDIR/$SESSION/log/main/current"
107    if [ "$OUTPUT" = 'log_follow' -o "$OUTPUT" = 'log_cat' ] ; then
108        echo "############################################################"
109        echo "### cereal log: $SESSION"
110        echo -n "### "
111        list "$SESSION"
112        echo "### log file: $LOG"
113        echo "############################################################"
114    fi
115    $OUTPUT
116}
117
118###############################################################
119### MAIN
120
121handle_command() {
122
123    COMMAND="$1"
124
125    [ "$COMMAND" ] || failure "Type '$CMD help' for usage."
126    shift
127
128    case $COMMAND in
129        'attach'|'a')
130            attach "$@"
131            ;;
132        'follow'|'f')
133            follow "$@"
134            ;;
135        'list'|'l')
136            list -d "$@" || failure "There are no sessions." 1
137            ;;
138        'help'|'h'|'?')
139            usage
140            ;;
141        *)
142            failure "Unknown command: '$COMMAND'
143Type '$CMD help' for usage."
144            ;;
145    esac
146
147    exit "$ERR"
148}
149
150handle_command "$@"
Note: See TracBrowser for help on using the repository browser.