Procházet zdrojové kódy

Sweep handlers under separate function

For code readability and maintenance
Alois Mahdal před 7 roky
rodič
revize
f5a754e2dd
1 změnil soubory, kde provedl 33 přidání a 13 odebrání
  1. 33
    13
      src/imapdomo.skel

+ 33
- 13
src/imapdomo.skel Zobrazit soubor

@@ -31,7 +31,7 @@ bug() {
31 31
     local msg=$1
32 32
     local self
33 33
     self=$(basename "$0")
34
-    die "bug in $self (__MKIT_PROJ_VERSION__): $msg"
34
+    warn "bug in $self (__MKIT_PROJ_VERSION__): $msg"
35 35
 }
36 36
 
37 37
 mkcmd() {
@@ -73,15 +73,44 @@ lshandlers() {
73 73
       | sed 's/.lua$//'
74 74
 }
75 75
 
76
+handle() {
77
+    #
78
+    # Handle action $Action
79
+    #
80
+    local cmd       # imapfilter command
81
+    lshandlers | grep -qw "$Action" || {
82
+        warn "no handler for action: $Action.lua in $CfgDir/handlers"
83
+        return 2
84
+    }
85
+    cmd=$(mkcmd)
86
+    debug -v cmd
87
+    bash -n <<<"$cmd" || {
88
+        bug "bad syntax of cmd: '$cmd'"
89
+        return 3
90
+    }
91
+    if test -n "$CdTo";
92
+    then
93
+        cd "$CdTo" || {
94
+            warn "cannot chdir to: $CdTo"
95
+            return 3
96
+        }
97
+    fi
98
+    mkdir -p "$HeaderDir" || {
99
+        warn "cannot create header directory: $HeaderDir"
100
+        return 3
101
+    }
102
+    eval "$cmd"
103
+}
104
+
76 105
 main() {
77 106
     local Action    # what to do
78 107
     local Debug     # true if debugging
79
-    local cmd       # imapfilter command
80 108
     local CfgDir    # config directory
81 109
     local LogDir    # directory to store logs
82 110
     local HeaderDir # directory to store headers by save_header()
83 111
     local CdTo      # change dir to this before running imapfilter
84 112
     local Certs     # certificate storage
113
+    local es=0      # exit status of this function
85 114
     CfgDir="$IMAPDOMO_CFGDIR"
86 115
     LogDir="$IMAPDOMO_USER_CACHE/logs"
87 116
     HeaderDir="$IMAPDOMO_USER_CACHE/headers"
@@ -101,17 +130,8 @@ main() {
101 130
     Action="$1"; shift
102 131
     test -n "$Action" || usage -w "no action specified"
103 132
     debug -v Action CfgDir LogDir HeaderDir Debug CdTo
104
-    lshandlers | grep -qw "$Action" \
105
-     || die "no handler for action: $Action.lua in $CfgDir/handlers"
106
-    cmd=$(mkcmd)
107
-    debug -v cmd
108
-    bash -n <<<"$cmd" || bug "bad syntax of cmd: '$cmd'"
109
-    if test -n "$CdTo";
110
-    then
111
-        cd "$CdTo" || die "cannot chdir to: $CdTo"
112
-    fi
113
-    mkdir -p "$HeaderDir" || die "cannot create header directory: $HeaderDir"
114
-    eval "$cmd"
133
+    handle "$Action"; es=$?
134
+    return $es
115 135
 }
116 136
 
117 137
 main "$@"