-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkm
executable file
·783 lines (763 loc) · 29.9 KB
/
mkm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#!/usr/bin/env bash
# vim:ts=4:sw=4:expandtab
############################################################
# NAME
# mkm - a toy for manager mkx
#
# SYNOPSIS
# mkm init project-path
#
# mkm path target [ --global | --system ]
# mkm list target [ --global | --system ]
# mkm find target [ --global | --system ] target
# mkm del target [ --global | --system ] targets...
# mkm add target [ --global | --system ] module-name host-path targets...
#
# mkm path module [ --global | --system ]
# mkm list module [ --global | --system ]
# mkm find module [ --global | --system ]
# mkm del module [ --global | --system ] modules...
# mkm add module [ --global | --system ] module-name local
# mkm add module [ --global | --system ] module-name rsa username rsapkey-path hostname hostport
# mkm add module [ --global | --system ] module-name passwd username password hostname hostport
# mkm add module [ --global | --system ] module-name custom [config3] [config4] [config5] [config6]
# mkm add module [ --global | --system ] module-name su username password hostname hostport
#
# mkm path config [ --global | --system ]
# mkm list config [ --global | --system ]
# mkm get config [ --global | --system ] key [default-config]
# mkm find config [ --global | --system ] key
# mkm del config [ --global | --system ] keys
# mkm add config [ --global | --system ] key = vaule
#
# mkm path command [ --global | --system ]
# mkm list command [ --global | --system ]
# mkm find command [ --global | --system ] target [type]
# mkm del command [ --global | --system ] target type
# mkm add command [ --global | --system ] target type command
#
# mkm path commandtpl [ --global | --system ]
# mkm list commandtpl [ --global | --system ]
# mkm find commandtpl [ --global | --system ] module-name [type]
# mkm del commandtpl [ --global | --system ] module-name type
# mkm add commandtpl [ --global | --system ] module-name type command
#
# mkm path targetreg [ --global | --system ]
# mkm list targetreg [ --global | --system ]
# mkm find targetreg [ --global | --system ] target
# mkm add targetreg [ --global | --system ] module-name host-path regexp
#
# debug=on mkm ...
#
# AUTHORS
# neiku project <[email protected]>
#
# SEE ALSO
# mkxrc_modules
# mkxrc_targets
# mkxrc_commands
# mkxrc_commandtpls
#
# VERSION
# 2015/11/26: 支持查看/删除/新增target
# 2015/11/27: 支持查看/删除/新增module
# 2015/11/28: 支持project/global/system级别
# target/module/config/command配置(多项目场景)
# 2015/12/03: 支持本地部署模块(local module)
# 2015/12/11: 支持get config自动去掉左边空白(SP/TAB)
# 2015/12/12: bugfix: get config去空白得在find之后马上执行
# 支持命令模板(可配置不同模块的不同命令模板)
# 2015/12/16: 支持模式匹配target(TODO: 支持del targetreg)
# 2016/02/16: get config支持默认配置
# 2016/03/25: 修复无法正常初始化项目级targetregs配置问题
# 2016/05/28: 支持使用反斜杠作为多行配置的续行符
# ( \\\\ 在两个反引号之间等价于 \\ )
# ( targetreg 不支持多行配置 )
# 2016/06/18: 支持su module(module type = su)
# 2017/01/16: 支持跨平台
# - 通过env程序+PATH变量动态查询bash,控制权
# 、准确性都得到保障
#
############################################################
# Entity Relationship:
# target(*) <-----> module(1) <-----> destination(1)
# target(1) <-----> command(*)
mkxrc_targets_sys="/etc/mkxrc_targets"
mkxrc_targets_glo="$HOME/.mkxrc_targets"
mkxrc_targets_prj="`pwd -P`/.mkxrc_targets~"
mkxrc_modules_sys="/etc/mkxrc_modules"
mkxrc_modules_glo="$HOME/.mkxrc_modules"
mkxrc_modules_prj="`pwd -P`/.mkxrc_modules~"
mkxrc_configs_sys="/etc/mkxrc_configs"
mkxrc_configs_glo="$HOME/.mkxrc_configs"
mkxrc_configs_prj="`pwd -P`/.mkxrc_configs~"
mkxrc_commands_sys="/etc/mkxrc_commands"
mkxrc_commands_glo="$HOME/.mkxrc_commands"
mkxrc_commands_prj="`pwd -P`/.mkxrc_commands~"
mkxrc_commandtpls_sys="/etc/mkxrc_commandtpls"
mkxrc_commandtpls_glo="$HOME/.mkxrc_commandtpls"
mkxrc_commandtpls_prj="`pwd -P`/.mkxrc_commandtpls~"
mkxrc_targetregs_sys="/etc/mkxrc_targetregs"
mkxrc_targetregs_glo="$HOME/.mkxrc_targetregs"
mkxrc_targetregs_prj="`pwd -P`/.mkxrc_targetregs~"
function showhelp()
{
echo "usage:"
echo " mkm init project-path"
echo
echo " mkm path target [ --global | --system ]"
echo " mkm list target [ --global | --system ]"
echo " mkm find target [ --global | --system ] target"
echo " mkm del target [ --global | --system ] targets..."
echo " mkm add target [ --global | --system ] module-name host-path targets..."
echo
echo " mkm path module [ --global | --system ]"
echo " mkm list module [ --global | --system ]"
echo " mkm find module [ --global | --system ] module"
echo " mkm del module [ --global | --system ] modules..."
echo " mkm add module [ --global | --system ] module-name local"
echo " mkm add module [ --global | --system ] module-name rsa username rsapkey-path hostname hostport"
echo " mkm add module [ --global | --system ] module-name passwd username password hostname hostport"
echo " mkm add module [ --global | --system ] module-name custom [config3] [config4] [config5] [config6]"
echo " mkm add module [ --global | --system ] module-name su username password"
echo
echo " mkm path config [ --global | --system ]"
echo " mkm list config [ --global | --system ]"
echo " mkm get config [ --global | --system ] key [default-config]"
echo " mkm find config [ --global | --system ] key"
echo " mkm del config [ --global | --system ] keys..."
echo " mkm add config [ --global | --system ] key = value"
echo
echo " mkm path command [ --global | --system ]"
echo " mkm list command [ --global | --system ]"
echo " mkm find command [ --global | --system ] target [type]"
echo " mkm del command [ --global | --system ] target type"
echo " mkm add command [ --global | --system ] target type command"
echo
echo " mkm path commandtpl [ --global | --system ]"
echo " mkm list commandtpl [ --global | --system ]"
echo " mkm find commandtpl [ --global | --system ] module-name [type]"
echo " mkm del commandtpl [ --global | --system ] module-name type"
echo " mkm add commandtpl [ --global | --system ] module-name type command"
echo
echo " mkm path targetreg [ --global | --system ]"
echo " mkm list targetreg [ --global | --system ]"
echo " mkm find targetreg [ --global | --system ] target"
echo " mkm add targetreg [ --global | --system ] module-name host-path regexp"
}
if [ $# -lt 2 ] ; then
showhelp && exit 1;
fi
cmd="$1"
mod="$2"
if [ "$cmd" != "init" \
-a "$cmd" != "add" \
-a "$cmd" != "list" \
-a "$cmd" != "find" \
-a "$cmd" != "del" \
-a "$cmd" != "get" \
-a "$cmd" != "path" ] ; then
showhelp && exit 1;
fi
if [ "$cmd" != "init" \
-a "$mod" != "target" \
-a "$mod" != "module" \
-a "$mod" != "config" \
-a "$mod" != "command" \
-a "$mod" != "commandtpl" \
-a "$mod" != "targetreg" ] ; then
showhelp && exit 1;
fi
if [ "$cmd" = "get" \
-a "$mod" != "config" ] ; then
showhelp && exit 1;
fi
shift 2
if [ "$cmd" = "init" ] ; then
[ -z "$mod" ] && showhelp && exit
touch "$mod/`basename $mkxrc_targets_prj`"
touch "$mod/`basename $mkxrc_modules_prj`"
touch "$mod/`basename $mkxrc_configs_prj`"
touch "$mod/`basename $mkxrc_commands_prj`"
touch "$mod/`basename $mkxrc_commandtpls_prj`"
touch "$mod/`basename $mkxrc_targetregs_prj`"
echo "init project in $mod/ succ"
fi
if [ $mod = "target" ] ; then
case "$cmd" in
find)
[ -z "$1" ] && showhelp && exit 1
path="`mkm path target $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w "$2" \
| tail -n1
exit
fi
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w "$1" \
| tail -n1`"
[ -z "$out" ] && out="`mkm find target --global $1`"
[ -z "$out" ] && out="`mkm find target --system $1`"
[ -n "$out" ] && echo "$out"
exit
;;
list)
path="`mkm path target $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
debug=on mklog debug "# ${1:2}'s targets ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
exit
fi
debug=on mklog debug "# project's targets ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
mkm list target --global
mkm list target --system
exit
;;
del)
path="`mkm path target $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
[ -f "$path" ] \
&& for target in $* ; do
sed -i "s/\<$target\>//g" "$path"
mklog normal "del ${flag:2} target($target) succ"
done \
|| mklog error "del ${flag:2} target($*) fail"
exit
fi
[ -f "$path" ] \
&& for target in $* ; do
sed -i "s/\<$target\>//g" "$path"
mklog normal "del project target($target) succ"
done \
|| mklog error "del project target($*) fail"
exit
;;
add)
path="`mkm path target $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
echo "$*" >> "$path" \
&& mklog normal "add ${flag:2}($path) target succ" \
|| mklog error "add ${flag:2}($path) target fail"
exit
fi
echo "$*" >> "$path" \
&& mklog normal "add project($path) target succ" \
|| mklog error "add project($path) target fail"
exit
;;
path)
[ "$1" = "--global" ] && echo "$mkxrc_targets_glo" && exit
[ "$1" = "--system" ] && echo "$mkxrc_targets_sys" && exit
[ -f "$mkxrc_targets_prj" ] && echo "$mkxrc_targets_prj" && exit
mkxrc_targets_prj="`dirname $(dirname "$mkxrc_targets_prj")`/`basename "$mkxrc_targets_prj"`"
while [ "`dirname "$mkxrc_targets_prj"`" != "/" ] ; do
[ -f "$mkxrc_targets_prj" ] && echo "$mkxrc_targets_prj" && exit
mkxrc_targets_prj="`dirname $(dirname "$mkxrc_targets_prj")`/`basename "$mkxrc_targets_prj"`"
done
mkxrc_targets_prj="`pwd -P`/`basename $mkxrc_targets_prj`"
echo "$mkxrc_targets_prj"
exit
;;
esac
fi
if [ $mod = "module" ] ; then
case "$cmd" in
find)
[ -z "$1" ] && showhelp && exit 1
path="`mkm path module $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w "^$2" \
| tail -n1
exit
fi
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w "^$1" \
| tail -n1`"
[ -z "$out" ] && out="`mkm find module --global $1`"
[ -z "$out" ] && out="`mkm find module --system $1`"
[ -n "$out" ] && echo "$out"
exit
;;
add)
path="`mkm path module $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
[ $# -lt 2 ] && showhelp && exit 1
[ "$2" != "rsa" -a "$2" != "local" -a "$2" != "passwd" ] && showhelp && exit 1
[ "$2" = "rsa" -a $# -lt 6 ] && showhelp && exit 1
[ "$2" = "passwd" -a $# -lt 6 ] && showhelp && exit 1
echo "$*" >> "$path" \
&& mklog normal "add ${flag:2}($path) module succ" \
|| mklog error "add ${flag:2}($path) module fail"
exit
fi
[ $# -lt 2 ] && showhelp && exit 1
[ "$2" != "rsa" \
-a "$2" != "local" \
-a "$2" != "passwd" \
-a "$2" != "custom" \
-a "$2" != "su" ] && showhelp && exit 1
[ "$2" = "rsa" -a $# -lt 6 ] && showhelp && exit 1
[ "$2" = "passwd" -a $# -lt 6 ] && showhelp && exit 1
[ "$2" = "su" -a $# -lt 2 ] && showhelp && exit 1
echo "$*" >> "$path" \
&& mklog normal "add project($path) module succ" \
|| mklog error "add project($path) module fail"
exit
;;
list)
path="`mkm path module $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
debug=on mklog debug "# ${1:2}'s modules ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
exit
fi
debug=on mklog debug "# project's modules ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
mkm list module --global
mkm list module --system
exit
;;
del)
path="`mkm path module $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
[ -f "$path" ] \
&& for module in $* ; do
sed -i "/^$module[ \t]/d" "$path"
mklog normal "del ${flag:2} module($module) succ"
done \
|| mklog error "del ${flag:2} module($*) fail"
exit
fi
[ -f "$path" ] \
&& for module in $* ; do
sed -i "/^$module[ \t]/d" "$path"
mklog normal "del project module($module) succ"
done \
|| mklog error "del project module($*) fail"
exit
;;
path)
[ "$1" = "--global" ] && echo "$mkxrc_modules_glo" && exit
[ "$1" = "--system" ] && echo "$mkxrc_modules_sys" && exit
[ -f "$mkxrc_modules_prj" ] && echo "$mkxrc_modules_prj" && exit
mkxrc_modules_prj="`dirname $(dirname "$mkxrc_modules_prj")`/`basename "$mkxrc_modules_prj"`"
while [ "`dirname "$mkxrc_modules_prj"`" != "/" ] ; do
[ -f "$mkxrc_modules_prj" ] && echo "$mkxrc_modules_prj" && exit
mkxrc_modules_prj="`dirname $(dirname "$mkxrc_modules_prj")`/`basename "$mkxrc_modules_prj"`"
done
mkxrc_modules_prj="`pwd -P`/`basename $mkxrc_modules_prj`"
echo "$mkxrc_modules_prj"
exit
;;
esac
fi
if [ $mod = "config" ] ; then
case "$cmd" in
get)
[ -z "$1" ] && showhelp && exit 1
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
val="`mkm find config $1 $2 | sed "s/^[ \t]*$2[ \t]*=[ \t]*//g"`"
[ -z "$val" ] && val="$3"
[ -n "$val" ] && echo "$val"
exit
fi
val="`mkm find config $1 $2 | sed "s/^[ \t]*$1[ \t]*=[ \t]*//g"`"
[ -z "$val" ] && val="`mkm find config --global $1`"
[ -z "$val" ] && val="`mkm find config --system $1`"
[ -z "$val" ] && val="$2"
[ -n "$val" ] && echo "$val"
exit
;;
find)
[ -z "$1" ] && showhelp && exit 1
path="`mkm path config $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\[ \t]*$//g' \
| grep -v "^$" \
| grep -P "^[ \t]*$2[ \t]*=" \
| tail -n1
exit
fi
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -P "^[ \t]*$1[ \t]*=" \
| tail -n1`"
[ -z "$out" ] && out="`mkm find config --global $1`"
[ -z "$out" ] && out="`mkm find config --system $1`"
[ -n "$out" ] && echo "$out"
exit
;;
add)
path="`mkm path config $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
echo "$*" >> "$path" \
&& mklog normal "add ${flag:2}($path) config succ" \
|| mklog error "add ${flag:2}($path) config fail"
exit
fi
echo "$*" >> "$path" \
&& mklog normal "add project($path) config succ" \
|| mklog error "add project($path) config fail"
exit
;;
list)
path="`mkm path config $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
debug=on mklog debug "# ${1:2}'s configs ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
exit
fi
debug=on mklog debug "# project's configs ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
mkm list config --global
mkm list config --system
exit
;;
del)
path="`mkm path config $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
[ -f "$path" ] \
&& for config in $* ; do
sed -i "/^[ \t]*$config[ \t]*=/d" "$path"
mklog normal "del ${flag:2} config($config) succ"
done \
|| mklog error "del ${flag:2} config($*) fail"
exit
fi
[ -f "$path" ] \
&& for config in $* ; do
sed -i "/^[ \t]*$config[ \t]*=/d" "$path"
mklog normal "del project config($config) succ"
done \
|| mklog error "del project config($*) fail"
exit
;;
path)
[ "$1" = "--global" ] && echo "$mkxrc_configs_glo" && exit
[ "$1" = "--system" ] && echo "$mkxrc_configs_sys" && exit
[ -f "$mkxrc_configs_prj" ] && echo "$mkxrc_configs_prj" && exit
mkxrc_configs_prj="`dirname $(dirname "$mkxrc_configs_prj")`/`basename "$mkxrc_configs_prj"`"
while [ "`dirname "$mkxrc_configs_prj"`" != "/" ] ; do
[ -f "$mkxrc_configs_prj" ] && echo "$mkxrc_configs_prj" && exit
mkxrc_configs_prj="`dirname $(dirname "$mkxrc_configs_prj")`/`basename "$mkxrc_configs_prj"`"
done
mkxrc_configs_prj="`pwd -P`/`basename $mkxrc_configs_prj`"
echo "$mkxrc_configs_prj"
exit
;;
esac
fi
if [ $mod = "command" ] ; then
case "$cmd" in
find)
# [ --global | --system ] target [type]
[ -z "$1" ] && showhelp && exit 1
path="`mkm path command $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w -P "^$2[ \t]*"`"
[ -z "$out" ] && exit
[ -z "$3" ] && echo "$out" && exit
echo "$out" | grep -P "^$2[ \t]*$3" | tail -n1 && exit
fi
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w -P "^$1[ \t]*"`"
[ -n "$out" -a -z "$2" ] \
&& mkm find command --system $1 \
&& mkm find command --global $1 \
&& echo "$out" \
&& exit
[ -n "$out" -a -n "$2" ] \
&& out="`echo "$out" | grep -P "^$1[ \t]*$2" | tail -n1`"
[ -z "$out" ] && out="`mkm find command --global $1 $2`"
[ -z "$out" ] && out="`mkm find command --system $1 $2`"
[ -n "$out" ] && echo "$out"
exit
;;
add)
path="`mkm path command $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
echo "$*" >> "$path" \
&& mklog normal "add ${flag:2}($path) command succ" \
|| mklog error "add ${flag:2}($path) command fail"
exit
fi
echo "$*" >> "$path" \
&& mklog normal "add project($path) command succ" \
|| mklog error "add project($path) command fail"
exit
;;
list)
path="`mkm path command $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
debug=on mklog debug "# ${1:2}'s commands ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
exit
fi
debug=on mklog debug "# project's commands ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
mkm list command --global
mkm list command --system
exit
;;
del)
path="`mkm path command $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
[ $# -lt 2 ] && showhelp && exit 1
[ -f "$path" ] \
&& sed -i "/^[ \t]*$1[ \t]$2/d" "$path" \
&& mklog normal "del ${flag:2} command($1 $2) succ" \
|| mklog error "del ${flag:2} command($1 $2) fail"
exit
fi
[ $# -lt 2 ] && showhelp && exit 1
[ -f "$path" ] \
&& sed -i "/^[ \t]*$1[ \t]$2/d" "$path" \
&& mklog normal "del project command($1 $2) succ" \
|| mklog error "del project command($1 $2) fail"
exit
;;
path)
[ "$1" = "--global" ] && echo "$mkxrc_commands_glo" && exit
[ "$1" = "--system" ] && echo "$mkxrc_commands_sys" && exit
[ -f "$mkxrc_commands_prj" ] && echo "$mkxrc_commands_prj" && exit
mkxrc_commands_prj="`dirname $(dirname "$mkxrc_commands_prj")`/`basename "$mkxrc_commands_prj"`"
while [ "`dirname "$mkxrc_commands_prj"`" != "/" ] ; do
[ -f "$mkxrc_commands_prj" ] && echo "$mkxrc_commands_prj" && exit
mkxrc_commands_prj="`dirname $(dirname "$mkxrc_commands_prj")`/`basename "$mkxrc_commands_prj"`"
done
mkxrc_commands_prj="`pwd -P`/`basename $mkxrc_commands_prj`"
echo "$mkxrc_commands_prj"
exit
;;
esac
fi
if [ $mod = "commandtpl" ] ; then
case "$cmd" in
find)
# [ --global | --system ] target [type]
[ -z "$1" ] && showhelp && exit 1
path="`mkm path commandtpl $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w -P "^$2[ \t]*"`"
[ -z "$out" ] && exit
[ -z "$3" ] && echo "$out" && exit
echo "$out" | grep -P "^$2[ \t]*$3" | tail -n1 && exit
fi
out="`grep -v -E "^#" "$path" 2>/dev/null \
| sed 's/^[ \t]*//g' \
| sed ':begin; N; s/\\\\[ \t]*\n/ /g; b begin;' \
| sed 's/\\\\[ \t]*$//g' \
| grep -v "^$" \
| grep -w -P "^$1[ \t]*"`"
[ -n "$out" -a -z "$2" ] \
&& mkm find commandtpl --system $1 \
&& mkm find commandtpl --global $1 \
&& echo "$out" \
&& exit
[ -n "$out" -a -n "$2" ] \
&& out="`echo "$out" | grep -P "^$1[ \t]*$2" | tail -n1`"
[ -z "$out" ] && out="`mkm find commandtpl --global $1 $2`"
[ -z "$out" ] && out="`mkm find commandtpl --system $1 $2`"
[ -n "$out" ] && echo "$out"
exit
;;
add)
path="`mkm path commandtpl $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
echo "$*" >> "$path" \
&& mklog normal "add ${flag:2}($path) commandtpl succ" \
|| mklog error "add ${flag:2}($path) commandtpl fail"
exit
fi
echo "$*" >> "$path" \
&& mklog normal "add project($path) commandtpl succ" \
|| mklog error "add project($path) commandtpl fail"
exit
;;
list)
path="`mkm path commandtpl $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
debug=on mklog debug "# ${1:2}'s commandtpl ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
exit
fi
debug=on mklog debug "# project's commandtpl ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
mkm list commandtpl --global
mkm list commandtpl --system
exit
;;
del)
path="`mkm path commandtpl $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
[ $# -lt 2 ] && showhelp && exit 1
[ -f "$path" ] \
&& sed -i "/^[ \t]*$1[ \t]$2/d" "$path" \
&& mklog normal "del ${flag:2} commandtpl($1 $2) succ" \
|| mklog error "del ${flag:2} commandtpl($1 $2) fail"
exit
fi
[ $# -lt 2 ] && showhelp && exit 1
[ -f "$path" ] \
&& sed -i "/^[ \t]*$1[ \t]$2/d" "$path" \
&& mklog normal "del project commandtpl($1 $2) succ" \
|| mklog error "del project commandtpl($1 $2) fail"
exit
;;
path)
[ "$1" = "--global" ] && echo "$mkxrc_commandtpls_glo" && exit
[ "$1" = "--system" ] && echo "$mkxrc_commandtpls_sys" && exit
[ -f "$mkxrc_commandtpls_prj" ] && echo "$mkxrc_commandtpls_prj" && exit
mkxrc_commandtpls_prj="`dirname $(dirname "$mkxrc_commandtpls_prj")`/`basename "$mkxrc_commandtpls_prj"`"
while [ "`dirname "$mkxrc_commandtpls_prj"`" != "/" ] ; do
[ -f "$mkxrc_commandtpls_prj" ] && echo "$mkxrc_commandtpls_prj" && exit
mkxrc_commandtpls_prj="`dirname $(dirname "$mkxrc_commandtpls_prj")`/`basename "$mkxrc_commandtpls_prj"`"
done
mkxrc_commandtpls_prj="`pwd -P`/`basename $mkxrc_commandtpls_prj`"
echo "$mkxrc_commandtpls_prj"
exit
;;
esac
fi
if [ $mod = "targetreg" ] ; then
case "$cmd" in
find)
[ -z "$1" ] && showhelp && exit 1
path="`mkm path targetreg $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
[ -z "$2" ] && showhelp && exit 1
grep -v -E "^#|^$" "$path" 2>/dev/null \
| tac \
| while read module hostpath regexp ; do
if echo "$2" | grep -P "$regexp" >/dev/null 2>&1; then
echo "$module $hostpath $regexp"
break
fi
done
exit
fi
out="`grep -v -E "^#|^$" "$path" 2>/dev/null \
| tac \
| while read module hostpath regexp ; do
if echo "$1" | grep -P "$regexp" >/dev/null 2>&1; then
echo "$module $hostpath $regexp"
break
fi
done`"
[ -z "$out" ] && out="`mkm find targetreg --global $1`"
[ -z "$out" ] && out="`mkm find targetreg --system $1`"
[ -n "$out" ] && echo "$out"
exit
;;
list)
path="`mkm path targetreg $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
debug=on mklog debug "# ${1:2}'s targetregs ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
exit
fi
debug=on mklog debug "# project's targetregs ($path):"
[ -s "$path" ] && cat "$path" | grep -v -E "^#|^$"
mkm list targetreg --global
mkm list targetreg --system
exit
;;
del) # TODO: targetreg 本身是正则表达式
mklog error "'mkm del targetreg' command not supported now!"
exit
;;
add)
path="`mkm path targetreg $1`"
if [ "$1" = "--global" -o "$1" = "--system" ] ; then
flag="$1"
shift 1
echo "$*" >> "$path" \
&& mklog normal "add ${flag:2}($path) targetreg succ" \
|| mklog error "add ${flag:2}($path) targetreg fail"
exit
fi
echo "$*" >> "$path" \
&& mklog normal "add project($path) targetreg succ" \
|| mklog error "add project($path) targetreg fail"
exit
;;
path)
[ "$1" = "--global" ] && echo "$mkxrc_targetregs_glo" && exit
[ "$1" = "--system" ] && echo "$mkxrc_targetregs_sys" && exit
[ -f "$mkxrc_targetregs_prj" ] && echo "$mkxrc_targetregs_prj" && exit
mkxrc_targetregs_prj="`dirname $(dirname "$mkxrc_targetregs_prj")`/`basename "$mkxrc_targetregs_prj"`"
while [ "`dirname "$mkxrc_targetregs_prj"`" != "/" ] ; do
[ -f "$mkxrc_targetregs_prj" ] && echo "$mkxrc_targetregs_prj" && exit
mkxrc_targetregs_prj="`dirname $(dirname "$mkxrc_targetregs_prj")`/`basename "$mkxrc_targetregs_prj"`"
done
mkxrc_targetregs_prj="`pwd -P`/`basename $mkxrc_targetregs_prj`"
echo "$mkxrc_targetregs_prj"
exit
;;
esac
fi