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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
|
#!/bin/bash
# =============================================
# Script: arch-shell
# Description: Arch Linux environment management
# Author: Killian Prin-abeil <killian@archimedeos.org>
# Date: $(date +%Y-%m-%d)
# =============================================
set -euo pipefail
DEBUG=false
NO_COLOR=false
YES=false
QUIET=false
ARCHSHELL_DIR="${HOME}/.arch-shell"
ARCHSHELL_ENVS="${ARCHSHELL_DIR}/envs"
ARCHSHELL_STORE="${ARCHSHELL_DIR}/store"
BASE_TEMPLATE="${ARCHSHELL_STORE}/base-template"
SHARED_CACHE="${ARCHSHELL_DIR}/cache"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
init_colors() {
if [ "$NO_COLOR" = true ]; then
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
}
ask_confirmation() {
local prompt="$1"
local default="${2:-N}"
if [ "$YES" = true ]; then
echo -e "${GREEN}[*] ${prompt} [AUTO-YES]${NC}"
return 0
fi
read -p "$prompt" -r
if [[ $REPLY =~ ^[oOyY]$ ]]; then
return 0
else
return 1
fi
}
quiet_log() {
if [ "$QUIET" != true ]; then
echo -e "$@"
fi
}
error_log() {
echo -e "$@" >&2
}
spinner() {
local pid=$1
local msg="$2"
local spinstr='|/-\\'
local i=0
if [ "$QUIET" = true ]; then
wait $pid
return
fi
tput civis
while kill -0 $pid 2>/dev/null; do
i=$(( (i+1) %4 ))
printf "\r${YELLOW}[*] %s %s${NC}" "${spinstr:$i:1}" "$msg"
sleep 0.1
done
printf "\r${GREEN}[*] %s completed!${NC}\n" "$msg"
tput cnorm
}
debug_log() {
if [ "$DEBUG" = true ]; then
echo -e "${BLUE}[DEBUG]${NC} $*" >&2
fi
}
debug_run() {
local cmd="$*"
debug_log "Execution: $cmd"
if [ "$DEBUG" = true ]; then
eval "$cmd"
return $?
else
eval "$cmd" > /dev/null 2>&1
return $?
fi
}
usage() {
echo "Usage: $0 [--debug] [--no-color] [--yes] [--quiet] <command> [options]"
echo ""
echo "Global options:"
echo " --debug Show commands and outputs in real time"
echo " --no-color Disable colors in output"
echo " --yes Automatically answer 'yes' to confirmations"
echo " --quiet Show only errors"
echo ""
echo "Commands:"
echo " init Initialize arch-shell"
echo " regen-base Regenerate base template"
echo " create <env> Create a new environment"
echo " -S <env> <pkg...> Install packages in an environment"
echo " -R <env> <pkg...> Remove packages from an environment"
echo " enter <env> Enter an environment"
echo " delete <env> Delete an environment"
echo " list List all environments"
echo " info <env> Show environment information"
echo " export <env> <file> Export an environment"
echo " import <file> <env> Import an environment"
echo " run <env> <command> Execute a command in an environment"
echo " clean <env> Clean an environment"
echo " snapshot <env> Manage environment snapshots"
echo " cache <options> Interact with shared cache"
echo " copy <src> <dest> Copy files between local and environment"
exit 1
}
usage_run() {
echo "Usage: $0 run <env> <command> [options]"
echo ""
echo "Execute a command or script in an environment."
echo ""
echo "Options:"
echo " --script <file> Execute a script in the environment"
echo " --env <VAR=value> Set an environment variable"
echo ""
echo "Examples:"
echo " $0 run my-env \"ls -la\""
echo " $0 run my-env \"make clean && make\""
echo " $0 run my-env --script build.sh"
echo " $0 run my-env --env DEBUG=1 \"./my-program\""
echo " $0 run my-env --env BUILD_TYPE=release --script deploy.sh"
exit 1
}
usage_snapshot() {
echo "Usage: $0 snapshot <env> [name] | $0 snapshot <env> <option>"
echo ""
echo "Manages snapshots (backups) of an environment."
echo ""
echo "Options:"
echo " --list List all snapshots of the environment"
echo " --restore <snap> Restore a snapshot (by name or number)"
echo " --delete <snap> Delete a snapshot (by name)"
echo ""
echo "Examples:"
echo " $0 snapshot my-env Create snapshot with automatic name"
echo " $0 snapshot my-env \"before-test\" Create named snapshot"
echo " $0 snapshot my-env --list List snapshots"
echo " $0 snapshot my-env --restore \"before-test\" Restore by name"
echo " $0 snapshot my-env --restore 1 Restore by number"
echo " $0 snapshot my-env --delete \"before-test\" Delete snapshot"
exit 1
}
usage_clean() {
echo "Usage: $0 clean <env> [options]"
echo ""
echo "Cleans temporary files and caches of an environment."
echo ""
echo "Options:"
echo " --cache Clean only pacman caches"
echo " --logs Clean only system logs"
echo " --tmp Clean only temporary files"
echo " --all Clean everything (cache + logs + tmp)"
echo " --dry-run Show what would be cleaned without doing it"
echo " --all-envs Clean all environments"
echo ""
echo "Examples:"
echo " $0 clean my-env Standard cleanup (cache + tmp)"
echo " $0 clean my-env --all Full cleanup"
echo " $0 clean my-env --dry-run Preview cleanup"
echo " $0 clean my-env --cache Only caches"
echo " $0 clean --all-envs Clean all environments"
echo " $0 clean --all-envs --dry-run Preview for all environments"
exit 1
}
usage_cache() {
echo "Usage: $0 cache [options]"
echo ""
echo "Allows management of the shared arch-shell cache."
echo ""
echo "Options:"
echo " --stats Show cache statistics"
echo " --list List packages in cache"
echo " --clean Clean cache"
exit 1
}
usage_copy() {
echo "Usage: $0 copy <source> <destination>"
echo ""
echo "Copy files between local system and environment."
echo ""
echo "Syntax:"
echo " <env>:<path> File or directory in environment"
echo " <path> File or directory on local system"
echo ""
echo "Options:"
echo " -r, --recursive Copy directories recursively"
echo ""
echo "Examples:"
echo " $0 copy my-env:/etc/hosts ./hosts Copy from env to local"
echo " $0 copy ./script.sh my-env:/root/script.sh Copy from local to env"
echo " $0 copy -r ./mydir my-env:/root/mydir Copy directory to env"
echo " $0 copy -r my-env:/var/log ./logs Copy directory from env"
exit 1
}
init_shared_cache() {
debug_log "Initializing shared cache in $SHARED_CACHE"
mkdir -p "$SHARED_CACHE/pkg"
mkdir -p "$SHARED_CACHE/db"
local stats_file="$SHARED_CACHE/cache-stats.json"
if [ ! -f "$stats_file" ]; then
cat > "$stats_file" << EOF
{
"created": "$(date -Iseconds)",
"total_downloads": 0,
"cache_hits": 0,
"total_size": 0,
"packages_count": 0,
"last_updated": "$(date -Iseconds)"
}
EOF
debug_log "Cache statistics file created"
fi
}
update_cache_stats() {
local stats_file="$SHARED_CACHE/cache-stats.json"
local pkg_count=$(find "$SHARED_CACHE/pkg" -name "*.pkg.tar.*" 2>/dev/null | wc -l || echo "0")
local total_size=$(du -sb "$SHARED_CACHE/pkg" 2>/dev/null | awk '{print $1}' || echo "0")
if [ -f "$stats_file" ] && command -v jq >/dev/null 2>&1; then
jq ".packages_count = $pkg_count | .total_size = $total_size | .last_updated = \"$(date -Iseconds)\"" "$stats_file" > "${stats_file}.tmp" && mv "${stats_file}.tmp" "$stats_file"
fi
}
track_cache_hit() {
local pkg="$1"
local stats_file="$SHARED_CACHE/cache-stats.json"
if find "$SHARED_CACHE/pkg" -name "${pkg}-*.pkg.tar.*" -type f 2>/dev/null | head -1 | grep -q .; then
echo -e "${GREEN}[*] Cache hit for ${pkg}${NC}"
if [ -f "$stats_file" ] && command -v jq >/dev/null 2>&1; then
local hits=$(jq -r '.cache_hits // 0' "$stats_file" 2>/dev/null || echo "0")
if [ "$hits" != "null" ] && [ -n "$hits" ]; then
jq ".cache_hits = $((hits + 1))" "$stats_file" > "${stats_file}.tmp" && mv "${stats_file}.tmp" "$stats_file" 2>/dev/null || true
fi
fi
return 0
fi
return 1
}
track_download() {
local stats_file="$SHARED_CACHE/cache-stats.json"
local packages_count="$1"
if [ -f "$stats_file" ] && command -v jq >/dev/null 2>&1; then
local downloads=$(jq -r '.total_downloads // 0' "$stats_file" 2>/dev/null || echo "0")
if [ "$downloads" != "null" ] && [ -n "$downloads" ]; then
jq ".total_downloads = $((downloads + packages_count))" "$stats_file" > "${stats_file}.tmp" && mv "${stats_file}.tmp" "$stats_file" 2>/dev/null || true
fi
fi
}
show_cache_stats() {
local stats_file="$SHARED_CACHE/cache-stats.json"
local cache_size=$(du -sh "$SHARED_CACHE/pkg" 2>/dev/null | awk '{print $1}' || echo "0")
local pkg_count=$(find "$SHARED_CACHE/pkg" -name "*.pkg.tar.*" 2>/dev/null | wc -l || echo "0")
echo -e "${BLUE}[*] arch-shell shared cache statistics:${NC}"
echo -e "${GREEN} Packages in cache: ${pkg_count}${NC}"
echo -e "${GREEN} Cache size: ${cache_size}${NC}"
if [ -f "$stats_file" ] && command -v jq >/dev/null 2>&1; then
local hits=$(jq -r '.cache_hits // 0' "$stats_file" 2>/dev/null || echo "0")
local downloads=$(jq -r '.total_downloads // 0' "$stats_file" 2>/dev/null || echo "0")
local created=$(jq -r '.created // "N/A"' "$stats_file" 2>/dev/null || echo "N/A")
echo -e "${GREEN} Cache hits: ${hits}${NC}"
echo -e "${GREEN} Downloads: ${downloads}${NC}"
echo -e "${GREEN} Created: ${created}${NC}"
if [ "$downloads" -gt 0 ]; then
local hit_rate=$((hits * 100 / (hits + downloads)))
echo -e "${GREEN} Cache hit rate: ${hit_rate}%${NC}"
fi
fi
}
update_env_info() {
local envname="$1"
local envdir="${ARCHSHELL_ENVS}/${envname}"
local infofile="${envdir}/.arch-shell-env"
cat > "$infofile" << EOF
{
"name": "$envname",
"created": "$(date -Iseconds)",
"last_used": "$(date -Iseconds)",
"packages": [],
"arch": "$(uname -m)"
}
EOF
}
update_last_used() {
local envname="$1"
local infofile="${ARCHSHELL_ENVS}/${envname}/.arch-shell-env"
if [ -f "$infofile" ]; then
if command -v jq >/dev/null 2>&1; then
jq ".last_used = \"$(date -Iseconds)\"" "$infofile" > "${infofile}.tmp" && mv "${infofile}.tmp" "$infofile"
else
sed -i "s/\"last_used\": \"[^\"]*\"/\"last_used\": \"$(date -Iseconds)\"/" "$infofile"
fi
fi
}
add_package() {
local envname="$1"
local package="$2"
local infofile="${ARCHSHELL_ENVS}/${envname}/.arch-shell-env"
if [ -f "$infofile" ]; then
if command -v jq >/dev/null 2>&1; then
jq ".packages += [\"$package\"]" "$infofile" > "${infofile}.tmp" && mv "${infofile}.tmp" "$infofile"
else
sed -i "s/\"packages\": \[\([^]]*\)\]/\"packages\": [\1, \"$package\"]/" "$infofile"
fi
fi
}
remove_package() {
local envname="$1"
local package="$2"
local infofile="${ARCHSHELL_ENVS}/${envname}/.arch-shell-env"
if [ -f "$infofile" ]; then
if command -v jq >/dev/null 2>&1; then
jq ".packages -= [\"$package\"]" "$infofile" > "${infofile}.tmp" && mv "${infofile}.tmp" "$infofile"
else
sed -i "s/\"$package\",\?//g; s/,\s*]/]/g; s/\[,/[/g" "$infofile"
fi
fi
}
check_package_in_env() {
local envname="$1"
local package="$2"
local infofile="${ARCHSHELL_ENVS}/${envname}/.arch-shell-env"
if [ -f "$infofile" ]; then
if command -v jq >/dev/null 2>&1; then
jq -e ".packages | index(\"$package\")" "$infofile" >/dev/null 2>&1
else
grep -q "\"$package\"" "$infofile"
fi
else
return 1
fi
}
export_env() {
local envname="$1"
local export_file="$2"
local envdir="${ARCHSHELL_ENVS}/${envname}"
local infofile="${envdir}/.arch-shell-env"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
return 1
fi
if [ ! -f "$infofile" ]; then
echo -e "${RED}[!] Missing metadata file: ${infofile}${NC}"
return 1
fi
if [ -d "$export_file" ]; then
echo -e "${RED}[!] Export file cannot be a directory: ${export_file}${NC}"
echo -e "${YELLOW}[?] Use a filename like: ${envname}-backup.tar.gz${NC}"
return 1
fi
if [ -f "$export_file" ]; then
echo -e "${YELLOW}[?] The file ${export_file} already exists${NC}"
if ! ask_confirmation "Overwrite existing file? [o/N] "; then
echo -e "${YELLOW}[!] Export cancelled${NC}"
return 1
fi
fi
export_file=$(realpath "$export_file" 2>/dev/null || echo "$export_file")
local temp_dir
temp_dir=$(mktemp -d)
local export_dir="${temp_dir}/export"
mkdir -p "$export_dir"
echo -e "${BLUE}[*] Preparing export of ${envname}...${NC}"
cat > "${envdir}/.arch-shell-export-metadata.json" << EOF
{
"arch_shell_version": "$(grep -o 'v[0-9]\+\.[0-9]\+\.[0-9]\+' "$0" 2>/dev/null || echo 'unknown')",
"export_date": "$(date -Iseconds)",
"environment_name": "$envname",
"exported_by": "$(whoami)",
"hostname": "$(hostname)",
"arch": "$(uname -m)"
}
EOF
local total_size
total_size=$(sudo du -sb "$envdir" | awk '{print $1}')
echo -e "${BLUE}[*] Creation of optimized archive ($(numfmt --to=iec $total_size))...${NC}"
(
cd "$envdir"
debug_run "sudo tar --zstd -cf '$export_file' ."
) &
spinner $! "Direct compression with zstd"
rm -f "${envdir}/.arch-shell-export-metadata.json"
local checksum
checksum=$(sha256sum "$export_file" | awk '{print $1}')
rm -rf "$temp_dir"
quiet_log "${GREEN}[*] Export completed: ${export_file}${NC}"
quiet_log "${GREEN}[*] Checksum SHA256 : ${checksum}${NC}"
local archive_size
archive_size=$(du -h "$export_file" | awk '{print $1}')
quiet_log "${GREEN}[*] Archive size: ${archive_size}${NC}"
}
import_env() {
local import_file="$1"
local envname="$2"
local envdir="${ARCHSHELL_ENVS}/${envname}"
if [ ! -f "$import_file" ]; then
echo -e "${RED}[!] Import file not found: ${import_file}${NC}"
return 1
fi
if [ -d "$envdir" ]; then
echo -e "${RED}[!] The environment ${envname} already exists${NC}"
echo -e "${YELLOW}[?] Use .arch-shell delete ${envname}. to remove it first${NC}"
return 1
fi
local temp_dir
temp_dir=$(mktemp -d)
echo -e "${BLUE}[*] Verifying and extracting optimized archive...${NC}"
if tar --zstd -tf "$import_file" >/dev/null 2>&1; then
echo -e "${BLUE}[*] zstd archive detected (optimized format)${NC}"
mkdir -p "$envdir"
(
cd "$envdir"
debug_run "sudo tar --zstd -xf '$import_file'"
) &
spinner $! "Direct extraction with zstd"
if [ -f "${envdir}/.arch-shell-export-metadata.json" ] && command -v jq >/dev/null 2>&1; then
echo -e "${BLUE}[*] Archive information:${NC}"
echo -e "${BLUE} Archive:${NC} $(jq -r '.export_date' "${envdir}/.arch-shell-export-metadata.json" 2>/dev/null || echo 'unknown')"
echo -e "${BLUE} Environment:${NC} $(jq -r '.environment_name' "${envdir}/.arch-shell-export-metadata.json" 2>/dev/null || echo 'unknown')"
echo -e "${BLUE} Exported by:${NC} $(jq -r '.exported_by' "${envdir}/.arch-shell-export-metadata.json" 2>/dev/null || echo 'unknown')"
rm -f "${envdir}/.arch-shell-export-metadata.json"
fi
else
echo -e "${BLUE}[*] gzip archive detected (legacy format)${NC}"
if ! tar xzf "$import_file" -C "$temp_dir" 2>/dev/null; then
echo -e "${RED}[!] Error during archive extraction${NC}"
rm -rf "$temp_dir"
return 1
fi
local export_dir="${temp_dir}/export"
if [ ! -d "$export_dir" ] || [ ! -f "${export_dir}/environment.tar.gz" ]; then
echo -e "${RED}[!] Incomplete or corrupted archive${NC}"
rm -rf "$temp_dir"
return 1
fi
mkdir -p "$envdir"
(
cd "$envdir"
gzip -dc "${export_dir}/environment.tar.gz" | sudo tar xf -
) &
spinner $! "Environment extraction (legacy format)"
if [ -f "${export_dir}/arch-shell-env.json" ]; then
cp "${export_dir}/arch-shell-env.json" "${envdir}/.arch-shell-env"
fi
fi
sudo chown -R $USER: "$envdir"
update_last_used "$envname"
rm -rf "$temp_dir"
echo -e "${GREEN}[*] Import completed: environment ${envname} created${NC}"
}
clean_env() {
local envname="$1"
local clean_cache=false
local clean_logs=false
local clean_tmp=false
local clean_all=false
local dry_run=false
local all_envs=false
shift 1
while [[ $# -gt 0 ]]; do
case $1 in
--cache)
clean_cache=true
shift
;;
--logs)
clean_logs=true
shift
;;
--tmp)
clean_tmp=true
shift
;;
--all)
clean_all=true
shift
;;
--dry-run)
dry_run=true
shift
;;
--all-envs)
all_envs=true
shift
;;
*)
echo -e "${RED}[!] Unknown option: $1${NC}"
return 1
;;
esac
done
if [ "$clean_cache" = false ] && [ "$clean_logs" = false ] && [ "$clean_tmp" = false ] && [ "$clean_all" = false ]; then
clean_cache=true
clean_tmp=true
fi
if [ "$clean_all" = true ]; then
clean_cache=true
clean_logs=true
clean_tmp=true
fi
clean_single_env() {
local env="$1"
local envdir="${ARCHSHELL_ENVS}/${env}"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environnement inexistant : ${env}${NC}"
return 1
fi
echo -e "${BLUE}[*] Cleaning environment ${env}...${NC}"
local total_freed=0
local files_removed=0
if [ "$clean_cache" = true ]; then
local cache_dir="${envdir}/var/cache/pacman/pkg"
if [ -d "$cache_dir" ]; then
local cache_size=$(sudo du -sb "$cache_dir" 2>/dev/null | awk '{print $1}' || echo "0")
local cache_files=$(sudo find "$cache_dir" -name "*.pkg.tar.*" 2>/dev/null | wc -l || echo "0")
if [ "$dry_run" = true ]; then
echo -e "${YELLOW}${YELLOW}[DRY-RUN] Pacman cache: ${NC} ${cache_files} fichiers ($(numfmt --to=iec $cache_size))${NC}"
else
sudo find "$cache_dir" -name "*.pkg.tar.*" -delete 2>/dev/null
echo -e "${GREEN}[✓] Pacman cache cleaned: ${cache_files} fichiers ($(numfmt --to=iec $cache_size))${NC}"
total_freed=$((total_freed + cache_size))
files_removed=$((files_removed + cache_files))
fi
fi
fi
if [ "$clean_logs" = true ]; then
local log_dir="${envdir}/var/log"
if [ -d "$log_dir" ]; then
local log_size=$(sudo du -sb "$log_dir" 2>/dev/null | awk '{print $1}' || echo "0")
local log_files=$(sudo find "$log_dir" -type f -name "*.log*" 2>/dev/null | wc -l || echo "0")
if [ "$dry_run" = true ]; then
echo -e "${YELLOW}${YELLOW}[DRY-RUN] System logs: ${NC} ${log_files} fichiers ($(numfmt --to=iec $log_size))${NC}"
else
sudo find "$log_dir" -type f -name "*.log*" -delete 2>/dev/null
sudo find "$log_dir" -type f -name "*.old" -delete 2>/dev/null
echo -e "${GREEN}[✓] Logs cleaned: ${log_files} fichiers ($(numfmt --to=iec $log_size))${NC}"
total_freed=$((total_freed + log_size))
files_removed=$((files_removed + log_files))
fi
fi
fi
if [ "$clean_tmp" = true ]; then
local tmp_dir="${envdir}/tmp"
if [ -d "$tmp_dir" ]; then
local tmp_size=$(sudo du -sb "$tmp_dir" 2>/dev/null | awk '{print $1}' || echo "0")
local tmp_files=$(sudo find "$tmp_dir" -type f 2>/dev/null | wc -l || echo "0")
if [ "$dry_run" = true ]; then
echo -e "${YELLOW}${YELLOW}[DRY-RUN] Temporary files: ${NC} ${tmp_files} fichiers ($(numfmt --to=iec $tmp_size))${NC}"
else
sudo find "$tmp_dir" -type f -delete 2>/dev/null
echo -e "${GREEN}[✓] Temporary files cleaned: ${tmp_files} fichiers ($(numfmt --to=iec $tmp_size))${NC}"
total_freed=$((total_freed + tmp_size))
files_removed=$((files_removed + tmp_files))
fi
fi
fi
if [ "$dry_run" = false ] && [ $total_freed -gt 0 ]; then
echo -e "${GREEN}[*] Environnement ${env} : ${files_removed} files removed, $(numfmt --to=iec $total_freed) freed${NC}"
update_last_used "$env"
fi
}
if [ "$all_envs" = true ]; then
echo -e "${BLUE}[*] Cleaning all environments...${NC}"
local env_count=0
for env_path in "${ARCHSHELL_ENVS}"/*; do
[ -d "$env_path" ] || continue
local env_basename=$(basename "$env_path")
[ "$env_basename" = "store" ] && continue
[ -f "$env_path/.arch-shell-env" ] || continue
clean_single_env "$env_basename"
env_count=$((env_count + 1))
done
if [ $env_count -eq 0 ]; then
echo -e "${YELLOW}[!] No environments found to clean${NC}"
else
echo -e "${GREEN}[*] $(echo "${GREEN}[*] Cleanup completed for") ${env_count} environment(s) ${env_count} environnement(s)${NC}"
fi
else
if [ -z "$envname" ]; then
echo -e "${RED}[!] Environment name required or use --all-envs${NC}"
return 1
fi
clean_single_env "$envname"
fi
}
run_command() {
local envname="$1"
local command=""
local script_file=""
local env_vars=""
shift 1
while [[ $# -gt 0 ]]; do
case $1 in
--script)
script_file="$2"
shift 2
;;
--env)
env_vars="$env_vars $2"
shift 2
;;
*)
if [ -z "$command" ]; then
command="$1"
else
command="$command $1"
fi
shift
;;
esac
done
local envdir="${ARCHSHELL_ENVS}/${envname}"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
return 1
fi
update_last_used "$envname"
if [ -n "$script_file" ]; then
if [ ! -f "$script_file" ]; then
echo -e "${RED}[!] Script not found: ${script_file}${NC}"
return 1
fi
echo -e "${BLUE}[*] Executing script ${script_file} in ${envname}...${NC}"
local script_name=$(basename "$script_file")
debug_run "sudo mkdir -p '${envdir}/root'"
debug_run "sudo cp '$script_file' '${envdir}/root/${script_name}'"
debug_run "sudo chmod +x '${envdir}/root/${script_name}'"
debug_run "sudo arch-nspawn -c '$SHARED_CACHE/pkg' '$envdir' /bin/bash -c 'cd /root && ./${script_name}'"
local exit_code=$?
debug_run "sudo rm -f '${envdir}/root/${script_name}'"
if [ $exit_code -eq 0 ]; then
echo -e "${GREEN}[*] Script executed successfully${NC}"
else
echo -e "${RED}[!] Script terminated with error code ${exit_code}${NC}"
fi
return $exit_code
else
if [ -z "$command" ]; then
echo -e "${RED}[!] No command specified${NC}"
return 1
fi
echo -e "${BLUE}[*] Executing .${command}. in ${envname}...${NC}"
if [ -n "$env_vars" ]; then
debug_run "sudo arch-nspawn -c '$SHARED_CACHE/pkg' '$envdir' /bin/bash -c 'export $env_vars >/dev/null 2>&1; $command'"
else
debug_run "sudo arch-nspawn -c '$SHARED_CACHE/pkg' '$envdir' /bin/bash -c '$command'"
fi
local exit_code=$?
if [ $exit_code -eq 0 ]; then
echo -e "${GREEN}[*] Command executed successfully${NC}"
else
echo -e "${RED}[!] Command terminated with error code ${exit_code}${NC}"
fi
return $exit_code
fi
}
SNAPSHOTS_DIR="${ARCHSHELL_DIR}/snapshots"
create_snapshot() {
local envname="$1"
local snapshot_name="$2"
local envdir="${ARCHSHELL_ENVS}/${envname}"
local env_snapshots_dir="${SNAPSHOTS_DIR}/${envname}"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
exit 3
fi
mkdir -p "$env_snapshots_dir"
if [ -z "$snapshot_name" ]; then
snapshot_name="$(date +%Y-%m-%d_%H-%M-%S)"
fi
local snapshot_file="${env_snapshots_dir}/${snapshot_name}.tar.zst"
if [ -f "$snapshot_file" ]; then
echo -e "${RED}[!] The snapshot .${snapshot_name}. already exists${NC}"
exit 4
fi
echo -e "${BLUE}[*] Creating snapshot .${snapshot_name}. for ${envname}...${NC}"
(
cd "${ARCHSHELL_ENVS}" && \
debug_run "tar --zstd -cf '$snapshot_file' '${envname}'"
) &
spinner $! "Creating snapshot"
local metadata_file="${env_snapshots_dir}/snapshots.json"
local snapshot_size=$(du -h "$snapshot_file" | cut -f1)
local snapshot_date=$(date -Iseconds)
if [ ! -f "$metadata_file" ]; then
echo "[]" > "$metadata_file"
fi
if command -v jq >/dev/null 2>&1; then
jq ". += [{\"name\": \"$snapshot_name\", \"date\": \"$snapshot_date\", \"size\": \"$snapshot_size\"}]" "$metadata_file" > "${metadata_file}.tmp" && mv "${metadata_file}.tmp" "$metadata_file"
else
echo " {\"name\": \"$snapshot_name\", \"date\": \"$snapshot_date\", \"size\": \"$snapshot_size\"}" >> "$metadata_file.simple"
fi
echo -e "${GREEN}[*] Snapshot .${snapshot_name}. created (${snapshot_size})${NC}"
}
list_snapshots() {
local envname="$1"
local env_snapshots_dir="${SNAPSHOTS_DIR}/${envname}"
if [ ! -d "${ARCHSHELL_ENVS}/${envname}" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
exit 3
fi
if [ ! -d "$env_snapshots_dir" ] || [ -z "$(ls -A "$env_snapshots_dir"/*.tar.{zst,xz} 2>/dev/null)" ]; then
echo -e "${YELLOW}[*] No snapshots found for environment ${envname}${NC}"
return
fi
echo -e "${BLUE}[*] Snapshots for environment '${envname}':${NC}"
local i=1
for snapshot_file in "$env_snapshots_dir"/*.tar.{zst,xz}; do
if [ -f "$snapshot_file" ]; then
local snapshot_name=$(basename "$snapshot_file")
snapshot_name=${snapshot_name%.tar.*}
local snapshot_size=$(du -h "$snapshot_file" | cut -f1)
local snapshot_date=$(stat -c %y "$snapshot_file" | cut -d. -f1)
local format=""
if [[ "$snapshot_file" == *.tar.zst ]]; then
format=" (zstd)"
else
format=" (legacy xz)"
fi
echo -e " ${GREEN}${i}.${NC} ${snapshot_name} - ${snapshot_size} (${snapshot_date})${format}"
((i++))
fi
done
}
restore_snapshot() {
local envname="$1"
local snapshot_name="$2"
local envdir="${ARCHSHELL_ENVS}/${envname}"
local env_snapshots_dir="${SNAPSHOTS_DIR}/${envname}"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
exit 3
fi
local snapshot_file
if [[ "$snapshot_name" =~ ^[0-9]+$ ]]; then
local i=1
for file in "$env_snapshots_dir"/*.tar.{zst,xz}; do
if [ -f "$file" ] && [ "$i" -eq "$snapshot_name" ]; then
snapshot_file="$file"
break
fi
((i++))
done
else
if [ -f "${env_snapshots_dir}/${snapshot_name}.tar.zst" ]; then
snapshot_file="${env_snapshots_dir}/${snapshot_name}.tar.zst"
elif [ -f "${env_snapshots_dir}/${snapshot_name}.tar.xz" ]; then
snapshot_file="${env_snapshots_dir}/${snapshot_name}.tar.xz"
else
snapshot_file="${env_snapshots_dir}/${snapshot_name}.tar.zst"
fi
fi
if [ ! -f "$snapshot_file" ]; then
echo -e "${RED}[!] Snapshot .${snapshot_name}. not found${NC}"
exit 5
fi
echo -e "${YELLOW}[!] WARNING: This operation will replace the current environment${NC}"
if ! ask_confirmation "Continue? [o/N] "; then
echo "Cancelled."
exit 0
fi
echo -e "${BLUE}[*] Restoring snapshot for ${envname}...${NC}"
local backup_name="backup-avant-restore-$(date +%Y%m%d%H%M%S)"
mv "$envdir" "${envdir}.${backup_name}"
(
cd "${ARCHSHELL_ENVS}" && \
if [[ "$snapshot_file" == *.tar.zst ]]; then
debug_run "tar --zstd -xf '$snapshot_file'"
else
debug_run "tar -xJf '$snapshot_file'"
fi
) &
if [[ "$snapshot_file" == *.tar.zst ]]; then
spinner $! "Snapshot restoration (zstd)"
else
spinner $! "Snapshot restoration (legacy xz)"
fi
if [ -d "$envdir" ]; then
sudo rm -rf "${envdir}.${backup_name}"
echo -e "${GREEN}[*] Snapshot restored successfully${NC}"
else
mv "${envdir}.${backup_name}" "$envdir"
echo -e "${RED}[!] Restoration failed, environment restored${NC}"
exit 6
fi
}
delete_snapshot() {
local envname="$1"
local snapshot_name="$2"
local env_snapshots_dir="${SNAPSHOTS_DIR}/${envname}"
local snapshot_file="${env_snapshots_dir}/${snapshot_name}.tar.zst"
if [ ! -d "${ARCHSHELL_ENVS}/${envname}" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
exit 3
fi
if [ ! -f "$snapshot_file" ]; then
echo -e "${RED}[!] Snapshot .${snapshot_name}. not found${NC}"
exit 5
fi
if ask_confirmation "Delete snapshot permanently '${snapshot_name}' ? [o/N] "; then
rm -f "$snapshot_file"
local metadata_file="${env_snapshots_dir}/snapshots.json"
if [ -f "$metadata_file" ] && command -v jq >/dev/null 2>&1; then
jq "map(select(.name != \"$snapshot_name\"))" "$metadata_file" > "${metadata_file}.tmp" && mv "${metadata_file}.tmp" "$metadata_file"
fi
echo -e "${GREEN}[*] Snapshot '${snapshot_name}' deleted${NC}"
else
echo "Cancelled."
fi
}
copy_file() {
local recursive=false
local src=""
local dest=""
while [[ $# -gt 0 ]]; do
case $1 in
-r|--recursive)
recursive=true
shift
;;
*)
if [ -z "$src" ]; then
src="$1"
elif [ -z "$dest" ]; then
dest="$1"
else
echo -e "${RED}[!] Too many arguments${NC}"
usage_copy
fi
shift
;;
esac
done
if [ -z "$src" ] || [ -z "$dest" ]; then
echo -e "${RED}[!] Source and destination required${NC}"
usage_copy
fi
local src_env=""
local src_path=""
local dest_env=""
local dest_path=""
if [[ "$src" =~ ^([^:]+):(.+)$ ]]; then
src_env="${BASH_REMATCH[1]}"
src_path="${BASH_REMATCH[2]}"
else
src_path="$src"
fi
if [[ "$dest" =~ ^([^:]+):(.+)$ ]]; then
dest_env="${BASH_REMATCH[1]}"
dest_path="${BASH_REMATCH[2]}"
else
dest_path="$dest"
fi
if [ -n "$src_env" ] && [ -n "$dest_env" ]; then
echo -e "${RED}[!] Cannot copy directly between two environments${NC}"
echo -e "${YELLOW}[?] Copy from env to local first, then from local to env${NC}"
exit 1
fi
if [ -z "$src_env" ] && [ -z "$dest_env" ]; then
echo -e "${RED}[!] At least one path must reference an environment (env:path)${NC}"
usage_copy
fi
local envname=""
local envdir=""
if [ -n "$src_env" ]; then
envname="$src_env"
envdir="${ARCHSHELL_ENVS}/${envname}"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
exit 3
fi
local full_src_path="${envdir}${src_path}"
if [ ! -e "$full_src_path" ]; then
echo -e "${RED}[!] Source file not found in environment: ${src_path}${NC}"
exit 1
fi
if [ -d "$full_src_path" ] && [ "$recursive" = false ]; then
echo -e "${RED}[!] Source is a directory. Use -r or --recursive${NC}"
exit 1
fi
echo -e "${BLUE}[*] Copying from environment ${envname}:${src_path} to ${dest_path}...${NC}"
if [ "$recursive" = true ]; then
debug_run "sudo cp -r '$full_src_path' '$dest_path'"
else
debug_run "sudo cp '$full_src_path' '$dest_path'"
fi
if [ $? -eq 0 ]; then
debug_run "sudo chown -R $USER: '$dest_path'"
echo -e "${GREEN}[*] Copy completed successfully${NC}"
update_last_used "$envname"
else
echo -e "${RED}[!] Copy failed${NC}"
exit 1
fi
elif [ -n "$dest_env" ]; then
envname="$dest_env"
envdir="${ARCHSHELL_ENVS}/${envname}"
if [ ! -d "$envdir" ]; then
echo -e "${RED}[!] Environment not found: ${envname}${NC}"
exit 3
fi
if [ ! -e "$src_path" ]; then
echo -e "${RED}[!] Source file not found: ${src_path}${NC}"
exit 1
fi
if [ -d "$src_path" ] && [ "$recursive" = false ]; then
echo -e "${RED}[!] Source is a directory. Use -r or --recursive${NC}"
exit 1
fi
local full_dest_path="${envdir}${dest_path}"
local dest_parent_dir=$(dirname "$full_dest_path")
echo -e "${BLUE}[*] Copying from ${src_path} to environment ${envname}:${dest_path}...${NC}"
debug_run "sudo mkdir -p '$dest_parent_dir'"
if [ "$recursive" = true ]; then
debug_run "sudo cp -r '$src_path' '$full_dest_path'"
else
debug_run "sudo cp '$src_path' '$full_dest_path'"
fi
if [ $? -eq 0 ]; then
echo -e "${GREEN}[*] Copy completed successfully${NC}"
update_last_used "$envname"
else
echo -e "${RED}[!] Copy failed${NC}"
exit 1
fi
fi
}
while [ $# -gt 0 ]; do
case "$1" in
--debug)
DEBUG=true
shift
debug_log "Debug mode enabled"
;;
--no-color)
NO_COLOR=true
shift
debug_log "No-color mode enabled"
;;
--yes)
YES=true
shift
debug_log "Automatic mode enabled"
;;
--quiet)
QUIET=true
shift
debug_log "Quiet mode enabled"
;;
*)
break
;;
esac
done
init_colors
if [ $# -lt 1 ]; then
usage
fi
case "$1" in
init)
mkdir -p "$ARCHSHELL_STORE"
init_shared_cache
if [ -d "$BASE_TEMPLATE" ]; then
echo "Base template already exists. Use 'regen-base' to regenerate it."
else
echo -e "${BLUE}[*] Creating base template...${NC}"
(
debug_run "sudo mkarchroot '$BASE_TEMPLATE' base"
) &
spinner $! "Creating base template"
echo -e "${GREEN}[*] Base template created.${NC}"
fi
;;
regen-base)
mkdir -p "$ARCHSHELL_STORE"
init_shared_cache
echo -e "${BLUE}[*] Regenerating base template...${NC}"
if [ -d "$BASE_TEMPLATE" ]; then
debug_run "sudo rm -rf '$BASE_TEMPLATE'"
fi
(
debug_run "sudo mkarchroot '$BASE_TEMPLATE' base"
) &
spinner $! "Regenerating base template"
echo -e "${GREEN}[*] Base template regenerated.${NC}"
;;
create)
if [ $# -lt 2 ]; then
echo "Usage: $0 create <env>"
exit 1
fi
ENVNAME="$2"
if [ ! -d "$BASE_TEMPLATE" ]; then
echo "Base template not found. Run d'abord '$0 init'."
exit 2
fi
if [ -d "${ARCHSHELL_ENVS}/${ENVNAME}" ]; then
echo "The environment ${ENVNAME} already exists."; exit 5;
fi
echo -e "${BLUE}[*] Creating environment $ENVNAME from template...${NC}"
mkdir -p "${ARCHSHELL_ENVS}/${ENVNAME}"
total=$(sudo du -sb "$BASE_TEMPLATE" | awk '{print $1}')
(
debug_run "sudo rsync -a --info=progress2 '$BASE_TEMPLATE/' '${ARCHSHELL_ENVS}/${ENVNAME}/'"
) &
spinner $! "Copying template"
sudo chown -R $USER: "${ARCHSHELL_ENVS}/${ENVNAME}"
update_env_info "$ENVNAME"
echo -e "${GREEN}[*] Environment .*ENVNAME created.${NC}"
;;
-S)
if [ $# -lt 3 ]; then
echo "Usage: $0 -S <env> <pkg...>"
exit 1
fi
ENVNAME="$2"
shift 2
if [ ! -d "${ARCHSHELL_ENVS}/${ENVNAME}" ]; then
echo "Environment not found: ${ENVNAME}"; exit 3;
fi
init_shared_cache
cache_hits=0
total_packages=$#
for pkg in "$@"; do
if track_cache_hit "$pkg"; then
cache_hits=$((cache_hits + 1))
fi
done
new_downloads=$((total_packages - cache_hits))
echo -e "${BLUE}[*] Installing package(s) $* in $ENVNAME...${NC}"
(
debug_run "sudo arch-nspawn -c '$SHARED_CACHE/pkg' '${ARCHSHELL_ENVS}/${ENVNAME}' pacman -S --noconfirm $*"
) &
install_pid=$!
spinner $install_pid "Installing packages"
wait $install_pid
install_result=$?
if [ $install_result -eq 0 ]; then
echo -e "${GREEN}[*] Installation completed.${NC}"
update_last_used "$ENVNAME"
for pkg in "$@"; do
add_package "$ENVNAME" "$pkg"
done
if [ $new_downloads -gt 0 ]; then
track_download $new_downloads
fi
update_cache_stats
else
echo -e "${RED}[!] Installation failed (code: $install_result)${NC}"
exit $install_result
fi
;;
-R)
if [ $# -lt 3 ]; then
echo "Usage: $0 -R <env> <pkg...>"
exit 1
fi
ENVNAME="$2"
shift 2
if [ ! -d "${ARCHSHELL_ENVS}/${ENVNAME}" ]; then
echo "Environment not found: ${ENVNAME}"; exit 3;
fi
init_shared_cache
missing_packages=()
for pkg in "$@"; do
if ! check_package_in_env "$ENVNAME" "$pkg"; then
missing_packages+=("$pkg")
fi
done
if [ ${#missing_packages[@]} -gt 0 ]; then
echo -e "${RED}[!] The following packages are not installed in environment ${ENVNAME} :${NC}"
for pkg in "${missing_packages[@]}"; do
echo " - $pkg"
done
echo -e "${YELLOW}[!] Only packages present in .arch-shell-env can be removed.${NC}"
exit 4
fi
echo -e "${BLUE}[*] Removing package(s) $* in $ENVNAME...${NC}"
(
debug_run "sudo arch-nspawn -c '$SHARED_CACHE/pkg' '${ARCHSHELL_ENVS}/${ENVNAME}' pacman -R --noconfirm $*"
) &
remove_pid=$!
spinner $remove_pid "Removing packages"
wait $remove_pid
remove_result=$?
if [ $remove_result -eq 0 ]; then
echo -e "${GREEN}[*] Removal completed.${NC}"
update_last_used "$ENVNAME"
for pkg in "$@"; do
remove_package "$ENVNAME" "$pkg"
done
else
echo -e "${RED}[!] Removal failed (code: $remove_result)${NC}"
exit $remove_result
fi
;;
enter)
if [ $# -lt 2 ]; then
echo "Usage: $0 enter <env>"
exit 1
fi
ENVNAME="$2"
if [ ! -d "${ARCHSHELL_ENVS}/${ENVNAME}" ]; then
echo "Environment not found: ${ENVNAME}"; exit 3;
fi
init_shared_cache
echo "Entering environment $ENVNAME."
update_last_used "$ENVNAME"
sudo arch-nspawn -c "$SHARED_CACHE/pkg" "${ARCHSHELL_ENVS}/${ENVNAME}"
;;
delete)
if [ $# -lt 2 ]; then
echo "Usage: $0 delete <env>"
exit 1
fi
ENVNAME="$2"
if [ ! -d "${ARCHSHELL_ENVS}/${ENVNAME}" ]; then
echo "Environment not found: ${ENVNAME}"; exit 3;
fi
if ask_confirmation "Delete ${ENVNAME} permanently? [o/N] "; then
sudo rm -rf "${ARCHSHELL_ENVS}/${ENVNAME}"
LOCKFILE="${ARCHSHELL_ENVS}/${ENVNAME}.lock"
if [ -f "$LOCKFILE" ]; then
sudo rm -f "$LOCKFILE"
fi
echo "Environment deleted."
else
echo "Cancelled."
fi
;;
list)
echo "Available environments:"
for env in "$ARCHSHELL_ENVS"/*; do
[ -d "$env" ] || continue
[ "$(basename "$env")" = "store" ] && continue
if [ -f "$env/.arch-shell-env" ]; then
echo "- $(basename "$env")"
fi
done
;;
info)
if [ $# -lt 2 ]; then
echo "Usage: $0 info <env>"
exit 1
fi
ENVNAME="$2"
INFOFILE="${ARCHSHELL_ENVS}/${ENVNAME}/.arch-shell-env"
if [ ! -f "$INFOFILE" ]; then
echo "Environment not found: ${ENVNAME}"; exit 3;
fi
echo "Information about environment ${ENVNAME}:"
if command -v jq >/dev/null 2>&1; then
jq '.' "$INFOFILE"
else
cat "$INFOFILE"
fi
;;
export)
if [ $# -lt 3 ]; then
echo "Usage: $0 export <env> <file>"
exit 1
fi
ENVNAME="$2"
EXPORT_FILE="$3"
export_env "$ENVNAME" "$EXPORT_FILE"
;;
import)
if [ $# -lt 3 ]; then
echo "Usage: $0 import <file> <env>"
exit 1
fi
IMPORT_FILE="$2"
ENVNAME="$3"
import_env "$IMPORT_FILE" "$ENVNAME"
;;
run)
if [ $# -lt 2 ]; then
usage_run
fi
ENVNAME="$2"
shift 2
run_command "$ENVNAME" "$@"
;;
clean)
if [ $# -lt 1 ]; then
usage_clean
fi
if [ $# -ge 2 ] && [ "$2" = "--all-envs" ]; then
shift 1
clean_env "" "$@"
else
if [ $# -lt 2 ]; then
usage_clean
fi
ENVNAME="$2"
shift 2
clean_env "$ENVNAME" "$@"
fi
;;
snapshot)
if [ $# -lt 2 ]; then
usage_snapshot
fi
if [ $# -ge 3 ] && [ "$3" = "--list" ]; then
list_snapshots "$2"
elif [ $# -ge 3 ] && [ "$3" = "--restore" ]; then
if [ $# -lt 4 ]; then
echo "Usage: $0 snapshot <env> --restore <snapshot>"
exit 1
fi
restore_snapshot "$2" "$4"
elif [ $# -ge 3 ] && [ "$3" = "--delete" ]; then
if [ $# -lt 4 ]; then
echo "Usage: $0 snapshot <env> --delete <snapshot>"
exit 1
fi
delete_snapshot "$2" "$4"
else
create_snapshot "$2" "${3:-}"
fi
;;
cache)
if [ $# -lt 2 ]; then
usage_cache
fi
case "${2:-}" in
--stats)
init_shared_cache
show_cache_stats
;;
--list)
init_shared_cache
echo -e "${BLUE}[*] Packages in shared cache:${NC}"
if [ ! -d "$SHARED_CACHE/pkg" ] || [ -z "$(ls -A "$SHARED_CACHE/pkg" 2>/dev/null)" ]; then
echo -e "${YELLOW}[*] Aucun paquet dans le cache${NC}"
else
total_count=0
total_size=0
declare -A seen_packages
for pkg in "$SHARED_CACHE/pkg"/*.pkg.tar.*; do
[ -f "$pkg" ] || continue
pkg_name=$(basename "$pkg")
[[ "$pkg_name" == *.sig ]] && continue
pkg_clean=$(echo "$pkg_name" | sed 's/\.pkg\.tar\..*//')
[ "${seen_packages[$pkg_clean]:-}" ] && continue
seen_packages[$pkg_clean]=1
pkg_size=$(stat -c%s "$pkg" 2>/dev/null || echo "0")
echo -e " ${GREEN}•${NC} $pkg_clean ($(numfmt --to=iec $pkg_size))"
total_count=$((total_count + 1))
total_size=$((total_size + pkg_size))
done
echo ""
echo -e "${BLUE}[*] Total: .* packages ($(numfmt --to=iec $total_size))${NC}"
fi
;;
--clean)
init_shared_cache
days_old="${3:-30}"
echo -e "${BLUE}[*] Cleaning shared cache (packages > ${days_old} days)...${NC}"
before_count=$(find "$SHARED_CACHE/pkg" -name "*.pkg.tar.*" 2>/dev/null | wc -l || echo "0")
before_size=$(du -sb "$SHARED_CACHE/pkg" 2>/dev/null | awk '{print $1}' || echo "0")
find "$SHARED_CACHE/pkg" -name "*.pkg.tar.*" -mtime +$days_old -delete 2>/dev/null
after_count=$(find "$SHARED_CACHE/pkg" -name "*.pkg.tar.*" 2>/dev/null | wc -l || echo "0")
after_size=$(du -sb "$SHARED_CACHE/pkg" 2>/dev/null | awk '{print $1}' || echo "0")
freed=$((before_size - after_size))
removed=$((before_count - after_count))
echo -e "${GREEN}[*] Cache cleaned: .* packages removed ($(numfmt --to=iec $freed) freed)${NC}"
update_cache_stats
;;
*)
exit 1
;;
esac
;;
copy)
if [ $# -lt 3 ]; then
usage_copy
fi
shift 1
copy_file "$@"
;;
*)
usage
;;
esac
|