This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp
executable file
·694 lines (613 loc) · 16.9 KB
/
mcp
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
#!/bin/bash
# Manage
# Control
# Projects
mcp(){
# ENVIRONMENT VARIABLES:
#--------------------------------------------------------------#
# full path to mcp folder
path_mcp="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# github username
github_username="$GITHUB_USERNAME"
# github password or token
github_auth="$GITHUB_AUTH"
#--------------------------------------------------------------#
# syntax info
usage="""usage: $FUNCNAME [help] [status] [autoenv] [var] [repolist]
[-g|-v] [-g|-v <name>] [-c <reponame>] [-c <reponame> <username>] [-d <'description'> <name>]
[env <name> <value>] [delvar <name>] [repo <reponame>] [delrepo <reponame>]
[repodesc <name> <'description'>]
"""
# default values
git_desc='No description was added.'
git_private=true
git_https=false
path_current="$PWD"
env_autoenv=true
env_name='.env' # should not be changed!
#-----------#
# USER HELP #
#-----------#
# command: BLANK
if [[ $1 == '' ]] ; then
# guidance info
echo "$FUNCNAME: missing arguments."
echo "Try '$FUNCNAME help' for usage information."
# Operation not permitted
exit 1
fi
# command: help
if [[ $1 == 'help' ]] ; then
echo "$usage"
echo "Commands:"
echo " repo Toggle the visibility of given repo."
echo " repolist Get a list of all repo's."
echo " repodesc Change the description for given repo."
echo " delrepo Delete a repo from github."
echo " env Create/change local environment variable."
echo " var Get a list of all local environment variables."
echo " delvar Delete a local environment variable."
echo " autoenv Toggle automatic environment activation (same as '-a' bellow)."
echo " status The current status of the current dir."
echo "-------------------------------------------------------------------------------"
echo "Options:"
echo " -g Setup a new repository on github."
echo " -d Set a description on new project. (default '"$git_desc"')"
echo " -p Make repo public on new project. (default private)"
echo " -h Use https to connect to github. (default SSH)"
echo ""
echo " -c Clone existing repo from github."
echo " -h Use https to connect to github. (default SSH)"
echo ""
echo "Extra:"
echo " -v Setup virtuelenv."
echo " -a Disable automatic activate (default enabled)."
echo ""
echo "Eks:"
echo " Create new project with: github(g), ssh protocol(default), public repo(p), virtualenv(v), disable autoenv(a) and description(d)"
echo " mcp -gpvad 'description' name"
echo ""
echo "Note: If no <name> is given, current folder name will be used."
# Success
exit 0
fi
#---------------#
# USER COMMANDS #
#---------------#
# commands: variables, status, repolist, autoenv
case $@ in
'var') variableList;;
'status') folderStatus;;
'repolist') getRepo;;
'autoenv') toggleEnv;;
esac
# command: delvariable
[[ $1 == 'delvar' ]] && variableDelete "$@"
# command: repo
[[ $1 == 'repo' ]] && toggleRepo "$@"
# command: description
[[ $1 == 'repodesc' ]] && descRepo "$@"
# command: delrepo
[[ $1 == 'delrepo' ]] && deleteRepo "$@"
# command: env
[[ $1 == 'env' ]] && envVariable "$@"
#----------------#
# USER ARGUMENTS #
#----------------#
# set silent alerts and valid options
while getopts ":acd:ghpv" option ; do
case "$option" in
# do not use autoenv
a) env_autoenv=false
;;
# clone repo
c) git_clone=true
;;
# add description
d) git_desc="$OPTARG"; group_ext=true
;;
# create github repo
g) git_create=true; group_git=true
;;
# https protocol
h) git_https=true
;;
# public repo
p) git_private=false; group_ext=true
;;
# setup env
v) env_virtual=true
;;
# handle option argument
:) printf "missing argument for -'%s'.\n" "$OPTARG"
echo "$usage"
# Operation not permitted
exit 1
;;
# handle unknown option
\?) printf "unknown option: -'%s'.\n" "$OPTARG"
echo "$usage"
# Operation not permitted
exit 1
;;
esac
done
# options handeling
optionHandeling $@
# remove options from $@
shift $((OPTIND - 1))
# argument handeling
argumentHandeling $@
# set name (given input or current folder name)
if [[ $1 != '' ]] ; then
# set name from argument
name="${1//[' ','_']/-}"
else
# set name to current folder name
IFS='/' # set split key
read -ra name <<< "$PWD" # split PWD
name="${name[-1]//[' ','_']/-}" # standarize and set last foldername
fi
#-----------------#
# COMMAND MANAGER #
#-----------------#
# command: g (d,h,p,v(a))
if [[ $git_create = true ]] ; then
# check file avaiability if new folder will be made
[[ $1 != '' ]] && checkFolder
# check if .git exist
cd "$path_current"
[[ -a '.git' ]] && echo "error, '.git' is already in this folder." && exit 1
# go to script folder
cd "$path_mcp"
# create new repo (method, username, auth, name, desc, visibility)
python3 mcp-api.py "createRepo" "$github_username" "$github_auth" "$name" "$git_desc" "$git_private"
# error code
ec="$?"
# if success
if [[ $ec == '0' ]] ; then
# go to current path
cd "$path_current"
# create new project folder if name is given
[[ $1 != '' ]] && createFolder && cd "$name"
# add git to folder
setupGit
# env can be setup
env_ready=true
else
# repo name already exist / error
exit 1
fi
fi
# command: c (h)
if [[ $git_clone == true ]] ; then
# go to current folder
cd "$path_current"
# check if .git exist
[[ -a '.git' ]] && echo "error, '.git' is already in this folder." && exit 1
# check for username input
if [[ $2 == '' ]] ; then
# clone your own repos
[[ $git_https == true ]] && git clone https://github.com/"$github_username"/"$name".git ||
git clone [email protected]:"$github_username"/"$name".git
else
# clone with reponame and username
[[ $git_https == true ]] && git clone https://github.com/"$2"/"$name".git ||
git clone [email protected]:"$2"/"$name".git
fi
fi
# command: v (a)
if [[ $env_virtual == true ]]; then
# go to current dir
cd "$path_current"
# check for new github project
if [[ $group_git == true ]] && [[ $1 != '' ]] ; then
cd "$name"
else
# check for creating new dir
if [[ $1 != '' ]] ; then
createFolder && cd "$name"
fi
fi
# setup virtualenv
setupEnv
fi
}
#---------------#
# MANAGE FOLDER #
#---------------#
# check if a folder exist
checkFolder(){
# go to project folder
cd "$path_current"
# compare to lowercase folder names
[[ -a "$name" ]] &&
echo 'error, folder already exist' &&
exit 1
}
# create a new folder inside project folder
createFolder(){
# check for existing folder
checkFolder
# create new folder
mkdir "$name" &&
echo 'created new folder' || (
echo 'error, failed to create folder.' &&
# exit script
exit 1 )
}
#---------------#
# MANAGE GITHUB #
#---------------#
# setup git
setupGit(){
echo 'setting up git...'
# create default files
[[ ! -a 'README.md' ]] &&
echo $"# $name" > README.md
[[ ! -a '.gitignore' ]] &&
echo $'__pycache__\n'$env_name$'\n*.log\n*.txt' > .gitignore
# choose protocol
protocol
# git commands
git init
git add .
git commit -m "Initial commit"
git remote add origin "$url"
git push -u origin master
echo 'git initialized'
}
# get protocol
protocol(){
# choose protocol
if [[ $git_https == true ]] ; then
echo 'using https'
url="https://github.com/"$github_username"/"$name".git"
else
echo 'using ssh'
url="[email protected]:"$github_username"/"$name".git"
# testing ssh connection
ssh -q [email protected]
if [[ $? == '255' ]] ; then
echo 'error, ssh it not connected to github.'
echo 'IMPORTANT: using https insted.'
url="https://github.com/"$github_username"/"$name".git"
fi
fi
}
# get all repo's
getRepo(){
# go to python3 script
cd "$path_mcp"
# user text
echo 'Github repo list:'
# get repo list from api (method, username, auth)
python3 mcp-api.py "repoList" "$github_username" "$github_auth"
# Success
exit 0
}
# add/change repo description
descRepo(){
# error check
[[ $# != 3 ]] && echo 'error, need name and description.' && echo "$usage" && exit 1
# go to python3 script
cd "$path_mcp"
# run python3
python3 mcp-api.py "descRepo" "$github_username" "$github_auth" "$2" "$3"
# Success
exit 0
}
# delete given repo
deleteRepo(){
# error check
[[ $# != 2 ]] && echo 'error, need repo name.' && echo "$usage" && exit 1
# go to python3 script
cd "$path_mcp"
# user comfirm
read -p "about to delete: '$2', continue? [y,n] " yn
case $yn in
[Yy] ) python3 mcp-api.py "delRepo" "$github_username" "$github_auth" "$2";;
[Nn] ) exit 0;;
* ) exit 0;;
esac
# Success
exit 0
}
# toggle repo visibility
toggleRepo(){
# error check
[[ $# != 2 ]] && echo 'error, need repo name.' && echo "$usage" && exit 1
# go to python3 script
cd "$path_mcp"
# toggle visibility (method, username, auth, reponame)
python3 mcp-api.py "gitVisibility" "$github_username" "$github_auth" "$2"
# dynamic exit
exit $?
}
#--------------------#
# MANAGE ENVIRONMENT #
#--------------------#
# find env folder, when env is activated
findEnv(){
# go to current dir
cd $path_current
# find env by backtracking
for i in {1..10} ; do
# check path
if [[ -a "$env_name/Scripts" ]]; then
# goto file
cd $env_name"/Scripts/"
# found path
break
else
if [[ -a "$env_name/bin" ]]; then
# goto file
cd $env_name"/bin"
# found path
break
else
# go out of current folder
cd ..
fi
fi
# file not found
[[ $i == 10 ]] && echo "error, didn't find: "$env_name && exit 1
done
}
# setup new env variable
envVariable(){
# error check
[[ $# != 3 ]] && echo 'error, need name and value.' && echo "$usage" && exit 1
# ENV is activate
if [[ -z "$VIRTUAL_ENV" ]]; then
echo 'error, env needs to be activated first, type: "activate".'
exit 1
fi
echo 'adding new environment variable...'
# find env folder
findEnv
# init files for current folder
if [ ! -f 'activate.bak' ]; then
echo 'first time setup...'
# make log file
touch custom_variables &&
# make bakup
cp activate activate.bak &&
# make nr.2 of activate file
cp activate activate2 &&
# add info to activate
awk $'/export VIRTUAL/ { print; print "# >>>> MY CUSTOM EXPORT <<<<\\n# >>>> END <<<<"; next }1' activate2 > activate &&
# save changes to activate2
cat activate > activate2 &&
# add info to activate
awk $'/unset VIRTUAL/ { print; print " # >>>> MY CUSTOM UNSET <<<<\\n # >>>> END <<<<"; next }1' activate2 > activate &&
# save to .bak
cat activate > activate.bak &&
# delete activate2
rm -f activate2 &&
# feedback
echo 'setup complete' ||
# error
( echo 'error, something went wrong.' && exit 1 )
fi
# check for invalid space input
[[ $2 == *" "* ]] && echo "error, can't use spaces." && exit 1
[[ $3 == *" "* ]] && echo "error, can't use spaces." && exit 1
# check for unique ENV name
if ! printenv | grep -q "$2"= && ! cat custom_variables | grep -q "$2"= ; then
# copy file
cat activate > activate2 &&
# add export
awk $'/CUSTOM EXPORT/ { print; print "export '$2'='$3'"; next }1' activate2 > activate &&
# update activate2
cat activate > activate2 &&
# add unset
awk $'/CUSTOM UNSET/ { print; print " unset '$2'"; next }1' activate2 > activate &&
# add variables to log
echo "$2=$3" >> custom_variables &&
# update .bak
cat activate > activate.bak &&
# restore file on error
echo 'added successfully' && echo 'IMPORTANT: re-activate env for variable to init.' || ( echo 'data may have been lost, type: "mcp var" to check.' && cat activate.bak > activate )
# delete activate2
rm -f activate2
# Success
exit 0
else
# ask to overwrite environment variable
read -p "overwrite variable: '$2', continue? [y,n] " yn
case $yn in
[Yy] ) overwriteVariable "$@";;
[Nn] ) echo 'not overwriting' && exit 0;;
* ) echo 'not a valid input, no action taken' && exit 0;;
esac
# Success
exit 0
fi
}
# overwrite environment variable
overwriteVariable(){
# edit export
sed -i 's/export '$2'=.*/export '$2'='$3'/' activate
# update variables to log
sed -i 's/'$2'=.*/'$2'='$3'/' custom_variables
# update .bak
cat activate > activate.bak &&
# restore file on error
echo 'overwritten successfully' && echo 'IMPORTANT: re-activate env for variable to init.' || ( echo 'data may have been lost, type: "mcp var" to check.' && cat activate.bak > activate )
# Success
exit 0
}
# get environment variables
variableList(){
echo $'Local variables:'
# ENV is activate
if [[ -z "$VIRTUAL_ENV" ]]; then
echo 'error, env needs to be activated.'
exit 1
fi
# find file
findEnv
# check if file in not empty and read
[[ -s custom_variables ]] && cat custom_variables ||
echo 'no variables added.'
# Success
exit 0
}
# delete environment varaible
variableDelete(){
# error check
[[ $# == 1 ]] && echo 'error, need at least one argument.' && echo "$usage" && exit 1
# ENV is activate
if [[ -z "$VIRTUAL_ENV" ]]; then
echo 'error, env needs to be activated, type: "activate".'
exit 1
fi
echo 'deleting environment variable...'
# find env folder
findEnv
# check for file
[[ ! -a custom_variables ]] && echo 'No environment variables has been set.' && exit 1
# delete all arguments
for i in ${@:2} ; do
# check if variable exist
! cat custom_variables | grep -q $i && echo 'error, variable "'$i'" was not set.' && exit 1
# delete line from activate
sed -i "/unset $i\b/d" activate &&
sed -i "/export $i\b/d" activate &&
# update activate.bak
cat activate > activate.bak &&
# delete line from log file
sed -i "/^$i\b/d" custom_variables &&
echo "deleted: $i" ||
echo "error, wasen't able to delete the variable."
done
# note
echo 'IMPORTANT: deactivate env for variable to unset.'
# Success
exit 0
}
# setup new env
setupEnv(){
# check if env exists
[[ -a $env_name ]] && echo "error, $env_name is already in this folder." && exit 1
echo 'setting up environment...'
# setup env with python3 -m
python3 -m venv $env_name ||
( echo 'need python3 to creat venv!' &&
exit 1 )
# check for autoenv
[[ $env_autoenv == true ]]
if [[ -a "$env_name/Scripts" ]]; then
cd "$env_name/Scripts"
else
cd "$env_name/bin"
fi
# add text to file
echo 'this file is used as a key to activate the environment.' > autoenv
# finsihed
echo 'environment setup, type: "activate" to activate.'
# Success
exit 0
}
# toggle autoenv
toggleEnv(){
# check if .env is available
findEnv
# check if autoenv file exists
[[ -a 'autoenv' ]] && rm -f 'autoenv' && echo 'autoenv is disabled' ||
( echo 'this file is used as a key to activate the environment.' > autoenv && echo 'autoenv is enabled' )
# Success
exit 0
}
#---------------#
# FOLDER STATUS #
#---------------#
# get folder status
folderStatus(){
# go to current dir
cd $path_current
fstatus=true
echo 'Folder status:'
[[ -d '.git' ]] && echo ' - git is added.' && fstatus=false
[[ -d "$env_name" ]] && echo ' - env is added.' && fstatus=false && env=true
# find env by backtracking
for i in {1..10} ; do
[[ -a '.env/Scripts/autoenv' || -a '.env/bin/autoenv' ]] && echo ' - autoenv is enabled.' || ( [[ "$env" == true ]] && echo ' - autoenv is disabled.' ) &&
# exit
break ||
# go out of current folder
cd ..
# file not found
[[ $i == 10 ]] && [[ $fstatus == true ]] && echo ' - just a normal folder.'
done
# Success
exit 0
}
#-----------------#
# ERROR HANDELING #
#-----------------#
# check and send input error
optionHandeling(){
# Unknown command
[[ $1 != '-'* ]] &&
echo "unknown command: '${1}'" &&
echo "$usage" &&
# Operation not permitted
exit 1
# invalid '--'
[[ $1 == '--' ]] &&
echo "unknown option: -'${1:1}'" &&
echo "$usage" &&
# Operation not permitted
exit 1
# can't use -g and -c at the same time
[[ $git_create == true && $git_clone == true ]] &&
echo "error, can't use both -g and -c" &&
echo "$usage" &&
# Operation not permitted
exit 1
# can't use -c and -v at the same time
[[ $git_clone == true && $env_virtual == true ]] &&
echo "error, can't use both -c and -v" &&
echo "$usage" &&
# Operation not permitted
exit 1
# need to use -g to be able to use -d or -p
[[ $group_ext == true && $git_create != true ]] &&
echo "error, can't use -d or -p without -g" &&
echo "$usage" &&
# Operation not permitted
exit 1
# need -g or -c for -h to work
[[ $git_https == true && $group_git != true ]] &&
echo "error, can't use -h without -g or -c" &&
echo "$usage" &&
# Operation not permitted
exit 1
# need -v for -a to work
[[ $env_autoenv == false && $env_virtual != true ]] &&
echo "error, can't use -a without -v" &&
echo "$usage" &&
# Operation not permitted
exit 1
}
# check and send input error
argumentHandeling(){
# to many arg for -g or -v
[[ $git_create || $env_virtual ]] && [[ $2 != '' ]] &&
echo "error, or to many arguments." &&
echo "$usage" &&
# Operation not permitted
exit 1
# to many arg for -c
[[ $git_clone ]] && [[ $3 != '' ]] &&
echo "error, to many arguments." &&
echo "$usage" &&
# Operation not permitted
exit 1
}
#------------------#
# START THE SCRIPT #
#------------------#
mcp "$@"