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

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

cereal: small update to is_running function to provide better return codes with
more information about running state.

  • Property svn:executable set to *
File size: 3.4 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.  Type '$CMD help' for more info."
91                fi
92                SESSION="$1"
93                break
94                ;;
95        esac
96    done
97   
98    if ! is_session "$SESSION" ; then
99        failure "Session '$SESSION' not found.
100Type 'cereal list' to list sessions."
101    elif ! can_follow "$SESSION" "$USER" ; then
102        failure "You are not allowed to follow session '$SESSION'."
103    fi
104
105    LOG="$SESSIONDIR/$SESSION/log/main/current"
106    if [ "$OUTPUT" = 'log_follow' -o "$OUTPUT" = 'log_cat' ] ; then
107        echo "############################################################"
108        echo "### cereal log: $SESSION"
109        echo -n "### "
110        list "$SESSION"
111        echo "### log file: $LOG"
112        echo "############################################################"
113    fi
114    $OUTPUT
115}
116
117###############################################################
118### MAIN
119
120handle_command() {
121
122    COMMAND="$1"
123
124    [ "$COMMAND" ] || failure "Type '$CMD help' for usage."
125    shift
126
127    case $COMMAND in
128        'attach'|'a')
129            attach "$@"
130            ;;
131        'follow'|'f')
132            follow "$@"
133            ;;
134        'list'|'l')
135            list "$@"
136            ;;
137        'help'|'h'|'?')
138            usage
139            ;;
140        *)
141            failure "Unknown command: '$COMMAND'
142Type '$CMD help' for usage."
143            ;;
144    esac
145}
146
147handle_command "$@"
Note: See TracBrowser for help on using the repository browser.