-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi
More file actions
executable file
·745 lines (612 loc) · 18.5 KB
/
api
File metadata and controls
executable file
·745 lines (612 loc) · 18.5 KB
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
#!/bin/bash
# api: Compatibility layer between Pi-Apps bash scripts and Go API implementation
# This script defines the same functions as the original Pi-Apps API but redirects calls to the Go implementation
# =================================================
# COMPATIBILITY NOTICE
# =================================================
# This Go implementation introduces several new functions that don't exist in the original Pi-Apps API:
# - file_exists: Checks if a file exists
# - dir_exists: Checks if a directory exists
# - ensure_dir: Creates a directory if it doesn't exist
# - copy_file: Copies a file from one location to another
#
# Using these functions in your scripts will make them incompatible with the bash version of Pi-Apps.
# For maximum compatibility, consider using bash built-ins or standard commands instead:
# - Instead of file_exists: [ -f "/path/to/file" ]
# - Instead of dir_exists: [ -d "/path/to/directory" ]
# - Instead of ensure_dir: mkdir -p "/path/to/directory"
# - Instead of copy_file: cp "/source/file" "/destination/file"
# =================================================
# Path to the Go API binary
GO_API_BIN="$(dirname "$(readlink -f "$0")")/api-go"
# Check if the path contains /usr/bin and switch to PWD if it does
# this a hack to make the script work in the Pi-Apps directory so that it does not return the path from the executed shell via GO_API_BIN
if [[ "$GO_API_BIN" == *"/usr/bin"* ]]; then
GO_API_BIN="$PWD/api-go"
fi
# Set debug mode if enabled in the original environment
if [ "$pi_apps_debug" = true ]; then
GO_API_ARGS="--debug"
else
GO_API_ARGS=""
fi
# Output functions
error() {
"$GO_API_BIN" $GO_API_ARGS error "$1"
local exit_code=$?
return $exit_code
}
warning() {
"$GO_API_BIN" $GO_API_ARGS warning "$1"
}
status() {
if [[ "$1" == '-'* ]] && [ ! -z "$2" ]; then
"$GO_API_BIN" $GO_API_ARGS status "$1" "$2"
else
"$GO_API_BIN" $GO_API_ARGS status "$1"
fi
}
status_green() {
"$GO_API_BIN" $GO_API_ARGS status_green "$1"
}
debug() {
[ "$pi_apps_debug" = true ] && "$GO_API_BIN" $GO_API_ARGS debug "$1"
}
generate_logo() {
"$GO_API_BIN" $GO_API_ARGS generate_logo
}
add_english() {
"$GO_API_BIN" $GO_API_ARGS add_english
}
# Package management functions
package_info() {
"$GO_API_BIN" $GO_API_ARGS package_info "$1"
}
package_installed() {
"$GO_API_BIN" $GO_API_ARGS package_installed "$1"
return $? # Preserve exit code
}
package_available() {
local package="$1"
local dpkg_arch="$2"
if [ -z "$dpkg_arch" ]; then
"$GO_API_BIN" $GO_API_ARGS package_available "$package"
else
"$GO_API_BIN" $GO_API_ARGS package_available "$package" "$dpkg_arch"
fi
return $? # Preserve exit code
}
package_dependencies() {
"$GO_API_BIN" $GO_API_ARGS package_dependencies "$1"
}
package_installed_version() {
"$GO_API_BIN" $GO_API_ARGS package_installed_version "$1"
}
package_latest_version() {
if [ "$2" = "-t" ] && [ ! -z "$3" ]; then
"$GO_API_BIN" $GO_API_ARGS package_latest_version "$1" "-t" "$3"
else
"$GO_API_BIN" $GO_API_ARGS package_latest_version "$1"
fi
}
package_is_new_enough() {
"$GO_API_BIN" $GO_API_ARGS package_is_new_enough "$1" "$2"
return $? # Preserve exit code
}
# File operations
download_file() {
local url="$1"
local destination="$2"
if [ -z "$url" ]; then
error "download_file: No URL specified!"
return 1
fi
if [ -z "$destination" ]; then
error "download_file: No destination specified!"
return 1
fi
# If the GO_API_USE_WGET environment variable is set to "true", use the wget function
if [ "$GO_API_USE_WGET" = "true" ]; then
# Call our wget function to handle the download
wget -q "$url" -O "$destination"
return $?
else
# Otherwise, use the Go API implementation
"$GO_API_BIN" $GO_API_ARGS download_file "$url" "$destination"
return $?
fi
}
# NOTE: NEW FUNCTION - Not available in the original Pi-Apps API
# Using this function will make your script incompatible with the bash version of Pi-Apps
file_exists() {
"$GO_API_BIN" $GO_API_ARGS file_exists "$1"
return $? # Preserve exit code
}
# NOTE: NEW FUNCTION - Not available in the original Pi-Apps API
# Using this function will make your script incompatible with the bash version of Pi-Apps
dir_exists() {
"$GO_API_BIN" $GO_API_ARGS dir_exists "$1"
return $? # Preserve exit code
}
# NOTE: NEW FUNCTION - Not available in the original Pi-Apps API
# Using this function will make your script incompatible with the bash version of Pi-Apps
ensure_dir() {
"$GO_API_BIN" $GO_API_ARGS ensure_dir "$1"
return $? # Preserve exit code
}
# NOTE: NEW FUNCTION - Not available in the original Pi-Apps API
# Using this function will make your script incompatible with the bash version of Pi-Apps
copy_file() {
"$GO_API_BIN" $GO_API_ARGS copy_file "$1" "$2"
return $? # Preserve exit code
}
# Log operations
log_diagnose() {
local logfile="$1"
local allowwrite=""
if [ "$2" = "allowwrite" ]; then
allowwrite="--allow-write"
fi
"$GO_API_BIN" $GO_API_ARGS log_diagnose "$logfile" $allowwrite
# Preserve exit code from the Go implementation
return $?
}
# View log file with GTK3 interface
view_log() {
local logfile="$1"
if [ -z "$logfile" ]; then
error "view_log: No log file specified!"
return 1
fi
if [ ! -f "$logfile" ]; then
warning "view_log: Log file does not exist: $logfile"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS view_log "$logfile"
return $?
}
# General file viewer - for compatibility with original API
view_file() {
local file="$1"
if [ -z "$file" ]; then
error "view_file: No file specified!"
return 1
fi
if [ ! -f "$file" ]; then
warning "view_file: File does not exist: $file"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS view_file "$file"
return $?
}
# Log viewer GUI - shows all log files in a graphical interface
logviewer() {
"$GO_API_BIN" $GO_API_ARGS logviewer
return $?
}
# Category editor - manage app categories
categoryedit() {
if [ $# -eq 2 ]; then
# Command line usage: categoryedit <app> <category>
"$GO_API_BIN" $GO_API_ARGS categoryedit "$1" "$2"
else
# GUI usage: show category editor
"$GO_API_BIN" $GO_API_ARGS categoryedit
fi
return $?
}
# Device information
get_device_info() {
"$GO_API_BIN" $GO_API_ARGS get_device_info
return $?
}
# APT repository management functions
anything_installed_from_uri_suite_component() {
local uri="$1"
local suite="$2"
local component="$3"
if [ -z "$uri" ]; then
error "anything_installed_from_uri_suite_component: A repository uri must be specified."
return 1
fi
if [ -z "$suite" ]; then
error "anything_installed_from_uri_suite_component: A repository suite must be specified."
return 1
fi
"$GO_API_BIN" $GO_API_ARGS anything_installed_from_uri_suite_component "$uri" "$suite" "$component"
return $?
}
remove_repofile_if_unused() {
local file="$1"
local testmode="$2"
local key="$3"
if [ -z "$file" ]; then
error "remove_repofile_if_unused: no sources.list.d file specified!"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS remove_repofile_if_unused "$file" "$testmode" "$key"
return $?
}
# Local APT repository functions
repo_add() {
if [ $# -lt 1 ]; then
error "repo_add: no files specified!"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS repo_add "$@"
return $?
}
repo_refresh() {
"$GO_API_BIN" $GO_API_ARGS repo_refresh
return $?
}
# Remove the local apt repository
repo_rm() {
"$GO_API_BIN" $GO_API_ARGS repo_rm
return $?
}
# Convert an app name to a package name
app_to_pkgname() {
local app="$1"
if [ -z "$app" ]; then
error "app_to_pkgname(): no app-name specified"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS app_to_pkgname "$app"
return $?
}
# APT utility functions
apt_lock_wait() {
"$GO_API_BIN" $GO_API_ARGS apt_lock_wait
return $?
}
apt_update() {
# Pass all arguments to the Go API implementation
"$GO_API_BIN" $GO_API_ARGS apt_update "$@"
return $?
}
less_apt() {
# This function receives input from stdin, filters it, and outputs the result
local input
if [ -p /dev/stdin ]; then
input=$(cat)
echo "$input" | "$GO_API_BIN" $GO_API_ARGS less_apt
else
"$GO_API_BIN" $GO_API_ARGS less_apt "$1"
fi
return $?
}
# Download functionality - implement the original wget function for compatibility
wget() {
# Pass all arguments to the Go implementation
"$GO_API_BIN" $GO_API_ARGS wget "$@"
return $?
}
# Git clone functionality
git_clone() {
# Pass all arguments to the Go implementation
"$GO_API_BIN" $GO_API_ARGS git_clone "$@"
return $?
}
# Chmod with status output
chmod() {
# Pass all arguments to the Go implementation
"$GO_API_BIN" $GO_API_ARGS chmod "$@"
return $?
}
# Unzip with status output
unzip() {
# Pass all arguments to the Go implementation
"$GO_API_BIN" $GO_API_ARGS unzip "$@"
return $?
}
# nproc that considers available memory
nproc() {
# Pass request to the Go implementation
"$GO_API_BIN" $GO_API_ARGS nproc
return $?
}
# Check if process with given PID exists
process_exists() {
local pid="$1"
if [ -z "$pid" ]; then
error "process_exists: No PID specified"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS process_exists "$pid"
return $?
}
# Ensure a kernel module is loaded and configured to load on startup
enable_module() {
local module="$1"
if [ -z "$module" ]; then
error "enable_module: No module name specified"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS enable_module "$module"
return $?
}
# Run command with elevated privileges
sudo_popup() {
if [ $# -lt 1 ]; then
error "sudo_popup: No command specified"
return 1
fi
local command="$1"
shift
"$GO_API_BIN" $GO_API_ARGS sudo_popup "$command" "$@"
return $?
}
# Package installation functions
install_packages() {
if [ -z "$app" ]; then
error "install_packages function can only be used by apps to install packages. (the \$app variable was not set)"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS install_packages "$@"
return $?
}
# Package purging function
purge_packages() {
if [ -z "$app" ]; then
error "purge_packages function can only be used by apps to install packages. (the \$app variable was not set)"
return 1
fi
# Check if script_input is set to "update"
if [ "$script_input" == "update" ]; then
"$GO_API_BIN" $GO_API_ARGS purge_packages "--update"
else
"$GO_API_BIN" $GO_API_ARGS purge_packages
fi
return $?
}
# Package icon finder
get_icon_from_package() {
if [ -z "$1" ]; then
error "get_icon_from_package(): requires an apt package name"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS get_icon_from_package "$@"
return $?
}
# PPA installer for Ubuntu-based distros
ubuntu_ppa_installer() {
local ppa_name="$1"
if [ -z "$ppa_name" ]; then
error "ubuntu_ppa_installer(): This function is used to add a ppa to a ubuntu based install but a required input argument was missing."
return 1
fi
"$GO_API_BIN" $GO_API_ARGS ubuntu_ppa_installer "$ppa_name"
return $?
}
# PPA installer for Debian-based distros
debian_ppa_installer() {
local ppa_name="$1"
local ppa_dist="$2"
local key="$3"
if [ -z "$ppa_name" ] || [ -z "$ppa_dist" ] || [ -z "$key" ]; then
error "debian_ppa_installer(): This function is used to add a ppa to a debian based install but a required input argument was missing."
return 1
fi
"$GO_API_BIN" $GO_API_ARGS debian_ppa_installer "$ppa_name" "$ppa_dist" "$key"
return $?
}
# External repository management
add_external_repo() {
local reponame="$1"
local pubkeyurl="$2"
local uris="$3"
local suites="$4"
local components="$5"
# Check if all needed vars are set
[ -z "$reponame" ] && error "add_external_repo: reponame not set"
[ -z "$uris" ] && error "add_external_repo: uris not set"
[ -z "$suites" ] && error "add_external_repo: suites not set"
[ -z "$pubkeyurl" ] && error "add_external_repo: pubkeyurl not set"
# Build the arguments array
local args=("$reponame" "$pubkeyurl" "$uris" "$suites")
# Add components if provided
if [ ! -z "$components" ]; then
args+=("$components")
else
args+=("")
fi
# Add any additional options
local i=6
while [ $i -le $# ]; do
args+=("${!i}")
i=$((i+1))
done
"$GO_API_BIN" $GO_API_ARGS add_external_repo "${args[@]}"
return $?
}
rm_external_repo() {
local reponame="$1"
local force="$2"
[ -z "$reponame" ] && error "rm_external_repo: reponame not provided"
if [ "$force" == "force" ]; then
"$GO_API_BIN" $GO_API_ARGS rm_external_repo "$reponame" "force"
else
"$GO_API_BIN" $GO_API_ARGS rm_external_repo "$reponame"
fi
return $?
}
# Adoptium repository installer
adoptium_installer() {
"$GO_API_BIN" $GO_API_ARGS adoptium_installer
return $?
}
# Python package management with pipx
pipx_install() {
if [ $# -lt 1 ]; then
error "pipx_install: requires at least one package name"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS pipx_install "$@"
return $?
}
pipx_uninstall() {
if [ $# -lt 1 ]; then
error "pipx_uninstall: requires at least one package name"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS pipx_uninstall "$@"
return $?
}
# App management functions
remove_deprecated_app() {
local app="$1"
local removal_arch="$2"
local message="$3"
[ -z "$app" ] && error "remove_deprecated_app(): requires a pi-apps app name"
"$GO_API_BIN" $GO_API_ARGS remove_deprecated_app "$app" "$removal_arch" "$message"
return $?
}
terminal_manage() {
local action="$1"
local app="$2"
[ -z "$action" ] && error "terminal_manage(): Must specify an action: either 'install' or 'uninstall' or 'update' or 'refresh'"
"$GO_API_BIN" $GO_API_ARGS terminal_manage "$action" "$app"
return $?
}
terminal_manage_multi() {
local queue="$1"
"$GO_API_BIN" $GO_API_ARGS terminal_manage_multi "$queue"
return $?
}
# User interface function for dialogs
userinput_func() {
[ -z "$1" ] && error "userinput_func(): requires a description"
[ -z "$2" ] && error "userinput_func(): requires at least one output selection option"
# Call the Go implementation and capture the output
output=$("$GO_API_BIN" $GO_API_ARGS userinput_func "$@")
return $?
}
# App diagnostics
diagnose_apps() {
local failure_list="$1"
"$GO_API_BIN" $GO_API_ARGS diagnose_apps "$failure_list"
return $?
}
# App creation
createapp() {
"$GO_API_BIN" $GO_API_ARGS createapp "$@"
return $?
}
# App creation
importapp() {
"$GO_API_BIN" $GO_API_ARGS importapp "$@"
return $?
}
# Runonce function
runonce() {
# Pass all arguments to the Go implementation
"$GO_API_BIN" $GO_API_ARGS runonce "$@"
}
# Refresh all package app status files
refresh_all_pkgapp_status() {
"$GO_API_BIN" $GO_API_ARGS refresh_all_pkgapp_status
return $?
}
# Flatpak package management
flatpak_install() {
if [ $# -lt 1 ]; then
error "flatpak_install: requires at least one package name"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS flatpak_install "$@"
return $?
}
flatpak_uninstall() {
if [ $# -lt 1 ]; then
error "flatpak_uninstall: requires at least one package name"
return 1
fi
"$GO_API_BIN" $GO_API_ARGS flatpak_uninstall "$@"
return $?
}
get_pi_app_icon() { #get the path to an app's icon file (icon-64.png)
local app_name="$1"
if [ -z "$app_name" ]; then
error "get_pi_app_icon: app name is required"
fi
"$GO_API_BIN" $GO_API_ARGS get_pi_app_icon "$app_name"
return $?
}
terminal-run() {
"$GO_API_BIN" $GO_API_ARGS terminal-run "$@"
return $?
}
# If script is executed directly (not sourced), handle command line arguments
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# If called with arguments, pass them to the Go API
if [ $# -gt 0 ]; then
"$GO_API_BIN" $GO_API_ARGS "$@"
exit $?
else
# Display usage if no arguments provided
echo "Usage: $(basename "$0") <command> [options]"
echo "This is a compatibility wrapper for the Pi-Apps Go API."
echo "Run '$(basename "$0") --help' for more information."
exit 1
fi
fi
# =================================================
# COMPATIBILITY HELPER FUNCTIONS
# =================================================
# The following functions can be included in your scripts to make them
# compatible with both the original Bash API and the new Go API.
# Simply include these functions in your script to ensure compatibility.
# Compatibility wrapper for file_exists
compat_file_exists() {
local file="$1"
if type file_exists >/dev/null 2>&1; then
# New API with file_exists function
file_exists "$file"
return $?
else
# Original Bash API without file_exists function
[ -f "$file" ]
return $?
fi
}
# Compatibility wrapper for dir_exists
compat_dir_exists() {
local dir="$1"
if type dir_exists >/dev/null 2>&1; then
# New API with dir_exists function
dir_exists "$dir"
return $?
else
# Original Bash API without dir_exists function
[ -d "$dir" ]
return $?
fi
}
# Compatibility wrapper for ensure_dir
compat_ensure_dir() {
local dir="$1"
if type ensure_dir >/dev/null 2>&1; then
# New API with ensure_dir function
ensure_dir "$dir"
return $?
else
# Original Bash API without ensure_dir function
mkdir -p "$dir"
return $?
fi
}
# Compatibility wrapper for copy_file
compat_copy_file() {
local src="$1"
local dst="$2"
if type copy_file >/dev/null 2>&1; then
# New API with copy_file function
copy_file "$src" "$dst"
return $?
else
# Original Bash API without copy_file function
cp "$src" "$dst"
return $?
fi
}