-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgogs
executable file
·999 lines (796 loc) · 22.6 KB
/
gogs
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
#!/bin/bash
# config ----------
gogs_token=""
gogs_token_envvar=GOGS_API_TOKEN
gogs_token_filename=gogs-api-token.txt
gogs_server="" # https://try.gogs.io
gogs_server_envvar=GOGS_SERVER_URL
gogs_server_filename=gogs-server-url.txt
# utils ----------
shopt -s expand_aliases
function configure {
# server url
gogs_server=$(printenv $gogs_server_envvar)
if [ -z "$gogs_server" ] && [ -f "$gogs_server_filename" ]; then
gogs_server=$(cat "$gogs_server_filename")
fi
if [ -z "$gogs_server" ] && [ $# -eq 0 ]; then
echo-warning No Gogs server URL \(e.g. https://try.gogs.io\) configured.
read -p "URL: " gogs_server
echo
if [ -z "$gogs_server" ]; then exit 0; fi
fi
# api token
gogs_token=$(printenv $gogs_token_envvar)
if [ -z "$gogs_token" ] && [ -f "$gogs_token_filename" ]; then
gogs_token=$(cat "$gogs_token_filename")
fi
if [ -z "$gogs_token" ] && [ -f "$gogs_token_filename.gpg" ]; then
gogs_token=$(gpg -q -d "$gogs_token_filename.gpg" 2>/dev/null)
fi
if [ -z "$gogs_token" ] && [ $# -eq 0 ]; then
echo-warning No Gogs API token configured.
read -sp "Token: " gogs_token
echo
if [ -z "$gogs_token" ]; then exit 0; fi
fi
}
function run_curl_and_log {
if is-debug; then
echo "$@" > log_curl_cmd.txt
output=`"$@" 2> log_error.txt`
echo "$output" > log_curl_response.txt
echo -n "$output"
else
"$@" 2> /dev/null
fi
}
function gogs-curl {
method=$1
auth="?token=$gogs_token"
cmd=$2
data="$3"
address=$gogs_server"/api/v1"$cmd$auth
if is-debug; then
echo \#
echo -e "# Method: $method"
echo -e "# Command: $cmd"
echo -e "# Address: $address"
echo -e "# Data: $data"
fi
if ! $dry_run; then
echo
sleep 1s
if [ -n "$data" ]; then
run_curl_and_log curl -d "$data" -H "Content-Type: application/json" -X $method "$address" --insecure
else
run_curl_and_log curl -X $method "$address" --insecure
fi
fi
}
function gogs-curl-basic {
method=$1
auth=$2
cmd=$3
data="$4"
address=$gogs_server"/api/v1"$cmd
if is-debug; then
echo \#
echo -e "# Method: $method"
echo -e "# Auth: $auth"
echo -e "# Command: $cmd"
echo -e "# Address: $address"
echo -e "# Data: $data"
fi
if ! $dry_run; then
echo
sleep 1s
if [ -n "$data" ]; then
run_curl_and_log curl -u $auth -d "$data" -H "Content-Type: application/json" -X $method "$address" --insecure
else
run_curl_and_log curl -u $auth -X $method "$address" --insecure
fi
fi
}
function syntax-help {
if [ $1 -gt $(( $# - 2 )) ]; then local missing_args=true; else local missing_args=false; fi
if $help || [ "$3" = "--help" ]; then local help_requested=true; else local help_requested=false; fi
if $missing_args || $help_requested; then
if [ -z "$2" ]; then
echo Syntax: \(no arguments\)
else
if ! $help_requested; then
echo-error Missing argument\(s\)
fi
echo Syntax: $2
fi
exit 1
fi
}
function echo-error {
echo-colored 31 "$@"
}
function echo-warning {
echo-colored 33 "$@"
}
function echo-success {
echo-colored 32 "$@"
}
function echo-hint {
echo-colored 90 "$@"
}
function echo-colored {
local color=$1
shift
if [ "$1" = "-if" ]; then
condition=$2
shift; shift
if [ $condition -ne 0 ]; then
echo "$@"
return
fi
fi
if [ "$1" = "-if-error" ]; then
condition=$2
shift; shift
if [ $condition -eq 0 ]; then
echo "$@"
return
fi
fi
echo_cmd="echo -e"
while [ -n "$1" ] && [[ "$1" = "-"* ]]; do
echo_cmd="$echo_cmd $1"
shift
done
`echo $echo_cmd` "\033[0;${color}m$@\033[0m"
}
function split-json {
if [ $# -gt 2 ]; then
echo "$3"
else
cat
fi | sed "s/$1$2/$1\n$2/g"
}
function json-property {
if [ $# -gt 1 ]; then
echo "$2"
else
cat
fi | sed "0,/\"$1\"/{s/\"$1\"/@@@/}" | sed -nE "s/^.*@@@:(\
(true)|\
(false)|\
([0-9]+)|\
\"([^\"]*)\"|\
(\[[^]]*\])|\
(\
\{[^{}]*(\
\{[^{}]*(\
\{[^{}]*\}[^{}]*\
)*\}[^{}]*\
)*\}\
)\
).*$/\2\3\4\5\6\7/p"
}
function json-properties {
json="`</dev/stdin`"
for i in $(seq 1 $#); do
echo -n "$json" | json-property ${!i}
echo -n " "
done
echo
}
function per-line {
cat | while IFS='' read -r line; do
echo $line | $1
done
}
function is-number {
case $1 in
''|*[!0-9]*) return 1;;
esac;
}
function is-not-number {
case $1 in
''|*[!0-9]*) return 0;;
esac;
return 1
}
function sub-cmd {
if is-debug; then
echo "# \_ $1"
fi
}
function sub-cmd-no-debug {
sub-cmd "$@"
if [ $debug -gt 0 ]; then
((debug++))
fi
}
alias sub-cmd-error-check='
echo-if-error "$response"; return-exit-code-if-error
if [ $debug -gt 0 ]; then
((debug--))
fi
'
alias sub-cmd-cleanup='
if [ $debug -gt 0 ]; then
((debug--))
fi
'
alias sub-cmd-skip-if-debug='
if $dry_run; then
((debug--))
return 0
fi
'
function is-debug {
if [ $debug -eq 1 ]; then
return 0
fi
return 1
}
# sanity checks ----------
alias return-if-error='
if [ $? -ne 0 ]; then
return 0
fi
'
alias return-exit-code-if-error='
local return_exit_code_if_error_code=$?
if [ $return_exit_code_if_error_code -ne 0 ]; then
return $return_exit_code_if_error_code
fi
'
function check-entity {
if $no_checks; then
return 0
fi
local not=$1
local entity=$2
local check=$3
local desc=$4
shift; shift; shift; shift
sub-cmd-no-debug "get-$entity (check-$check-exists`if $not; then echo -not; fi`)"
sub-cmd-skip-if-debug
`echo get-$entity` "$@" > /dev/null 2>&1
local check_entity_error_code=$?
sub-cmd-cleanup
if ! $not && [ $check_entity_error_code -eq 0 ]; then
echo-error $desc already exists
return 1
elif $not && [ $check_entity_error_code -ne 0 ]; then
echo-error $desc does NOT exist
return 1
fi
return 0
}
function check-entity-exists { check-entity false "$@"; }
function check-entity-exists-not { check-entity true "$@"; }
function check-org-exists { check-entity-exists org org "Organization \`$1\`" $1; }
function check-org-exists-not { check-entity-exists-not org org "Organization \`$1\`" $1; }
function check-user-exists { check-entity-exists user user "User \`$1\`" $1; }
function check-user-exists-not { check-entity-exists-not user user "User \`$1\`" $1; }
function check-user-org-exists { check-entity-exists user user-org "User/Organization \`$1\`" $1; }
function check-user-org-exists-not { check-entity-exists-not user user-org "User/Organization \`$1\`" $1; }
function check-repo-exists { check-entity-exists repo repo "Repository \`$1/$2\`" $1 $2; }
function check-repo-exists-not { check-entity-exists-not repo repo "Repository \`$1/$2\`" $1 $2; }
function check-team-exists { check-entity-exists team team "Team \`$1:$2\`" $1 $2; }
function check-team-exists-not { check-entity-exists-not team team "Team \`$1:$2\`" $1 $2; }
function echo-and-exit-code--error-if-empty {
if [ -n "$1" ]; then
echo "$1"
return 0
fi
return 1
}
function check-message {
local message=`echo "$1" | json-property message`
if [ -n "$message" ]; then
echo-error "$message"
return 1
fi
return 0
}
function echo-and-exit-code--error-if-message {
if [ -n "$1" ]; then
check-message "$1"
return-exit-code-if-error
echo "$1"
fi
return 0
}
function echo-if-not {
if [ $1 -ne 0 ]; then
echo-error "$2"
fi
return $1
}
alias echo-if-error='echo-if-not $?'
# specialized helpers ----------
function resolve-team-id-and-following-name {
if is-number $1; then
id=$1
name=$2
elif [ -z $3 ]; then
echo-error "The provided \`team-id\` is not a number"
return 1
else
check-org-exists-not $1; return-exit-code-if-error
sub-cmd-no-debug "get-teams id (resolve-team-id-and-following-name)"
response=`get-team $1 $2 id`
sub-cmd-error-check
id=$response
name=$3
fi
return 0
}
function resolve-user-org-id {
if is-number $1; then
echo $1
else
check-user-org-exists-not $1; return-exit-code-if-error
sub-cmd-no-debug "get-user id (resolve-user-org-id)"
response=`get-user $1 id`
sub-cmd-error-check
echo $response
fi
return 0
}
function resolve-or-input-password {
if $dry_run; then
password=PASSWORD
elif [ -z "$1" ]; then
read -sp "Password: " password
echo
else
password="$1"
fi
}
# actions ----------
actions=$actions" create-api-token"
function create-api-token {
syntax-help 2 "user-name token-name [password]" "$@"
check-user-exists-not $1; return-exit-code-if-error
resolve-or-input-password "$3"
response=`gogs-curl-basic POST "$1:$password" /users/$1/tokens "{\"name\":\"$2\"}"`
if [ -z "$response" ]; then
echo-error Token could NOT be created. Wrong password?
return 1
elif is-debug; then
echo "$response"
else
echo "$response" | json-property sha1
fi
}
actions=$actions" list-api-tokens"
function list-api-tokens {
syntax-help 1 "user-name [password]" "$@"
check-user-exists-not $1; return-exit-code-if-error
resolve-or-input-password "$2"
response=`gogs-curl-basic GET "$1:$password" /users/$1/tokens`
check-message "$response"; return-exit-code-if-error
if is-debug; then
echo "$response"
else
echo "$response" | split-json "}," "{" | per-line "json-properties sha1 name"
fi
}
actions=$actions" get-api-token-user"
function get-api-token-user {
syntax-help 0 "[property]" "$@"
response=`gogs-curl GET /user`
if [ -z $1 ] || is-debug; then
output="$response"
else
output=`echo "$response" | json-property "$1"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" -"
actions=$actions" (admin) create-user"
function create-user {
syntax-help 3 "user-name email password" "$@"
check-user-exists $1; return-if-error
output=`gogs-curl POST /admin/users "{\"username\":\"$1\",\"email\":\"$2\",\"password\":\"$3\"}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" get-user"
function get-user {
syntax-help 1 "user-name [property]" "$@"
response=`gogs-curl GET /users/$1`
if [ -z "$response" ]; then
echo-error User \`$1\` does NOT exist
return 1
elif [ -z $2 ] || is-debug; then
output="$response"
else
output=`echo "$response" | json-property "$2"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" (admin) delete-user"
function delete-user {
syntax-help 1 "user-name" "$@"
check-user-exists-not $1; return-if-error
output=`gogs-curl DELETE /admin/users/$1`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" -"
actions=$actions" create-own-org"
function create-own-org {
syntax-help 1 "org-name" "$@"
check-org-exists $1; return-if-error
output=`gogs-curl POST /user/orgs "{\"username\":\"$1\"}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" (admin) create-org"
function create-org {
syntax-help 2 "org-name owner-user-name" "$@"
check-org-exists $1; return-if-error
check-user-exists-not $2; return-exit-code-if-error
output=`gogs-curl POST /admin/users/$2/orgs "{\"username\":\"$1\"}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" get-org"
function get-org {
syntax-help 1 "org-name [property]" "$@"
response=`gogs-curl GET /orgs/$1`
if [ -z "$response" ]; then
echo-error Organization \`$1\` does NOT exist
return 1
elif [ -z $2 ] || is-debug; then
output="$response"
else
output=`echo "$response" | json-property "$2"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" -"
actions=$actions" create-own-repo"
function create-own-repo {
syntax-help 3 "repo-name description private:true/false" "$@"
check-repo-exists $1 $2; return-if-error
output=`gogs-curl POST /user/repos "{\"name\":\"$1\",\"description\":\"$2\",\"private\":$3}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" create-org-repo"
function create-org-repo {
syntax-help 4 "org-name repo-name description private:true/false" "$@"
check-org-exists-not $1; return-exit-code-if-error
check-repo-exists $1 $2; return-if-error
output=`gogs-curl POST /org/$1/repos "{\"name\":\"$2\",\"description\":\"$3\",\"private\":$4}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" (admin) create-repo"
function create-repo {
syntax-help 4 "user/org-name repo-name description private:true/false" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
check-repo-exists $1 $2; return-if-error
output=`gogs-curl POST /admin/users/$1/repos "{\"name\":\"$2\",\"description\":\"$3\",\"private\":$4}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" create-mirror-repo"
function create-mirror {
syntax-help 6 "clone-url repo-name (owner-user/org-name | -id) private:true/false mirror:true/false description [user-name password]" "$@"
owner=`resolve-user-org-id $3`
request="\"clone_addr\":\"$1\",\"repo_name\":\"$2\",\"uid\":$owner,\"private\":$4,\"mirror\":$5,\"description\":\"$6\""
if [ ! -z "$7" ]; then
request="$request,\"auth_username\":\"$7\",\"auth_password\":\"$8\""
fi
output=`gogs-curl POST /repos/migrate {"$request"}`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" list-own-repos"
function list-own-repos {
syntax-help 0 "" "$@"
response=`gogs-curl GET /user/repos`
if is-debug; then
echo "$response"
else
echo "$response" | split-json "}}," "{" | per-line "json-property name"
fi
}
actions=$actions" list-org-repos"
function list-org-repos {
syntax-help 1 "org-name" "$@"
check-org-exists-not $1; return-exit-code-if-error
response=`gogs-curl GET /orgs/$1/repos`
if is-debug; then
echo "$response"
else
echo "$response" | split-json "}}," "{" | per-line "json-property name"
fi
}
actions=$actions" list-user-repos"
function list-user-repos {
syntax-help 1 "user-name" "$@"
check-user-exists-not $1; return-exit-code-if-error
response=`gogs-curl GET /users/$1/repos`
if is-debug; then
echo "$response"
else
echo "$response" | split-json "}}," "{" | per-line "json-property name"
fi
}
actions=$actions" get-repo"
function get-repo {
syntax-help 2 "user/org-name repo-name [property]" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
response=`gogs-curl GET /repos/$1/$2`
if [ -z "$response" ]; then
echo-error Repository \`$1/$2\` does NOT exist
return 1
elif [ -z $3 ] || is-debug; then
output="$response"
else
output=`echo "$response" | json-property "$3"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" delete-repo"
function delete-repo {
syntax-help 2 "user/org-name repo-name" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
check-repo-exists-not $1 $2; return-if-error
output=`gogs-curl DELETE /repos/$1/$2`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" -"
actions=$actions" add-collaborator"
function add-collaborator {
syntax-help 3 "user/org-name repo-name user-name [permission:read/write/admin]" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
check-repo-exists-not $1 $2; return-exit-code-if-error
check-user-exists-not $3; return-exit-code-if-error
output=`gogs-curl PUT /repos/$1/$2/collaborators/$3 "{\"permission\":\"$4\"}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" list-collaborators"
function list-collaborators {
syntax-help 2 "user/org-name repo-name" "$@"
get-collaborators $1 $2 username
}
actions=$actions" get-collaborators"
function get-collaborators {
syntax-help 2 "user/org-name repo-name [property]" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
check-repo-exists-not $1 $2; return-exit-code-if-error
response=`gogs-curl GET /repos/$1/$2/collaborators`
if [ -z $3 ] || is-debug; then
output="$response"
else
output=`echo "$response" | split-json "}," "{" | per-line "json-property $3"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" remove-collaborator"
function remove-collaborator {
syntax-help 3 "user/org-name repo-name user-name" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
check-repo-exists-not $1 $2; return-exit-code-if-error
check-user-exists-not $3; return-if-error
output=`gogs-curl DELETE /repos/$1/$2/collaborators/$3`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" -"
actions=$actions" (admin) create-team"
function create-team {
syntax-help 3 "org-name team-name description [permission:read/write/admin]" "$@"
check-org-exists-not $1; return-exit-code-if-error
check-team-exists $1 $2; return-if-error
output=`gogs-curl POST /admin/orgs/$1/teams "{\"name\":\"$2\",\"description\":\"$3\",\"permission\":\"$4\"}"`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" list-teams"
function list-teams {
syntax-help 1 "org-name" "$@"
get-teams $1 name
}
actions=$actions" get-teams"
function get-teams {
syntax-help 1 "org-name [property]" "$@"
check-org-exists-not $1; return-exit-code-if-error
response=`gogs-curl GET /orgs/$1/teams`
if [ -z $2 ] || is-debug; then
output="$response"
else
output=`echo "$response" | split-json "}," "{" | per-line "json-property $2"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" get-team"
function get-team {
syntax-help 2 "org-name team-name [property]" "$@"
sub-cmd-no-debug get-teams
response=`get-teams $1`
sub-cmd-error-check
response=`echo "$response" | split-json "}," "{" | grep "\"name\":\"$2\""`
if [ -z "$response" ]; then
echo-error Team \`$1:$2\` does NOT exist
return 1
elif [ -z $3 ] || is-debug; then
output="$response"
else
output=`echo "$response" | json-property $3`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" -"
actions=$actions" (admin) add-team-member"
function add-team-member {
syntax-help 2 "(org-name team-name | team-id) user-name" "$@"
resolve-team-id-and-following-name "$@"; return-exit-code-if-error
check-user-exists-not $name; return-exit-code-if-error
output=`gogs-curl PUT /admin/teams/$id/members/$name`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" (admin) list-team-members"
function list-team-members {
syntax-help 1 "org-name team-name | team-id" "$@"
resolve-team-id-and-following-name "$@" _; return-exit-code-if-error
get-team-members $1 $2 username
}
actions=$actions" (admin) get-team-members"
function get-team-members {
syntax-help 1 "(org-name team-name | team-id) [property]" "$@"
resolve-team-id-and-following-name "$@" _; return-exit-code-if-error
response=`gogs-curl GET /admin/teams/$id/members`
if [ "$name" = "_" ] || is-debug; then
echo "$response"
else
echo "$response" | split-json "}," "{" | per-line "json-property $name"
fi
}
actions=$actions" (admin) remove-team-member"
function remove-team-member {
syntax-help 2 "(org-name team-name | team-id) user-name" "$@"
resolve-team-id-and-following-name "$@"; return-exit-code-if-error
output=`gogs-curl DELETE /admin/teams/$id/members/$name`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" -"
actions=$actions" (admin) add-team-repo"
function add-team-repo {
syntax-help 2 "(org-name team-name | team-id) repo-name" "$@"
resolve-team-id-and-following-name "$@"; return-exit-code-if-error
output=`gogs-curl PUT /admin/teams/$id/repos/$name`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" (admin) remove-team-repo"
function remove-team-repo {
syntax-help 2 "(org-name team-name | team-id) repo-name" "$@"
resolve-team-id-and-following-name "$@"; return-exit-code-if-error
output=`gogs-curl DELETE /admin/teams/$id/repos/$name`
echo-and-exit-code--error-if-message "$output"
}
actions=$actions" -"
actions=$actions" get-commit"
function get-commit {
syntax-help 3 "user/org-name repo-name branch/tag/sha [property]" "$@"
json=`gogs-curl GET /repos/$1/$2/commits/$3`
if [ -z $4 ] || is-debug; then
output="$json"
else
output=`echo "$json" | json-property "$4"`
fi
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" get-raw"
function get-raw {
syntax-help 3 "user/org-name repo-name ref" "$@"
$output = `gogs-curl GET /repos/$1/$2/raw/$3`
echo-and-exit-code--error-if-empty "$output"
}
actions=$actions" enable-issues"
function enable-issues {
syntax-help 3 "user/org-name repo-name enable:true/false" "$@"
check-user-org-exists-not $1; return-exit-code-if-error
check-repo-exists-not $1 $2; return-exit-code-if-error
output=`gogs-curl PATCH /repos/$1/$2/issue-tracker "{\"enable_issues\":$3}"`
echo-and-exit-code--error-if-message "$output"
}
# cmd interface ----------
function print-help {
echo
echo " Syntax: gogs [OPTIONS] ACTION [ARGUMENTS ...]"
echo
echo " "----------
echo " "OPTIONS
echo
echo " -h | --help Print help of the action"
echo " -d | --dry-run See what commands will be executed, but \do nothing"
echo " -D | --debug debug=1 Enable debug output"
echo " -n | --no-checks Disable sanity checks, e.g. \if an affected org/user/repo exists"
echo
echo " "----------
echo " "ACTIONS
echo
for action in $actions; do
if [ "$action" = "-" ]; then
echo
elif [[ "$action" = "("* ]]; then
info="$action"
else
echo -en " "$action
echo-hint " "$info
info=""
fi
done
configure do-not-ask-if-not-configured
echo
echo " "----------
echo " "CURRENT CONFIGURATION
echo
echo -e " Server: `if [ -z "$gogs_server" ]; then echo-warning NOT-CONFIGURED; fi`$gogs_server"
echo`if [ ! -z "$gogs_server" ]; then echo "-hint"; fi`\
" (Change via environment variable $gogs_server_envvar or $gogs_server_filename)"
echo
echo -e " API token: `if [ -z "$gogs_token" ]; then echo-warning NOT-CONFIGURED; else echo ${gogs_token:0:5}...; fi`"
echo`if [ ! -z "$gogs_server" ]; then echo "-hint"; fi`\
" (Change via environment variable $gogs_token_envvar or $gogs_token_filename[.gpg])\n"\
" (Create one via \`create-api-token\` or"\
"at `if [ -z "$gogs_server" ]; then echo https://YOUR.GOGS.SERVER; fi`$gogs_server/user/settings/applications)"
if [ ! -z "$gogs_server" ] && [ ! -z "$gogs_token" ]; then
echo
echo " Authenticated as:"
echo -n " ... "
response=`get-api-token-user`
r_login=`echo "$response" | json-property username`
r_name=`echo "$response" | json-property full_name`
r_email=`echo "$response" | json-property email`
echo -en "\r $r_login"
if [ ! -z "$r_name" ]; then echo -n " ($r_name)"; fi
echo " / $r_email"
fi
}
help=false
dry_run=false
debug=0
no_checks=false
while [ -n "$1" ] && [[ "$1" = "-"* ]] && [ "$1" != "--" ]; do
case "$1" in
-h | --help) help=true ;;
-d | --dry-run) dry_run=true; debug=1; echo \# DRY RUN \(doing nothing\) ;;
-D | --debug) debug=1; echo \# DEV RUN ;;
-n | --no-checks) no_checks=true ;;
esac
shift
done
if $no_checks && is-debug; then
echo \# SKIP SANITY CHECKS
fi
function run_action {
action=$1
configure
if is-debug; then
echo -e "# Config:\tURL: $gogs_server"
echo -e "#\t\tToken: $gogs_token"
echo \#
echo \# Action: $action
fi
"$@"
}
if [ $# -gt 0 ]; then
action=$1
shift
for a in $actions; do
if [ "$a" = "$action" ] && [ "$a" != "-" ]; then
run_action $action "$@"
exit $?
fi
done
case $action in
--)
action=$1
shift
run_action $action "$@"
;;
*)
echo-error Unrecognized action: \`$action\`
exit 1
;;
esac
else
print-help
fi