Browse Source

Move common saturnin routines to a module

This module is intended to be moved around with saturnin, but unlike
main meta-command (bin/saturnin.skel), it should not be necessary to
rename it or edit to adapt to new project.
Alois Mahdal 9 years ago
parent
commit
6abb8c6621
3 changed files with 69 additions and 61 deletions
  1. 1
    0
      mkit/config.ini
  2. 5
    61
      src/bin/saturnin.skel
  3. 63
    0
      src/ffoo/saturnin_common.sh

+ 1
- 0
mkit/config.ini View File

@@ -55,5 +55,6 @@
55 55
     src/libexec/saturnin-www                = saturnin-www
56 56
 
57 57
 [files:share]
58
+    src/ffoo/saturnin_common.sh             = ffoo/saturnin_common.sh
58 59
     src/ffoo/saturnin_www.sh                = ffoo/saturnin_www.sh
59 60
     src/ffoo/saturnin_zleep.sh              = ffoo/saturnin_zleep.sh

+ 5
- 61
src/bin/saturnin.skel View File

@@ -28,6 +28,8 @@ SATURNIN_VERSION="__VERSION__"
28 28
 # =====END BUILT PART=====
29 29
 #
30 30
 
31
+ffoo import saturnin_common
32
+
31 33
 export FFOO_INIGREP_PATH FFOO_PATH FFOO_PRETTY_USAGE \
32 34
        SATURNIN_VERSION SATURNIN_CACHE_HOME
33 35
 
@@ -37,69 +39,11 @@ usage() {
37 39
             "--version"
38 40
 }
39 41
 
40
-print_help() {
41
-    echo ""
42
-    echo "built-in sub-commands:"
43
-    echo ""
44
-    echo "    conf"
45
-    echo "    help"
46
-    echo ""
47
-    echo "installed sub-commands:"
48
-    echo ""
49
-    ls_subcommands | sed -e 's/^/    /'
50
-    return "$FFOO_EXIT_OK"
51
-}
52
-
53
-ls_subcommands() {
54
-    find "$SATURNIN_LIBEXEC" \
55
-        -mindepth 1 \
56
-        -maxdepth 1 \
57
-        -executable \
58
-      | sed -e 's/.*saturnin-//' \
59
-      | sort
60
-}
61
-
62 42
 version_info() {
63 43
     echo "$(basename $0) (Smart and ready desktop helper) $SATURNIN_VERSION"
64 44
     exit $FFOO_EXIT_OK
65 45
 }
66 46
 
67
-run_hook() {
68
-    #
69
-    # Run custom hook
70
-    #
71
-    local hname="$1"
72
-    local hook_code
73
-    test -n "$SATURNIN_SUBCOMMAND" || {
74
-        warn "unknown subcommand, ignoring hook: $hname"
75
-        return 0
76
-    }
77
-    hook_code="$(inigrep -j -p hook.$SATURNIN_SUBCOMMAND.$hname)"
78
-    debug -v SATURNIN_SUBCOMMAND hook_code hname
79
-    bash -n <<<"$hook_code" || {
80
-        warn "syntax errors, ignoring hook: $hname"
81
-        return 0
82
-    }
83
-    eval "$hook_code"
84
-}
85
-
86
-subcommand() {
87
-    local sc_es=0   # future exit (return) status
88
-    local lexpath="$SATURNIN_LIBEXEC/saturnin-$SATURNIN_SUBCOMMAND"
89
-    debug -v lexpath
90
-    debug "\$*='$*'"
91
-    test -x "$lexpath" || {
92
-        warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
93
-        print_help
94
-        return $FFOO_EXIT_USAGE
95
-    }
96
-    run_hook pre
97
-    "$lexpath" "$@" || return $?
98
-    sc_es=$?
99
-    run_hook post
100
-    return $sc_es
101
-}
102
-
103 47
 while true; do case $1 in
104 48
     -d|--debug)     export FFOO_DEBUG=true; shift   ;;
105 49
     -v|--verbose)   export FFOO_VERBOSE=true; shift ;;
@@ -120,10 +64,10 @@ shift
120 64
 
121 65
 case "$SATURNIN_SUBCOMMAND" in
122 66
     conf)               inigrep "$@"    ;;
123
-    help)               print_help      ;;
124
-    _ls_subcommands)    ls_subcommands ;;
67
+    help)               saturnin_help ;;
68
+    _ls_subcommands)    saturnin_lssc ;;
125 69
     _lsfun)             ffoom lsfun ;;
126 70
     _lsmod)             ffoom lsmod ;;
127 71
     _ffrun)             ffrun "$@" ;;
128
-    *)                  subcommand "$@" ;;
72
+    *)                  saturnin_runsc "$@" ;;
129 73
 esac

+ 63
- 0
src/ffoo/saturnin_common.sh View File

@@ -0,0 +1,63 @@
1
+#!/bin/bash
2
+
3
+ffoo import exit
4
+ffoo import inigrep
5
+ffoo import pretty
6
+
7
+saturnin_help() {
8
+    echo ""
9
+    echo "built-in sub-commands:"
10
+    echo ""
11
+    echo "    conf"
12
+    echo "    help"
13
+    echo ""
14
+    echo "installed sub-commands:"
15
+    echo ""
16
+    saturnin_lssc | sed -e 's/^/    /'
17
+    return "$FFOO_EXIT_OK"
18
+}
19
+
20
+saturnin_lssc() {
21
+    find "$SATURNIN_LIBEXEC" \
22
+        -mindepth 1 \
23
+        -maxdepth 1 \
24
+        -executable \
25
+      | sed -e 's/.*saturnin-//' \
26
+      | sort
27
+}
28
+
29
+saturnin_runhook() {
30
+    #
31
+    # Run custom hook
32
+    #
33
+    local hname="$1"
34
+    local hook_code
35
+    test -n "$SATURNIN_SUBCOMMAND" || {
36
+        warn "unknown subcommand, ignoring hook: $hname"
37
+        return 0
38
+    }
39
+    hook_code="$(inigrep -j -p hook.$SATURNIN_SUBCOMMAND.$hname)"
40
+    debug -v SATURNIN_SUBCOMMAND hook_code hname
41
+    bash -n <<<"$hook_code" || {
42
+        warn "syntax errors, ignoring hook: $hname"
43
+        return 0
44
+    }
45
+    eval "$hook_code"
46
+}
47
+
48
+saturnin_runsc() {
49
+    local sc_es=0   # future exit (return) status
50
+    local lexpath="$SATURNIN_LIBEXEC/saturnin-$SATURNIN_SUBCOMMAND"
51
+    debug -v lexpath
52
+    debug "\$*='$*'"
53
+    test -x "$lexpath" || {
54
+        warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
55
+        saturnin_help
56
+        return $FFOO_EXIT_USAGE
57
+    }
58
+    saturnin_runhook pre
59
+    "$lexpath" "$@" || return $?
60
+    sc_es=$?
61
+    saturnin_runhook post
62
+    return $sc_es
63
+}