cargo_shuttle/
lib.rs

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
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
mod args;
pub mod config;
mod init;
mod provisioner_server;
mod suggestions;
mod util;

use std::collections::{BTreeMap, HashMap};
use std::ffi::OsString;
use std::fs::{read_to_string, File};
use std::io::{Read, Write};
use std::net::{Ipv4Addr, SocketAddr};
use std::path::{Path, PathBuf};
use std::process::exit;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use chrono::Utc;
use clap::{parser::ValueSource, CommandFactory, FromArgMatches};
use crossterm::style::Stylize;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password, Select};
use flate2::write::GzEncoder;
use flate2::Compression;
use futures::{SinkExt, StreamExt, TryFutureExt};
use git2::Repository;
use globset::{Glob, GlobSetBuilder};
use ignore::overrides::OverrideBuilder;
use ignore::WalkBuilder;
use indicatif::ProgressBar;
use indoc::{formatdoc, printdoc};
use reqwest::header::HeaderMap;
use shuttle_api_client::ShuttleApiClient;
use shuttle_common::{
    constants::{
        headers::X_CARGO_SHUTTLE_VERSION, API_URL_DEFAULT, API_URL_DEFAULT_BETA,
        DEFAULT_IDLE_MINUTES, EXAMPLES_REPO, EXECUTABLE_DIRNAME, RESOURCE_SCHEMA_VERSION,
        RUNTIME_NAME, SHUTTLE_IDLE_DOCS_URL, SHUTTLE_LEGACY_NEW_PROJECT, STORAGE_DIRNAME,
        TEMPLATES_SCHEMA_VERSION,
    },
    deployment::{DeploymentStateBeta, DEPLOYER_END_MESSAGES_BAD, DEPLOYER_END_MESSAGES_GOOD},
    log::LogsRange,
    models::{
        auth::{KeyMessage, TokenMessage},
        deployment::{
            deployments_table_beta, get_deployments_table, BuildArgsBeta, BuildArgsRustBeta,
            BuildMetaBeta, DeploymentRequest, DeploymentRequestBeta,
            DeploymentRequestBuildArchiveBeta, DeploymentRequestImageBeta, DeploymentResponseBeta,
            CREATE_SERVICE_BODY_LIMIT, GIT_STRINGS_MAX_LENGTH,
        },
        error::ApiError,
        project::{self, ProjectUpdateRequestBeta},
        resource::{get_certificates_table_beta, get_resource_tables, get_resource_tables_beta},
    },
    resource::{self, ResourceInput, ShuttleResourceOutput},
    semvers_are_compatible, DatabaseResource, DbInput, LogItem, LogItemBeta, VersionInfo,
};
use shuttle_proto::{
    provisioner::{provisioner_server::Provisioner, DatabaseRequest},
    runtime::{self, LoadRequest, StartRequest, StopRequest},
};
use shuttle_service::{
    builder::{async_cargo_metadata, build_workspace, find_shuttle_packages, BuiltService},
    runner, Environment,
};
use strum::{EnumMessage, VariantArray};
use tar::Builder;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Child;
use tokio::time::{sleep, Duration};
use tokio_tungstenite::tungstenite::Message;
use tonic::{Request, Status};
use tracing::{debug, error, info, trace};
use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter};
use uuid::Uuid;
use zip::write::FileOptions;

use crate::args::{
    CertificateCommand, ConfirmationArgs, DeployArgs, DeploymentCommand, GenerateCommand, InitArgs,
    LoginArgs, LogoutArgs, LogsArgs, ProjectCommand, ProjectStartArgs, ProjectUpdateCommand,
    ResourceCommand, SecretsArgs, TableArgs, TemplateLocation,
};
pub use crate::args::{Command, ProjectArgs, RunArgs, ShuttleArgs};
use crate::config::RequestContext;
use crate::provisioner_server::beta::{ProvApiState, ProvisionerServerBeta};
use crate::provisioner_server::LocalProvisioner;
use crate::util::{
    check_and_warn_runtime_version, generate_completions, generate_manpage, get_templates_schema,
    is_dirty, open_gh_issue, read_ws_until_text, update_cargo_shuttle,
};

const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Returns the args and whether the PATH arg of the init command was explicitly given
pub fn parse_args() -> (ShuttleArgs, bool) {
    let matches = ShuttleArgs::command().get_matches();
    let args =
        ShuttleArgs::from_arg_matches(&matches).expect("args to already be parsed successfully");
    let provided_path_to_init = matches
        .subcommand_matches("init")
        .is_some_and(|init_matches| {
            init_matches.value_source("path") == Some(ValueSource::CommandLine)
        });

    (args, provided_path_to_init)
}

pub fn setup_tracing(debug: bool) {
    registry()
        .with(fmt::layer())
        .with(
            // let user set RUST_LOG if they want to
            EnvFilter::try_from_default_env().unwrap_or_else(|_| {
                if debug {
                    EnvFilter::new("info,cargo_shuttle=trace,shuttle=trace")
                } else {
                    EnvFilter::default()
                }
            }),
        )
        .init();
}

#[derive(PartialEq)]
pub enum Binary {
    CargoShuttle,
    Shuttle,
}

impl Binary {
    pub fn name(&self) -> String {
        match self {
            Self::CargoShuttle => "cargo-shuttle".to_owned(),
            Self::Shuttle => "shuttle".to_owned(),
        }
    }
}

pub struct Shuttle {
    ctx: RequestContext,
    client: Option<ShuttleApiClient>,
    version_info: Option<VersionInfo>,
    /// Strings to print at the end of command execution
    version_warnings: Vec<String>,
    /// Alter behaviour to interact with the new platform
    beta: bool,
    /// Alter behaviour based on which CLI is used
    bin: Binary,
}

impl Shuttle {
    pub fn new(bin: Binary) -> Result<Self> {
        let ctx = RequestContext::load_global()?;
        Ok(Self {
            ctx,
            client: None,
            version_info: None,
            version_warnings: vec![],
            beta: false,
            bin,
        })
    }

    pub async fn run(mut self, mut args: ShuttleArgs, provided_path_to_init: bool) -> Result<()> {
        if self.bin == Binary::Shuttle {
            // beta is always enabled in `shuttle`
            args.beta = true;
        }
        self.beta = args.beta;
        if self.beta {
            if matches!(args.cmd, Command::Project(ProjectCommand::Restart { .. })) {
                bail!("This command is discontinued on the NEW platform (shuttle.dev). Deploy to start a new deployment.");
            }
            if matches!(args.cmd, Command::Status) {
                bail!("This command is discontinued on the NEW platform (shuttle.dev). Use `deployment status` instead.");
            }
            if matches!(
                args.cmd,
                Command::Stop | Command::Project(ProjectCommand::Stop { .. })
            ) {
                bail!("This command is discontinued on the NEW platform (shuttle.dev). Use `deployment stop` instead.");
            }
            if matches!(args.cmd, Command::Clean) {
                bail!("This command is discontinued on the NEW platform (shuttle.dev).");
            }
            if matches!(args.cmd, Command::Resource(ResourceCommand::Dump { .. })) {
                bail!("This command is not yet supported on the NEW platform (shuttle.dev).");
            }
        } else if matches!(
            args.cmd,
            Command::Deployment(DeploymentCommand::Stop)
                | Command::Deployment(DeploymentCommand::Redeploy { .. })
                | Command::Account
                | Command::Project(ProjectCommand::Link)
                | Command::Project(ProjectCommand::Update(..))
        ) {
            bail!("This command is not supported on the OLD platform (shuttle.rs).");
        }

        if !matches!(
            args.cmd,
            // commands that don't differ in behavior in any way between .rs/.dev
            Command::Feedback | Command::Generate(_) | Command::Upgrade { .. }
        ) {
            if self.beta {
                eprintln!("{}", "INFO: Using NEW platform API (shuttle.dev)".green());
            } else {
                eprintln!("{}", "INFO: Using OLD platform API (shuttle.rs)".blue());
            }
        }
        if let Some(ref url) = args.api_url {
            if (!self.beta && url != API_URL_DEFAULT) || (self.beta && url != API_URL_DEFAULT_BETA)
            {
                eprintln!(
                    "{}",
                    format!("INFO: Targeting non-default API: {url}").yellow(),
                );
            }
            if url.ends_with('/') {
                eprintln!("WARNING: API URL is probably incorrect. Ends with '/': {url}");
            }
        }
        self.ctx.set_api_url(args.api_url);

        // All commands that call the API
        if matches!(
            args.cmd,
            Command::Init(..)
                | Command::Deploy(..)
                | Command::Status
                | Command::Logs { .. }
                | Command::Account
                | Command::Login(..)
                | Command::Logout(..)
                | Command::Deployment(..)
                | Command::Resource(..)
                | Command::Certificate(..)
                | Command::Stop
                | Command::Clean
                | Command::Project(..)
        ) || (
            // project linking on beta requires api client
            // TODO: refactor so that beta local run does not need to know project id / always uses crate name ???
            self.beta && matches!(args.cmd, Command::Run(..))
        ) {
            let client = ShuttleApiClient::new(
                self.ctx.api_url(self.beta),
                self.ctx.api_key().ok(),
                Some(
                    HeaderMap::try_from(&HashMap::from([(
                        X_CARGO_SHUTTLE_VERSION.clone(),
                        crate::VERSION.to_owned(),
                    )]))
                    .unwrap(),
                ),
                None,
            );
            self.client = Some(client);
            if !args.offline && !self.beta {
                self.check_api_versions().await?;
            }
        }

        // All commands that need to know which project is being handled
        if matches!(
            args.cmd,
            Command::Deploy(..)
                | Command::Deployment(..)
                | Command::Resource(..)
                | Command::Certificate(..)
                | Command::Project(
                    // ProjectCommand::List does not need to know which project we are in
                    ProjectCommand::Start { .. }
                        | ProjectCommand::Update(..)
                        | ProjectCommand::Status { .. }
                        | ProjectCommand::Stop { .. }
                        | ProjectCommand::Restart { .. }
                        | ProjectCommand::Delete { .. }
                        | ProjectCommand::Link
                )
                | Command::Stop
                | Command::Clean
                | Command::Status
                | Command::Logs { .. }
        ) {
            // Command::Run only uses load_local (below) instead of load_project since it does not target a project in the API
            self.load_project(
                &args.project_args,
                matches!(args.cmd, Command::Project(ProjectCommand::Link)),
                // only deploy should create a project if the provided name is not found in the project list.
                // (project start should always make the POST call, it's an upsert operation)
                matches!(args.cmd, Command::Deploy(..)),
            )
            .await?;
        }

        let res = match args.cmd {
            Command::Init(init_args) => {
                self.init(
                    init_args,
                    args.project_args,
                    provided_path_to_init,
                    args.offline,
                )
                .await
            }
            Command::Generate(cmd) => match cmd {
                GenerateCommand::Manpage => generate_manpage(),
                GenerateCommand::Shell { shell, output } => {
                    generate_completions(self.bin, shell, output)
                }
            },
            Command::Account => self.account().await,
            Command::Login(login_args) => self.login(login_args, args.offline).await,
            Command::Logout(logout_args) => self.logout(logout_args).await,
            Command::Feedback => open_gh_issue(),
            Command::Run(run_args) => {
                self.ctx.load_local(&args.project_args)?;
                if self.beta {
                    self.local_run_beta(run_args, args.debug).await
                } else {
                    self.local_run(run_args).await
                }
            }
            Command::Deploy(deploy_args) => self.deploy(deploy_args).await,
            Command::Status => self.status().await,
            Command::Logs(logs_args) => {
                if self.beta {
                    self.logs_beta(logs_args).await
                } else {
                    self.logs(logs_args).await
                }
            }
            Command::Deployment(cmd) => match cmd {
                DeploymentCommand::List { page, limit, table } => {
                    self.deployments_list(page, limit, table).await
                }
                DeploymentCommand::Status { id } => self.deployment_get(id).await,
                DeploymentCommand::Redeploy { id } => self.deployment_redeploy(id).await,
                DeploymentCommand::Stop => self.stop_beta().await,
            },
            Command::Stop => self.stop().await,
            Command::Clean => self.clean().await,
            Command::Resource(cmd) => match cmd {
                ResourceCommand::List {
                    table,
                    show_secrets,
                } => {
                    if self.beta {
                        self.resources_list_beta(table, show_secrets).await
                    } else {
                        self.resources_list(table, show_secrets).await
                    }
                }
                ResourceCommand::Delete {
                    resource_type,
                    confirmation: ConfirmationArgs { yes },
                } => self.resource_delete(&resource_type, yes).await,
                ResourceCommand::Dump { resource_type } => self.resource_dump(&resource_type).await,
            },
            Command::Certificate(cmd) => match cmd {
                CertificateCommand::Add { domain } => self.add_certificate(domain).await,
                CertificateCommand::List { table } => self.list_certificates(table).await,
                CertificateCommand::Delete {
                    domain,
                    confirmation: ConfirmationArgs { yes },
                } => self.delete_certificate(domain, yes).await,
            },
            Command::Project(cmd) => match cmd {
                ProjectCommand::Start(ProjectStartArgs { idle_minutes }) => {
                    if self.beta {
                        self.project_create_beta().await
                    } else {
                        self.project_start(idle_minutes).await
                    }
                }
                ProjectCommand::Update(cmd) => match cmd {
                    ProjectUpdateCommand::Name { name } => self.project_rename_beta(name).await,
                },
                ProjectCommand::Restart(ProjectStartArgs { idle_minutes }) => {
                    self.project_restart(idle_minutes).await
                }
                ProjectCommand::Status { follow } => {
                    if self.beta {
                        self.project_status_beta().await
                    } else {
                        self.project_status(follow).await
                    }
                }
                ProjectCommand::List { table, .. } => self.projects_list(table).await,
                ProjectCommand::Stop => self.project_stop().await,
                ProjectCommand::Delete(ConfirmationArgs { yes }) => {
                    if self.beta {
                        self.project_delete_beta(yes).await
                    } else {
                        self.project_delete(yes).await
                    }
                }
                ProjectCommand::Link => Ok(()), // logic is done in `load_local`
            },
            Command::Upgrade { preview } => update_cargo_shuttle(preview).await,
        };

        for w in self.version_warnings {
            println!("{w}");
        }

        res
    }

    async fn check_api_versions(&mut self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        debug!("Checking API versions");
        if let Ok(versions) = client.get_api_versions().await {
            debug!("Got API versions: {versions:?}");
            self.version_info = Some(versions);

            // check cargo-shuttle version
            // should always be a valid semver
            let my_version = &semver::Version::from_str(VERSION).unwrap();
            let latest_version = &self.version_info.as_ref().unwrap().cargo_shuttle;
            if my_version != latest_version {
                let newer_version_exists = my_version < latest_version;
                let string = if semvers_are_compatible(my_version, latest_version) {
                    newer_version_exists.then(|| {
                        format!("Info: A newer version of cargo-shuttle exists ({latest_version}).")
                    })
                    // Having a newer but compatible version does not show warning
                } else {
                    newer_version_exists.then(||
                        formatdoc! {"
                            Warning:
                                A newer version of cargo-shuttle exists ({latest_version}).
                                It is recommended to upgrade.
                                Refer to the upgrading docs: https://docs.shuttle.rs/configuration/shuttle-versions#upgrading-shuttle-version"
                        }
                    ).or_else(||
                        Some(formatdoc! {"
                            Warning:
                                Your version of cargo-shuttle ({my_version}) is newer than what the API expects ({latest_version}).
                                This means a new release is likely underway!
                                Unexpected behavior can occur until the API is updated."
                        })
                    )
                };
                if let Some(s) = string {
                    self.version_warnings.push(s.yellow().to_string());
                }
            }
        } else {
            debug!("Failed to get API version info");
        }

        Ok(())
    }

    /// Log in, initialize a project and potentially create the Shuttle environment for it.
    ///
    /// If project name, template, and path are passed as arguments, it will run without any extra
    /// interaction.
    async fn init(
        &mut self,
        args: InitArgs,
        mut project_args: ProjectArgs,
        provided_path_to_init: bool,
        offline: bool,
    ) -> Result<()> {
        // Turns the template or git args (if present) to a repo+folder.
        let git_template = args.git_template()?;
        let no_git = args.no_git;

        let needs_name = project_args.name_or_id.is_none();
        let needs_template = git_template.is_none();
        let needs_path = !provided_path_to_init;
        let needs_login = self.ctx.api_key().is_err() && args.login_args.api_key.is_none();
        let interactive = needs_name || needs_template || needs_path || needs_login;

        let theme = ColorfulTheme::default();

        // 1. Log in (if not logged in yet)
        if needs_login {
            println!("First, let's log in to your Shuttle account.");
            self.login(args.login_args.clone(), offline).await?;
            println!();
        } else if args.login_args.api_key.is_some() {
            self.login(args.login_args.clone(), offline).await?;
        }

        // 2. Ask for project name or validate the given one
        if needs_name && !self.beta {
            printdoc! {"
                What do you want to name your project?
                It will be hosted at ${{project_name}}.shuttleapp.rs, so choose something unique!
                "
            };
        }
        let mut prev_name: Option<String> = None;
        loop {
            // prompt if interactive
            let name: String = if let Some(name) = project_args.name_or_id.clone() {
                name
            } else {
                // not using `validate_with` due to being blocking.
                Input::with_theme(&theme)
                    .with_prompt("Project name")
                    .interact()?
            };
            let force_name = args.force_name
                || (needs_name && prev_name.as_ref().is_some_and(|prev| prev == &name));
            if force_name {
                project_args.name_or_id = Some(name);
                break;
            }
            // validate and take action based on result
            if self
                .check_project_name(&mut project_args, name.clone())
                .await
            {
                // success
                break;
            } else if needs_name {
                // try again
                println!(r#"Type the same name again to use "{}" anyways."#, name);
                prev_name = Some(name);
            } else {
                // don't continue if non-interactive
                bail!(
                    "Invalid or unavailable project name. Use `--force-name` to use this project name anyways."
                );
            }
        }
        if needs_name {
            println!();
        }

        // 3. Confirm the project directory
        let path = if needs_path {
            let path = args.path.join(
                project_args
                    .name_or_id
                    .as_ref()
                    .expect("name should be set"),
            );

            loop {
                println!("Where should we create this project?");

                let directory_str: String = Input::with_theme(&theme)
                    .with_prompt("Directory")
                    .default(format!("{}", path.display()))
                    .interact()?;
                println!();

                let path = args::create_and_parse_path(OsString::from(directory_str))?;

                if std::fs::read_dir(&path)
                    .expect("init dir to exist and list entries")
                    .count()
                    > 0
                    && !Confirm::with_theme(&theme)
                        .with_prompt("Target directory is not empty. Are you sure?")
                        .default(true)
                        .interact()?
                {
                    println!();
                    continue;
                }

                break path;
            }
        } else {
            args.path.clone()
        };

        // 4. Ask for the template
        let template = match git_template {
            Some(git_template) => git_template,
            None => {
                // Try to present choices from our up-to-date examples.
                // Fall back to the internal (potentially outdated) list.
                let schema = if offline {
                    None
                } else {
                    get_templates_schema()
                        .await
                        .map_err(|e| {
                            error!(err = %e, "Failed to get templates");
                            println!(
                                "{}",
                                "Failed to look up template list. Falling back to internal list."
                                    .yellow()
                            )
                        })
                        .ok()
                        .and_then(|s| {
                            if s.version == TEMPLATES_SCHEMA_VERSION {
                                return Some(s);
                            }
                            println!(
                                "{}",
                                "Template list with incompatible version found. Consider updating cargo-shuttle. Falling back to internal list."
                                    .yellow()
                            );

                            None
                        })
                };
                if let Some(schema) = schema {
                    println!("What type of project template would you like to start from?");
                    let i = Select::with_theme(&theme)
                        .items(&[
                            "A Hello World app in a supported framework",
                            "Browse our full library of templates", // TODO(when templates page is live): Add link to it?
                        ])
                        .clear(false)
                        .default(0)
                        .interact()?;
                    println!();
                    if i == 0 {
                        // Use a Hello world starter
                        let mut starters = schema.starters.into_values().collect::<Vec<_>>();
                        starters.sort_by_key(|t| {
                            // Make the "No templates" appear last in the list
                            if t.title.starts_with("No") {
                                "zzz".to_owned()
                            } else {
                                t.title.clone()
                            }
                        });
                        let starter_strings = starters
                            .iter()
                            .map(|t| {
                                format!("{} - {}", t.title.clone().bold(), t.description.clone())
                            })
                            .collect::<Vec<_>>();
                        let index = Select::with_theme(&theme)
                            .with_prompt("Select template")
                            .items(&starter_strings)
                            .default(0)
                            .interact()?;
                        println!();
                        let path = starters[index]
                            .path
                            .clone()
                            .expect("starter to have a path");

                        TemplateLocation {
                            auto_path: EXAMPLES_REPO.into(),
                            subfolder: Some(path),
                        }
                    } else {
                        // Browse all non-starter templates
                        let mut templates = schema.templates.into_values().collect::<Vec<_>>();
                        templates.sort_by_key(|t| t.title.clone());
                        let template_strings = templates
                            .iter()
                            .map(|t| {
                                format!(
                                    "{} - {}{}",
                                    t.title.clone().bold(),
                                    t.description.clone(),
                                    t.tags
                                        .first()
                                        .map(|tag| format!(" ({tag})").dim().to_string())
                                        .unwrap_or_default(),
                                )
                            })
                            .collect::<Vec<_>>();
                        let index = Select::with_theme(&theme)
                            .with_prompt("Select template")
                            .items(&template_strings)
                            .default(0)
                            .interact()?;
                        println!();
                        let path = templates[index]
                            .path
                            .clone()
                            .expect("template to have a path");

                        TemplateLocation {
                            auto_path: EXAMPLES_REPO.into(),
                            subfolder: Some(path),
                        }
                    }
                } else {
                    println!("Shuttle works with many frameworks. Which one do you want to use?");
                    let frameworks = args::InitTemplateArg::VARIANTS;
                    let framework_strings = frameworks
                        .iter()
                        .map(|t| {
                            t.get_documentation()
                                .expect("all template variants to have docs")
                        })
                        .collect::<Vec<_>>();
                    let index = Select::with_theme(&theme)
                        .items(&framework_strings)
                        .default(0)
                        .interact()?;
                    println!();
                    frameworks[index].template()
                }
            }
        };

        // 5. Initialize locally
        crate::init::generate_project(
            path.clone(),
            project_args
                .name_or_id
                .as_ref()
                .expect("to have a project name provided"),
            &template,
            no_git,
        )?;
        println!();

        // 6. Confirm that the user wants to create the project environment on Shuttle
        let should_create_environment = if !interactive {
            args.create_env
        } else if args.create_env {
            true
        } else {
            let name = project_args
                .name_or_id
                .as_ref()
                .expect("to have a project name provided");

            let should_create = Confirm::with_theme(&theme)
                .with_prompt(if self.beta {
                    format!(r#"Create a project on Shuttle with the name "{name}"?"#)
                } else {
                    format!(
                        r#"Claim the project name "{name}" by starting a project container on Shuttle?"#
                    )
                })
                .default(true)
                .interact()?;
            if !should_create && !self.beta {
                println!(
                    "Note: The project name will not be claimed until \
                    you start the project with `cargo shuttle project start`."
                )
            }
            println!();
            should_create
        };

        if should_create_environment {
            // Set the project working directory path to the init path,
            // so `load_project` is ran with the correct project path
            project_args.working_directory.clone_from(&path);

            self.load_project(&project_args, true, true).await?;
            if !self.beta {
                self.project_start(DEFAULT_IDLE_MINUTES).await?;
            }
        }

        if std::env::current_dir().is_ok_and(|d| d != path) {
            println!("You can `cd` to the directory, then:");
        }
        if self.beta {
            println!("Run `shuttle run` to run the app locally.");
        } else {
            println!("Run `cargo shuttle run` to run the app locally.");
        }
        if !should_create_environment {
            if self.beta {
                println!("Run `shuttle deploy` to deploy it to Shuttle.");
            } else {
                println!(
                    "Run `cargo shuttle project start` to create a project environment on Shuttle."
                );
                let serenity_idle_hint = template
                    .subfolder
                    .as_ref()
                    .is_some_and(|s| s.contains("serenity") || s.contains("poise"));
                if serenity_idle_hint {
                    printdoc!(
                        "
                        Hint: Discord bots might want to use `--idle-minutes 0` when starting the
                        project so that they don't go offline: {SHUTTLE_IDLE_DOCS_URL}
                        "
                    );
                }
            }
        }

        Ok(())
    }

    /// true -> success/neutral. false -> try again.
    async fn check_project_name(&self, project_args: &mut ProjectArgs, name: String) -> bool {
        let client = self.client.as_ref().unwrap();
        match if self.beta {
            client.check_project_name_beta(&name).await
        } else {
            client.check_project_name(&name).await
        } {
            Ok(true) => {
                // inner value is inverted on beta
                if self.beta {
                    project_args.name_or_id = Some(name);
                    return true;
                }

                println!("{} {}", "Project name already taken:".red(), name);
                println!("{}", "Try a different name.".yellow());

                false
            }
            // not possible on beta
            Ok(false) => {
                project_args.name_or_id = Some(name);

                true
            }
            Err(e) => {
                // If API error contains message regarding format of error name, print that error and prompt again
                if let Ok(api_error) = e.downcast::<ApiError>() {
                    // If the returned error string changes, this could break
                    if api_error.message.contains("Invalid project name") {
                        println!("{}", api_error.message.yellow());
                        println!("{}", "Try a different name.".yellow());
                        return false;
                    }
                }
                // Else, the API error was about something else.
                // Ignore and keep going to not prevent the flow of the init command.
                project_args.name_or_id = Some(name);
                println!(
                    "{}",
                    "Failed to check if project name is available.".yellow()
                );

                true
            }
        }
    }

    pub async fn load_project(
        &mut self,
        project_args: &ProjectArgs,
        do_linking: bool,
        create_missing_beta_project: bool,
    ) -> Result<()> {
        trace!("project arguments: {project_args:?}");

        self.ctx.load_local(project_args)?;
        if self.beta {
            // load project id from file if exists
            self.ctx.load_local_internal(project_args)?;
            if let Some(name) = project_args.name_or_id.as_ref() {
                // uppercase project id
                if let Some(suffix) = name.strip_prefix("proj_") {
                    // Soft (dumb) validation of ULID format in the id (ULIDs are 26 chars)
                    if suffix.len() == 26 {
                        let proj_id_uppercase = format!("proj_{}", suffix.to_ascii_uppercase());
                        if *name != proj_id_uppercase {
                            eprintln!("INFO: Converted project id to '{}'", proj_id_uppercase);
                            self.ctx.set_project_id(proj_id_uppercase);
                        }
                    }
                }
                // translate project name to project id if a name was given
                if !name.starts_with("proj_") {
                    trace!("unprefixed project id found, assuming it's a project name");
                    let client = self.client.as_ref().unwrap();
                    trace!(%name, "looking up project id from project name");
                    if let Some(proj) = client
                        .get_projects_list_beta()
                        .await?
                        .projects
                        .into_iter()
                        .find(|p| p.name == *name)
                    {
                        trace!("found project by name");
                        self.ctx.set_project_id(proj.id);
                    } else {
                        trace!("did not find project by name");
                        if create_missing_beta_project {
                            trace!("creating project since it was not found");
                            let proj = client.create_project_beta(name).await?;
                            eprintln!("Created project '{}' with id {}", proj.name, proj.id);
                            self.ctx.set_project_id(proj.id);
                        }
                    }
                }
                // if called from Link command, command-line override is saved to file
                if do_linking {
                    eprintln!("Linking to project {}", self.ctx.project_id());
                    self.ctx.save_local_internal()?;
                    return Ok(());
                }
            }
            // if project id is still not known or an explicit linking is wanted, start the linking prompt
            if !self.ctx.project_id_found() || do_linking {
                self.project_link(None).await?;
            }
        }

        Ok(())
    }

    async fn project_link(&mut self, id_or_name: Option<String>) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let projs = client.get_projects_list_beta().await?.projects;

        let theme = ColorfulTheme::default();

        let proj = if let Some(id_or_name) = id_or_name {
            projs
                .into_iter()
                .find(|p| p.id == id_or_name || p.name == id_or_name)
                .ok_or(anyhow!("Did not find project '{id_or_name}'."))?
        } else {
            let selected_project = if projs.is_empty() {
                eprintln!("Create a new project to link to this directory:");

                None
            } else {
                eprintln!("Which project do you want to link this directory to?");

                let mut items = projs.iter().map(|p| p.name.clone()).collect::<Vec<_>>();
                items.extend_from_slice(&["[CREATE NEW]".to_string()]);
                let index = Select::with_theme(&theme)
                    .items(&items)
                    .default(0)
                    .interact()?;

                if index == projs.len() {
                    // last item selected (create new)
                    None
                } else {
                    Some(projs[index].clone())
                }
            };

            match selected_project {
                Some(proj) => proj,
                None => {
                    let name: String = Input::with_theme(&theme)
                        .with_prompt("Project name")
                        .interact()?;

                    let proj = client.create_project_beta(&name).await?;
                    eprintln!("Created project '{}' with id {}", proj.name, proj.id);

                    proj
                }
            }
        };

        eprintln!("Linking to project '{}' with id {}", proj.name, proj.id);
        self.ctx.set_project_id(proj.id);
        self.ctx.save_local_internal()?;

        Ok(())
    }

    async fn account(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let user = client.get_current_user_beta().await?;
        print!("{}", user.to_string_colored());

        Ok(())
    }

    /// Log in with the given API key or after prompting the user for one.
    async fn login(&mut self, login_args: LoginArgs, offline: bool) -> Result<()> {
        let api_key = match login_args.api_key {
            Some(api_key) => api_key,
            None => {
                if login_args.prompt || !self.beta {
                    // manual input requested (always the case on shuttle.rs)

                    if !login_args.prompt && !self.beta {
                        // if !beta, open console
                        let url = SHUTTLE_LEGACY_NEW_PROJECT;
                        let _ = webbrowser::open(url);
                        println!("If your browser did not automatically open, go to {url}");
                    }

                    Password::with_theme(&ColorfulTheme::default())
                        .with_prompt("API key")
                        .validate_with(|input: &String| {
                            if input.is_empty() {
                                return Err("Empty API key was provided");
                            }
                            Ok(())
                        })
                        .interact()?
                } else {
                    // device auth flow via Shuttle Console
                    self.device_auth(login_args.console_url).await?
                }
            }
        };

        self.ctx.set_api_key(api_key.clone())?;

        if let Some(client) = self.client.as_mut() {
            client.api_key = Some(api_key);

            if self.beta {
                if offline {
                    eprintln!("INFO: Skipping API key verification");
                } else {
                    let u = client
                        .get_current_user_beta()
                        .await
                        .context("failed to check API key validity")?;
                    println!("Logged in as {} ({})", u.name.bold(), u.id.bold());
                }
            }
        }

        Ok(())
    }

    async fn device_auth(&self, console_url: String) -> Result<String> {
        let client = self.client.as_ref().unwrap();

        // should not have trailing slash
        if console_url.ends_with('/') {
            eprintln!("WARNING: Console URL is probably incorrect. Ends with '/': {console_url}");
        }

        let (mut tx, mut rx) = client.get_device_auth_ws().await?.split();

        // keep the socket alive with ping/pong
        let pinger = tokio::spawn(async move {
            loop {
                if let Err(e) = tx.send(Message::Ping(Vec::new())).await {
                    error!(error = %e, "Error when pinging websocket");
                    break;
                };
                sleep(Duration::from_secs(20)).await;
            }
        });

        let token = read_ws_until_text(&mut rx).await?;
        let Some(token) = token else {
            bail!("Did not receive device auth token over websocket");
        };
        let token = serde_json::from_str::<TokenMessage>(&token)?.token;

        let url = &format!("{}/device-auth?token={}", console_url, token);
        let _ = webbrowser::open(url);
        println!("Complete login in Shuttle Console to authenticate CLI.");
        println!("If your browser did not automatically open, go to {url}");
        println!();
        println!("{}", format!("Token: {token}").bold());
        println!();

        let key = read_ws_until_text(&mut rx).await?;
        let Some(key) = key else {
            bail!("Failed to receive API key over websocket");
        };
        let key = serde_json::from_str::<KeyMessage>(&key)?.api_key;

        pinger.abort();

        Ok(key)
    }

    async fn logout(&mut self, logout_args: LogoutArgs) -> Result<()> {
        if logout_args.reset_api_key {
            self.reset_api_key()
                .await
                .map_err(suggestions::api_key::reset_api_key_failed)?;
            println!("Successfully reset the API key.");
            if self.beta {
                println!(" -> Use `shuttle login` to get a new one.")
            } else {
                println!(" -> Go to {SHUTTLE_LEGACY_NEW_PROJECT} to get a new one.");
            }
            println!();
        }
        self.ctx.clear_api_key()?;
        println!("Successfully logged out.");

        Ok(())
    }

    async fn reset_api_key(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        client.reset_api_key().await.and_then(|res| {
            if res.status().is_success() {
                Ok(())
            } else {
                Err(anyhow!("Resetting API key failed."))
            }
        })
    }

    async fn stop_beta(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let pid = self.ctx.project_id();
        let res = client.stop_service_beta(pid).await?;
        println!("{res}");
        wait_with_spinner(2000, |_, pb| async move {
            let deployment = client.get_current_deployment_beta(pid).await?;

            let get_cleanup = |d: Option<DeploymentResponseBeta>| {
                move || {
                    if let Some(d) = d {
                        println!("{}", d.to_string_colored());
                    }
                }
            };
            let Some(deployment) = deployment else {
                return Ok(Some(get_cleanup(None)));
            };

            let state = deployment.state.clone();
            pb.set_message(deployment.to_string_summary_colored());
            let cleanup = get_cleanup(Some(deployment));
            match state {
                    DeploymentStateBeta::Pending
                    | DeploymentStateBeta::Stopping
                    | DeploymentStateBeta::InProgress
                    | DeploymentStateBeta::Running => Ok(None),
                    DeploymentStateBeta::Building // a building deployment should take it back to InProgress then Running, so don't follow that sequence
                    | DeploymentStateBeta::Failed
                    | DeploymentStateBeta::Stopped
                    | DeploymentStateBeta::Unknown => Ok(Some(cleanup)),
                }
        })
        .await?;

        Ok(())
    }

    async fn stop(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let p = self.ctx.project_name();
        wait_with_spinner(500, |i, pb| async move {
            let service = if i == 0 {
                client.stop_service(p).await?
            } else {
                client.get_service(p).await?
            };

            let service_str = format!("{service}");
            let cleanup = move || {
                println!("{}", "Successfully stopped service".bold());
                println!("{service_str}");
                println!("Run `cargo shuttle deploy` to re-deploy your service.");
            };

            let Some(ref deployment) = service.deployment else {
                return Ok(Some(cleanup));
            };

            pb.set_message(format!("Stopping {}", deployment.id));

            if deployment.state == shuttle_common::deployment::State::Stopped {
                Ok(Some(cleanup))
            } else {
                Ok(None)
            }
        })
        .await
        .map_err(suggestions::deployment::stop_deployment_failure)?;

        Ok(())
    }

    async fn status(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let summary = client.get_service(self.ctx.project_name()).await?;

        println!("{summary}");

        Ok(())
    }

    async fn clean(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let message = client
            .clean_project(self.ctx.project_name())
            .await
            .map_err(|err| {
                suggestions::project::project_request_failure(
                    err,
                    "Project clean failed",
                    true,
                    "cleaning your project or checking its status fail repeatedly",
                )
            })?;
        println!("{message}");

        Ok(())
    }

    async fn logs_beta(&self, args: LogsArgs) -> Result<()> {
        if args.follow {
            eprintln!("Streamed logs are not yet supported on the new platform.");
            return Ok(());
        }
        // TODO: implement logs range
        let client = self.client.as_ref().unwrap();
        let pid = self.ctx.project_id();
        let logs = if args.all_deployments {
            client.get_project_logs_beta(pid).await?.logs
        } else {
            let id = if args.latest {
                // Find latest deployment (not always an active one)
                let deployments = client.get_deployments_beta(pid, 1, 1).await?.deployments;
                let Some(most_recent) = deployments.first() else {
                    println!("No deployments found");
                    return Ok(());
                };
                eprintln!("Getting logs from: {}", most_recent.id);
                most_recent.id.to_string()
            } else if let Some(id) = args.id {
                id
            } else {
                let Some(current) = client.get_current_deployment_beta(pid).await? else {
                    println!("No deployments found");
                    return Ok(());
                };
                eprintln!("Getting logs from: {}", current.id);
                current.id
            };
            client.get_deployment_logs_beta(pid, &id).await?.logs
        };
        for log in logs {
            if args.raw {
                println!("{}", log.line);
            } else {
                println!("{log}");
            }
        }

        Ok(())
    }

    async fn logs(&self, args: LogsArgs) -> Result<()> {
        let range = match (args.head, args.tail, args.all) {
            (Some(num), _, _) => LogsRange::Head(num),
            (_, Some(num), _) => LogsRange::Tail(num),
            (_, _, true) => LogsRange::All,
            _ => LogsRange::Tail(1000),
        };
        let client = self.client.as_ref().unwrap();
        let id = if let Some(id) = args.id {
            id
        } else {
            let proj_name = self.ctx.project_name();

            if args.latest {
                // Find latest deployment (not always an active one)
                let deployments = client
                    .get_deployments(proj_name, 0, 1)
                    .await
                    .map_err(|err| {
                        suggestions::logs::get_logs_failure(
                            err,
                            "Fetching the latest deployment failed",
                        )
                    })?;
                let most_recent = deployments.first().context(format!(
                    "Could not find any deployments for '{proj_name}'. Try passing a deployment ID manually",
                ))?;

                most_recent.id.to_string()
            } else if let Some(deployment) = client.get_service(proj_name).await?.deployment {
                // Active deployment
                deployment.id.to_string()
            } else {
                bail!(
                    "Could not find a running deployment for '{proj_name}'. \
                    Try with '--latest', or pass a deployment ID manually"
                );
            }
        };

        if args.follow {
            let mut stream = client
                .get_logs_ws(self.ctx.project_name(), &id, range)
                .await
                .map_err(|err| {
                    suggestions::logs::get_logs_failure(err, "Connecting to the logs stream failed")
                })?;

            while let Ok(Some(s)) = read_ws_until_text(&mut stream).await {
                match serde_json::from_str::<LogItem>(&s) {
                    Ok(log) => {
                        if args.raw {
                            println!("{}", log.get_raw_line())
                        } else {
                            println!("{log}")
                        }
                    }
                    Err(err) => {
                        debug!(error = %err, "failed to parse message into log item");

                        let message = if let Ok(err) = serde_json::from_str::<ApiError>(&s) {
                            err.to_string()
                        } else {
                            "failed to parse logs, is your cargo-shuttle outdated?".to_string()
                        };

                        bail!(message);
                    }
                }
            }
        } else {
            let logs = client
                .get_logs(self.ctx.project_name(), &id, range)
                .await
                .map_err(|err| {
                    suggestions::logs::get_logs_failure(err, "Fetching the deployment failed")
                })?;

            for log in logs.into_iter() {
                if args.raw {
                    println!("{}", log.get_raw_line())
                } else {
                    println!("{log}")
                }
            }
        }

        Ok(())
    }

    async fn deployments_list(&self, page: u32, limit: u32, table_args: TableArgs) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        if limit == 0 {
            println!();
            return Ok(());
        }
        let limit = limit + 1;

        let proj_name = self.ctx.project_name();

        if self.beta {
            let mut deployments = client
                .get_deployments_beta(self.ctx.project_id(), page as i32, limit as i32)
                .await?
                .deployments;
            let page_hint = if deployments.len() == limit as usize {
                deployments.pop();
                true
            } else {
                false
            };
            let table = deployments_table_beta(&deployments, table_args.raw);

            println!(
                "{}",
                format!("Deployments in project '{}'", proj_name).bold()
            );
            println!("{table}");
            if page_hint {
                println!("View the next page using `--page {}`", page + 1);
            }
        } else {
            let mut deployments = client
                .get_deployments(proj_name, page, limit)
                .await
                .map_err(suggestions::deployment::get_deployments_list_failure)?;
            let page_hint = if deployments.len() == limit as usize {
                deployments.pop();
                true
            } else {
                false
            };
            let table =
                get_deployments_table(&deployments, proj_name, page, table_args.raw, page_hint);
            println!("{table}");

            if deployments.is_empty() {
                println!("Run `cargo shuttle deploy` to deploy your project.");
            } else {
                println!("Run `cargo shuttle logs <id>` to get logs for a given deployment.");
            }
        };

        Ok(())
    }

    async fn deployment_get(&self, deployment_id: Option<String>) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        if self.beta {
            let pid = self.ctx.project_id();
            let deployment = match deployment_id {
                Some(id) => client.get_deployment_beta(pid, &id).await,
                None => {
                    let d = client.get_current_deployment_beta(pid).await?;
                    let Some(d) = d else {
                        println!("No deployment found");
                        return Ok(());
                    };
                    Ok(d)
                }
            }?;
            println!("{}", deployment.to_string_colored());
        } else {
            let deployment_id = deployment_id.expect("deployment id required on alpha platform");
            let deployment = client
                .get_deployment_details(
                    self.ctx.project_name(),
                    &Uuid::from_str(&deployment_id).map_err(|err| {
                        anyhow!("Provided deployment id is not a valid UUID: {err}")
                    })?,
                )
                .await
                .map_err(suggestions::deployment::get_deployment_status_failure)?;

            println!("{deployment}");
        }

        Ok(())
    }

    async fn deployment_redeploy(&self, deployment_id: String) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        let pid = self.ctx.project_id();
        let deployment = client.redeploy_beta(pid, &deployment_id).await?;

        // TODO?: Make it print logs on fail
        self.track_deployment_status_beta(pid, &deployment.id)
            .await?;

        Ok(())
    }

    async fn resources_list(&self, table_args: TableArgs, show_secrets: bool) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let resources = client
            .get_service_resources(self.ctx.project_name())
            .await
            .map_err(suggestions::resources::get_service_resources_failure)?;
        let table = get_resource_tables(
            &resources,
            self.ctx.project_name(),
            table_args.raw,
            show_secrets,
        );

        println!("{table}");

        Ok(())
    }

    async fn resources_list_beta(&self, table_args: TableArgs, show_secrets: bool) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let pid = self.ctx.project_id();
        let resources = client.get_service_resources_beta(pid).await?.resources;
        let table =
            get_resource_tables_beta(resources.as_slice(), pid, table_args.raw, show_secrets);

        println!("{table}");

        Ok(())
    }

    async fn resource_delete(
        &self,
        resource_type: &resource::Type,
        no_confirm: bool,
    ) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        if !no_confirm {
            println!(
                "{}",
                formatdoc!(
                    "
                WARNING:
                    Are you sure you want to delete this project's {}?
                    This action is permanent.",
                    resource_type
                )
                .bold()
                .red()
            );
            if !Confirm::with_theme(&ColorfulTheme::default())
                .with_prompt("Are you sure?")
                .default(false)
                .interact()
                .unwrap()
            {
                return Ok(());
            }
        }

        if self.beta {
            let msg = client
                .delete_service_resource_beta(self.ctx.project_id(), resource_type)
                .await?;
            println!("{msg}");
        } else {
            client
                .delete_service_resource(self.ctx.project_name(), resource_type)
                .await?;
            println!("Deleted resource {resource_type}");
        }

        println!(
            "{}",
            formatdoc! {"
                Note:
                    Remember to remove the resource annotation from your #[shuttle_runtime::main] function.
                    Otherwise, it will be provisioned again during the next deployment."
            }
            .yellow(),
        );

        Ok(())
    }

    async fn resource_dump(&self, resource_type: &resource::Type) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        let bytes = client
            .dump_service_resource(self.ctx.project_name(), resource_type)
            .await?;

        std::io::stdout().write_all(&bytes).unwrap();

        Ok(())
    }

    async fn list_certificates(&self, table_args: TableArgs) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let certs = client
            .list_certificates_beta(self.ctx.project_id())
            .await?
            .certificates;

        let table = get_certificates_table_beta(certs.as_ref(), table_args.raw);
        println!("{}", table);

        Ok(())
    }
    async fn add_certificate(&self, domain: String) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let cert = client
            .add_certificate_beta(self.ctx.project_id(), domain.clone())
            .await?;

        println!("Added certificate for {}", cert.subject);

        Ok(())
    }
    async fn delete_certificate(&self, domain: String, no_confirm: bool) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        if !no_confirm {
            println!(
                "{}",
                formatdoc!(
                    "
                WARNING:
                    Delete the certificate for {}?",
                    domain
                )
                .bold()
                .red()
            );
            if !Confirm::with_theme(&ColorfulTheme::default())
                .with_prompt("Are you sure?")
                .default(false)
                .interact()
                .unwrap()
            {
                return Ok(());
            }
        }

        let msg = client
            .delete_certificate_beta(self.ctx.project_id(), domain.clone())
            .await?;
        println!("{msg}");

        Ok(())
    }

    fn get_secrets(run_args: &RunArgs, service: &BuiltService) -> Result<HashMap<String, String>> {
        let secrets_file = run_args.secret_args.secrets.clone().or_else(|| {
            let crate_dir = service.crate_directory();
            // Prioritise crate-local prod secrets over workspace dev secrets (in the rare case that both exist)
            [
                crate_dir.join("Secrets.dev.toml"),
                crate_dir.join("Secrets.toml"),
                service.workspace_path.join("Secrets.dev.toml"),
                service.workspace_path.join("Secrets.toml"),
            ]
            .into_iter()
            .find(|f| f.exists() && f.is_file())
        });
        let secrets = if let Some(secrets_file) = secrets_file {
            trace!("Loading secrets from {}", secrets_file.display());
            if let Ok(secrets_str) = read_to_string(secrets_file) {
                let secrets = toml::from_str::<HashMap<String, String>>(&secrets_str)?;
                trace!(keys = ?secrets.keys(), "available secrets");
                secrets
            } else {
                trace!("No secrets were loaded");
                Default::default()
            }
        } else {
            trace!("No secrets file was found");
            Default::default()
        };

        Ok(secrets)
    }

    fn get_secrets_beta(
        args: &SecretsArgs,
        workspace_root: &Path,
    ) -> Result<Option<HashMap<String, String>>> {
        // Look for a secrets file, first in the command args, then in the root of the workspace.
        let secrets_file = args.secrets.clone().or_else(|| {
            let secrets_file = workspace_root.join("Secrets.toml");

            if secrets_file.exists() && secrets_file.is_file() {
                Some(secrets_file)
            } else {
                None
            }
        });

        Ok(if let Some(secrets_file) = secrets_file {
            trace!("Loading secrets from {}", secrets_file.display());
            if let Ok(secrets_str) = read_to_string(&secrets_file) {
                let secrets = toml::from_str::<HashMap<String, String>>(&secrets_str)?;

                trace!(keys = ?secrets.keys(), "available secrets");

                Some(secrets)
            } else {
                trace!("No secrets were loaded");
                None
            }
        } else {
            trace!("No secrets file was found");
            None
        })
    }

    async fn spin_local_runtime(
        run_args: &RunArgs,
        service: &BuiltService,
        idx: u16,
    ) -> Result<Option<(Child, runtime::Client)>> {
        let secrets = Shuttle::get_secrets(run_args, service)?;

        trace!(path = ?service.executable_path, "runtime executable");

        if let Some(warning) = check_and_warn_runtime_version(&service.executable_path).await? {
            eprint!("{}", warning);
        }

        let runtime_executable = service.executable_path.clone();
        let port =
            portpicker::pick_unused_port().expect("unable to find available port for gRPC server");
        // Child process and gRPC client for sending requests to it
        let (mut runtime, mut runtime_client) =
            runner::start(port, runtime_executable, service.workspace_path.as_path()).await?;

        let service_name = service.service_name()?;
        let deployment_id: Uuid = Default::default();

        let child_stdout = runtime
            .stdout
            .take()
            .context("child process did not have a handle to stdout")?;
        let mut reader = BufReader::new(child_stdout).lines();
        let service_name_clone = service_name.clone();
        let raw = run_args.raw;
        tokio::spawn(async move {
            while let Some(line) = reader.next_line().await.unwrap() {
                let log_item = LogItem::new(
                    deployment_id,
                    shuttle_common::log::Backend::Runtime(service_name_clone.clone()),
                    line,
                );

                if raw {
                    println!("{}", log_item.get_raw_line())
                } else {
                    println!("{log_item}")
                }
            }
        });

        //
        // LOADING PHASE
        //

        let load_request = tonic::Request::new(LoadRequest {
            project_name: service_name.to_string(),
            env: Environment::Local.to_string(),
            secrets: secrets.clone(),
            path: service
                .executable_path
                .clone()
                .into_os_string()
                .into_string()
                .expect("to convert path to string"),
            ..Default::default()
        });

        trace!("loading service");
        let response = runtime_client
            .load(load_request)
            .or_else(|err| async {
                runtime.kill().await?;
                Err(err)
            })
            .await?
            .into_inner();

        if !response.success {
            error!(error = response.message, "failed to load your service");
            return Ok(None);
        }

        //
        // PROVISIONING PHASE
        //

        let resources = response.resources;
        let (resources, mocked_responses) =
            Shuttle::local_provision_phase(service_name.as_str(), resources, secrets).await?;

        println!(
            "{}",
            get_resource_tables(&mocked_responses, service_name.as_str(), false, false,)
        );

        //
        // START PHASE
        //

        let addr = SocketAddr::new(
            if run_args.external {
                Ipv4Addr::UNSPECIFIED // 0.0.0.0
            } else {
                Ipv4Addr::LOCALHOST // 127.0.0.1
            }
            .into(),
            run_args.port + idx,
        );

        println!(
            "    {} {} on http://{}\n",
            "Starting".bold().green(),
            service_name,
            addr
        );

        let start_request = StartRequest {
            ip: addr.to_string(),
            resources,
        };

        trace!(?start_request, "starting service");
        let response = runtime_client
            .start(tonic::Request::new(start_request))
            .or_else(|err| async {
                runtime.kill().await?;
                Err(err)
            })
            .await?
            .into_inner();

        trace!(response = ?response,  "client response: ");
        Ok(Some((runtime, runtime_client)))
    }

    async fn local_provision_phase(
        project_name: &str,
        mut resources: Vec<Vec<u8>>,
        secrets: HashMap<String, String>,
    ) -> Result<(Vec<Vec<u8>>, Vec<resource::Response>)> {
        // for displaying the tables
        let mut mocked_responses: Vec<resource::Response> = Vec::new();
        let prov = LocalProvisioner::new()?;

        // Fail early if any bytes is invalid json
        let values = resources
            .iter()
            .map(|bytes| {
                serde_json::from_slice::<ResourceInput>(bytes)
                    .context("deserializing resource input")
            })
            .collect::<anyhow::Result<Vec<_>>>()?;

        for (bytes, shuttle_resource) in
            resources
                .iter_mut()
                .zip(values)
                // ignore non-Shuttle resource items
                .filter_map(|(bytes, value)| match value {
                    ResourceInput::Shuttle(shuttle_resource) => Some((bytes, shuttle_resource)),
                    ResourceInput::Custom(_) => None,
                })
                .map(|(bytes, shuttle_resource)| {
                    if shuttle_resource.version == RESOURCE_SCHEMA_VERSION {
                        Ok((bytes, shuttle_resource))
                    } else {
                        Err(anyhow!("
                            Shuttle resource request for {} with incompatible version found. Expected {}, found {}. \
                            Make sure that this deployer and the Shuttle resource are up to date.
                            ",
                            shuttle_resource.r#type,
                            RESOURCE_SCHEMA_VERSION,
                            shuttle_resource.version
                        ))
                    }
                }).collect::<anyhow::Result<Vec<_>>>()?.into_iter()
        {
            match shuttle_resource.r#type {
                resource::Type::Database(db_type) => {
                    let config: DbInput = serde_json::from_value(shuttle_resource.config)
                        .context("deserializing resource config")?;
                    let res = match config.local_uri {
                        Some(local_uri) => DatabaseResource::ConnectionString(local_uri),
                        None => DatabaseResource::Info(
                            prov.provision_database(Request::new(DatabaseRequest {
                                project_name: project_name.to_string(),
                                db_type: Some(db_type.into()),
                                db_name: config.db_name,
                            }))
                            .await
                            .context("Failed to start database container. Make sure that a Docker engine is running.")?
                            .into_inner()
                            .into(),
                        ),
                    };
                    mocked_responses.push(resource::Response {
                        r#type: shuttle_resource.r#type,
                        config: serde_json::Value::Null,
                        data: serde_json::to_value(&res).unwrap(),
                    });
                    *bytes = serde_json::to_vec(&ShuttleResourceOutput {
                        output: res,
                        custom: shuttle_resource.custom,
                    })
                    .unwrap();
                }
                resource::Type::Secrets => {
                    // We already know the secrets at this stage, they are not provisioned like other resources
                    mocked_responses.push(resource::Response {
                        r#type: shuttle_resource.r#type,
                        config: serde_json::Value::Null,
                        data: serde_json::to_value(secrets.clone()).unwrap(),
                    });
                    *bytes = serde_json::to_vec(&ShuttleResourceOutput {
                        output: secrets.clone(),
                        custom: shuttle_resource.custom,
                    })
                    .unwrap();
                }
                resource::Type::Persist => {
                    // only show that this resource is "connected"
                    mocked_responses.push(resource::Response {
                        r#type: shuttle_resource.r#type,
                        config: serde_json::Value::Null,
                        data: serde_json::Value::Null,
                    });
                }
                resource::Type::Container => {
                    let config = serde_json::from_value(shuttle_resource.config)
                        .context("deserializing resource config")?;
                    let res = prov.start_container(config).await.context("Failed to start Docker container. Make sure that a Docker engine is running.")?;
                    *bytes = serde_json::to_vec(&ShuttleResourceOutput {
                        output: res,
                        custom: shuttle_resource.custom,
                    })
                    .unwrap();
                }
            }
        }

        Ok((resources, mocked_responses))
    }

    async fn stop_runtime(
        runtime: &mut Child,
        runtime_client: &mut runtime::Client,
    ) -> Result<(), Status> {
        let stop_request = StopRequest {};
        trace!(?stop_request, "stopping service");
        let response = runtime_client
            .stop(tonic::Request::new(stop_request))
            .or_else(|err| async {
                runtime.kill().await?;
                trace!(status = ?err, "killed the runtime by force because stopping it errored out");
                Err(err)
            })
            .await?
            .into_inner();
        trace!(response = ?response, "client stop response: ");
        Ok(())
    }

    async fn add_runtime_info(
        runtime: Option<(Child, runtime::Client)>,
        existing_runtimes: &mut Vec<(Child, runtime::Client)>,
    ) -> Result<(), Status> {
        match runtime {
            Some(inner) => {
                trace!("Adding runtime PID: {:?}", inner.0.id());
                existing_runtimes.push(inner);
            }
            None => {
                trace!("Runtime error: No runtime process. Crashed during startup?");
                for rt_info in existing_runtimes {
                    let mut errored_out = false;
                    // Stopping all runtimes gracefully first, but if this errors out the function kills the runtime forcefully.
                    Shuttle::stop_runtime(&mut rt_info.0, &mut rt_info.1)
                        .await
                        .unwrap_or_else(|_| {
                            errored_out = true;
                        });

                    // If the runtime stopping is successful, we still need to kill it forcefully because we exit outside the loop
                    // and destructors will not be guaranteed to run.
                    if !errored_out {
                        rt_info.0.kill().await?;
                    }
                }
                exit(1);
            }
        };
        Ok(())
    }

    async fn pre_local_run(&self, run_args: &RunArgs) -> Result<Vec<BuiltService>> {
        trace!("starting a local run with args: {run_args:?}");

        let (tx, mut rx) = tokio::sync::mpsc::channel::<String>(256);
        tokio::task::spawn(async move {
            while let Some(line) = rx.recv().await {
                println!("{line}");
            }
        });

        let working_directory = self.ctx.working_directory();

        trace!("building project");
        println!(
            "{} {}",
            "    Building".bold().green(),
            working_directory.display()
        );

        build_workspace(working_directory, run_args.release, tx, false).await
    }

    fn find_available_port(run_args: &mut RunArgs, services_len: usize) {
        let default_port = run_args.port;
        'outer: for port in (run_args.port..=u16::MAX).step_by(services_len.max(10)) {
            for inner_port in port..(port + services_len as u16) {
                if !portpicker::is_free_tcp(inner_port) {
                    continue 'outer;
                }
            }
            run_args.port = port;
            break;
        }

        if run_args.port != default_port
            && !Confirm::with_theme(&ColorfulTheme::default())
                .with_prompt(format!(
                    "Port {} is already in use. Would you like to continue on port {}?",
                    default_port, run_args.port
                ))
                .default(true)
                .interact()
                .unwrap()
        {
            exit(0);
        }
    }
    fn find_available_port_beta(run_args: &mut RunArgs) {
        let original_port = run_args.port;
        for port in (run_args.port..=u16::MAX).step_by(10) {
            if !portpicker::is_free_tcp(port) {
                continue;
            }
            run_args.port = port;
            break;
        }

        if run_args.port != original_port {
            eprintln!(
                "Port {} is already in use. Using port {}.",
                original_port, run_args.port,
            )
        };
    }

    #[cfg(target_family = "unix")]
    async fn local_run(&self, mut run_args: RunArgs) -> Result<()> {
        let services = self.pre_local_run(&run_args).await?;

        let mut sigterm_notif =
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
                .expect("Can not get the SIGTERM signal receptor");
        let mut sigint_notif =
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
                .expect("Can not get the SIGINT signal receptor");

        // Start all the services.
        let mut runtimes: Vec<(Child, runtime::Client)> = Vec::new();

        Shuttle::find_available_port(&mut run_args, services.len());

        let mut signal_received = false;
        for (i, service) in services.iter().enumerate() {
            // We must cover the case of starting multiple workspace services and receiving a signal in parallel.
            // This must stop all the existing runtimes and creating new ones.
            signal_received = tokio::select! {
                res = Shuttle::spin_local_runtime(&run_args, service, i as u16) => {
                    match res {
                        Ok(runtime) => {
                            Shuttle::add_runtime_info(runtime, &mut runtimes).await?;
                        },
                        Err(e) => println!("Error while starting service: {e:?}"),
                    }
                    false
                },
                _ = sigterm_notif.recv() => {
                    println!(
                        "cargo-shuttle received SIGTERM. Killing all the runtimes..."
                    );
                    true
                },
                _ = sigint_notif.recv() => {
                    println!(
                        "cargo-shuttle received SIGINT. Killing all the runtimes..."
                    );
                    true
                }
            };

            if signal_received {
                break;
            }
        }

        // If prior signal received is set to true we must stop all the existing runtimes and
        // exit the `local_run`.
        if signal_received {
            for (mut rt, mut rt_client) in runtimes {
                Shuttle::stop_runtime(&mut rt, &mut rt_client)
                    .await
                    .unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
            }
            return Ok(());
        }

        // If no signal was received during runtimes initialization, then we must handle each runtime until
        // completion and handle the signals during this time.
        for (mut rt, mut rt_client) in runtimes {
            // If we received a signal while waiting for any runtime we must stop the rest and exit
            // the waiting loop.
            if signal_received {
                Shuttle::stop_runtime(&mut rt, &mut rt_client)
                    .await
                    .unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
                continue;
            }

            // Receiving a signal will stop the current runtime we're waiting for.
            signal_received = tokio::select! {
                res = rt.wait() => {
                    println!(
                        "a service future completed with exit status: {:?}",
                        res.unwrap().code()
                    );
                    false
                },
                _ = sigterm_notif.recv() => {
                    println!(
                        "cargo-shuttle received SIGTERM. Killing all the runtimes..."
                    );
                    Shuttle::stop_runtime(&mut rt, &mut rt_client).await.unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
                    true
                },
                _ = sigint_notif.recv() => {
                    println!(
                        "cargo-shuttle received SIGINT. Killing all the runtimes..."
                    );
                    Shuttle::stop_runtime(&mut rt, &mut rt_client).await.unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
                    true
                }
            };
        }

        println!(
            "Run `cargo shuttle project start` to create a project environment on Shuttle.\n\
             Run `cargo shuttle deploy` to deploy your Shuttle service."
        );

        Ok(())
    }

    async fn local_run_beta(&self, mut run_args: RunArgs, debug: bool) -> Result<()> {
        let project_name = self.ctx.project_name().to_owned();
        let services = self.pre_local_run(&run_args).await?;
        let service = services
            .first()
            .expect("at least one shuttle service")
            .to_owned();

        trace!(path = ?service.executable_path, "runtime executable");

        let secrets = Shuttle::get_secrets(&run_args, &service)?;
        Shuttle::find_available_port_beta(&mut run_args);
        if let Some(warning) = check_and_warn_runtime_version(&service.executable_path).await? {
            eprint!("{}", warning);
        }

        let runtime_executable = service.executable_path.clone();
        let api_port = portpicker::pick_unused_port()
            .expect("failed to find available port for local provisioner server");
        let api_addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), api_port);
        let ip = if run_args.external {
            Ipv4Addr::UNSPECIFIED
        } else {
            Ipv4Addr::LOCALHOST
        };

        let state = Arc::new(ProvApiState {
            project_name: project_name.clone(),
            secrets,
        });
        ProvisionerServerBeta::start(state, &api_addr);

        println!(
            "\n    {} {} on http://{}:{}\n",
            "Starting".bold().green(),
            service.package_name,
            ip,
            run_args.port,
        );

        let mut envs = vec![
            ("SHUTTLE_BETA", "true".to_owned()),
            ("SHUTTLE_PROJECT_ID", "proj_LOCAL".to_owned()),
            ("SHUTTLE_PROJECT_NAME", project_name),
            ("SHUTTLE_ENV", Environment::Local.to_string()),
            ("SHUTTLE_RUNTIME_IP", ip.to_string()),
            ("SHUTTLE_RUNTIME_PORT", run_args.port.to_string()),
            ("SHUTTLE_API", format!("http://127.0.0.1:{}", api_port)),
        ];
        // Use a nice debugging tracing level if user does not provide their own
        if debug && std::env::var("RUST_LOG").is_err() {
            envs.push(("RUST_LOG", "info,shuttle=trace,reqwest=debug".to_owned()));
        }

        info!(
            path = %runtime_executable.display(),
            "Spawning runtime process",
        );
        let mut runtime = tokio::process::Command::new(
            dunce::canonicalize(runtime_executable).context("canonicalize path of executable")?,
        )
        .current_dir(&service.workspace_path)
        .envs(envs)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .kill_on_drop(true)
        .spawn()
        .context("spawning runtime process")?;

        let raw = run_args.raw;
        let mut stdout_reader = BufReader::new(
            runtime
                .stdout
                .take()
                .context("child process did not have a handle to stdout")?,
        )
        .lines();
        tokio::spawn(async move {
            while let Some(line) = stdout_reader.next_line().await.unwrap() {
                if raw {
                    println!("{}", line);
                } else {
                    let log_item = LogItemBeta::new(Utc::now(), "app".to_owned(), line);
                    println!("{log_item}");
                }
            }
        });
        let mut stderr_reader = BufReader::new(
            runtime
                .stderr
                .take()
                .context("child process did not have a handle to stderr")?,
        )
        .lines();
        tokio::spawn(async move {
            while let Some(line) = stderr_reader.next_line().await.unwrap() {
                if raw {
                    println!("{}", line);
                } else {
                    let log_item = LogItemBeta::new(Utc::now(), "app".to_owned(), line);
                    println!("{log_item}");
                }
            }
        });

        #[cfg(target_family = "unix")]
        let exit_result = {
            let mut sigterm_notif =
                tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
                    .expect("Can not get the SIGTERM signal receptor");
            let mut sigint_notif =
                tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
                    .expect("Can not get the SIGINT signal receptor");
            tokio::select! {
                exit_result = runtime.wait() => {
                    Some(exit_result)
                }
                _ = sigterm_notif.recv() => {
                    eprintln!("cargo-shuttle received SIGTERM. Killing the runtime...");
                    None
                },
                _ = sigint_notif.recv() => {
                    eprintln!("cargo-shuttle received SIGINT. Killing the runtime...");
                    None
                }
            }
        };
        #[cfg(target_family = "windows")]
        let exit_result = {
            let mut ctrl_break_notif = tokio::signal::windows::ctrl_break()
                .expect("Can not get the CtrlBreak signal receptor");
            let mut ctrl_c_notif =
                tokio::signal::windows::ctrl_c().expect("Can not get the CtrlC signal receptor");
            let mut ctrl_close_notif = tokio::signal::windows::ctrl_close()
                .expect("Can not get the CtrlClose signal receptor");
            let mut ctrl_logoff_notif = tokio::signal::windows::ctrl_logoff()
                .expect("Can not get the CtrlLogoff signal receptor");
            let mut ctrl_shutdown_notif = tokio::signal::windows::ctrl_shutdown()
                .expect("Can not get the CtrlShutdown signal receptor");
            tokio::select! {
                exit_result = runtime.wait() => {
                    Some(exit_result)
                }
                _ = ctrl_break_notif.recv() => {
                    eprintln!("cargo-shuttle received ctrl-break.");
                    None
                },
                _ = ctrl_c_notif.recv() => {
                    eprintln!("cargo-shuttle received ctrl-c.");
                    None
                },
                _ = ctrl_close_notif.recv() => {
                    eprintln!("cargo-shuttle received ctrl-close.");
                    None
                },
                _ = ctrl_logoff_notif.recv() => {
                    eprintln!("cargo-shuttle received ctrl-logoff.");
                    None
                },
                _ = ctrl_shutdown_notif.recv() => {
                    eprintln!("cargo-shuttle received ctrl-shutdown.");
                    None
                }
            }
        };
        match exit_result {
            Some(Ok(exit_status)) => {
                bail!(
                    "Runtime process exited with code {}",
                    exit_status.code().unwrap_or_default()
                );
            }
            Some(Err(e)) => {
                bail!("Failed to wait for runtime process to exit: {e}");
            }
            None => {
                runtime.kill().await?;
            }
        }

        Ok(())
    }

    #[cfg(target_family = "windows")]
    async fn handle_signals() -> bool {
        let mut ctrl_break_notif = tokio::signal::windows::ctrl_break()
            .expect("Can not get the CtrlBreak signal receptor");
        let mut ctrl_c_notif =
            tokio::signal::windows::ctrl_c().expect("Can not get the CtrlC signal receptor");
        let mut ctrl_close_notif = tokio::signal::windows::ctrl_close()
            .expect("Can not get the CtrlClose signal receptor");
        let mut ctrl_logoff_notif = tokio::signal::windows::ctrl_logoff()
            .expect("Can not get the CtrlLogoff signal receptor");
        let mut ctrl_shutdown_notif = tokio::signal::windows::ctrl_shutdown()
            .expect("Can not get the CtrlShutdown signal receptor");

        tokio::select! {
            _ = ctrl_break_notif.recv() => {
                println!("cargo-shuttle received ctrl-break.");
                true
            },
            _ = ctrl_c_notif.recv() => {
                println!("cargo-shuttle received ctrl-c.");
                true
            },
            _ = ctrl_close_notif.recv() => {
                println!("cargo-shuttle received ctrl-close.");
                true
            },
            _ = ctrl_logoff_notif.recv() => {
                println!("cargo-shuttle received ctrl-logoff.");
                true
            },
            _ = ctrl_shutdown_notif.recv() => {
                println!("cargo-shuttle received ctrl-shutdown.");
                true
            }
            else => {
                false
            }
        }
    }

    #[cfg(target_family = "windows")]
    async fn local_run(&self, mut run_args: RunArgs) -> Result<()> {
        let services = self.pre_local_run(&run_args).await?;

        // Start all the services.
        let mut runtimes: Vec<(Child, runtime::Client)> = Vec::new();

        Shuttle::find_available_port(&mut run_args, services.len());

        let mut signal_received = false;
        for (i, service) in services.iter().enumerate() {
            signal_received = tokio::select! {
                res = Shuttle::spin_local_runtime(&run_args, service, i as u16) => {
                    Shuttle::add_runtime_info(res.unwrap(), &mut runtimes).await?;
                    false
                },
                _ = Shuttle::handle_signals() => {
                    println!(
                        "Killing all the runtimes..."
                    );
                    true
                }
            };

            if signal_received {
                break;
            }
        }

        // If prior signal received is set to true we must stop all the existing runtimes and
        // exit the `local_run`.
        if signal_received {
            for (mut rt, mut rt_client) in runtimes {
                Shuttle::stop_runtime(&mut rt, &mut rt_client)
                    .await
                    .unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
            }
            return Ok(());
        }

        // If no signal was received during runtimes initialization, then we must handle each runtime until
        // completion and handle the signals during this time.
        for (mut rt, mut rt_client) in runtimes {
            // If we received a signal while waiting for any runtime we must stop the rest and exit
            // the waiting loop.
            if signal_received {
                Shuttle::stop_runtime(&mut rt, &mut rt_client)
                    .await
                    .unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
                continue;
            }

            // Receiving a signal will stop the current runtime we're waiting for.
            signal_received = tokio::select! {
                res = rt.wait() => {
                    println!(
                        "a service future completed with exit status: {:?}",
                        res.unwrap().code()
                    );
                    false
                },
                _ = Shuttle::handle_signals() => {
                    println!(
                        "Killing all the runtimes..."
                    );
                    Shuttle::stop_runtime(&mut rt, &mut rt_client).await.unwrap_or_else(|err| {
                        trace!(status = ?err, "stopping the runtime errored out");
                    });
                    true
                }
            };
        }

        println!(
            "Run `cargo shuttle project start` to create a project environment on Shuttle.\n\
             Run `cargo shuttle deploy` to deploy your Shuttle service."
        );

        Ok(())
    }

    async fn deploy(&mut self, args: DeployArgs) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let working_directory = self.ctx.working_directory();
        let manifest_path = working_directory.join("Cargo.toml");
        let project_name = self.ctx.project_name();

        let secrets = if self.beta {
            Shuttle::get_secrets_beta(&args.secret_args, working_directory)?
        } else {
            None
        };

        // Beta: Image deployment mode
        if self.beta {
            if let Some(image) = args.image {
                let pid = self.ctx.project_id();
                let deployment_req_image_beta = DeploymentRequestImageBeta { image, secrets };

                let deployment = client
                    .deploy_beta(pid, DeploymentRequestBeta::Image(deployment_req_image_beta))
                    .await?;

                if args.no_follow {
                    println!("{}", deployment.to_string_colored());
                    return Ok(());
                }

                // TODO?: Make it print logs on fail
                self.track_deployment_status_beta(pid, &deployment.id)
                    .await?;

                return Ok(());
            }
        }

        // Alpha and beta: Build archive deployment mode
        let mut deployment_req = DeploymentRequest {
            no_test: args.no_test,
            ..Default::default()
        };
        let mut deployment_req_beta = DeploymentRequestBuildArchiveBeta {
            secrets,
            ..Default::default()
        };

        if self.beta {
            let mut rust_build_args = BuildArgsRustBeta::default();

            let metadata = async_cargo_metadata(manifest_path.as_path()).await?;
            let packages = find_shuttle_packages(&metadata)?;
            // TODO: support overriding this
            let package = packages
                .first()
                .expect("Expected at least one crate with shuttle-runtime in the workspace");
            let package_name = package.name.to_owned();
            rust_build_args.package_name = Some(package_name);

            // activate shuttle feature if present
            let (no_default_features, features) = if package.features.contains_key("shuttle") {
                (true, Some(vec!["shuttle".to_owned()]))
            } else {
                (false, None)
            };
            rust_build_args.no_default_features = no_default_features;
            rust_build_args.features = features.map(|v| v.join(","));

            rust_build_args.shuttle_runtime_version = package
                .dependencies
                .iter()
                .find(|dependency| dependency.name == RUNTIME_NAME)
                .expect("shuttle package to have runtime dependency")
                .req
                .comparators
                .first()
                // is "^0.X.0" when `shuttle-runtime = "0.X.0"` is in Cargo.toml
                .and_then(|c| c.to_string().strip_prefix('^').map(ToOwned::to_owned));

            // TODO: determine which (one) binary to build

            deployment_req_beta.build_args = Some(BuildArgsBeta::Rust(rust_build_args));

            // TODO: have all of the above be configurable in CLI and Shuttle.toml
        }

        if let Ok(repo) = Repository::discover(working_directory) {
            let repo_path = repo
                .workdir()
                .context("getting working directory of repository")?;
            let repo_path = dunce::canonicalize(repo_path)?;
            trace!(?repo_path, "found git repository");

            let dirty = is_dirty(&repo);
            deployment_req.git_dirty = Some(dirty.is_err());

            let check_dirty = !self.beta || self.ctx.deny_dirty().is_some_and(|d| d);
            if check_dirty && !args.allow_dirty && dirty.is_err() {
                bail!(dirty.unwrap_err());
            }

            if let Ok(head) = repo.head() {
                // This is typically the name of the current branch
                // It is "HEAD" when head detached, for example when a tag is checked out
                deployment_req.git_branch = head
                    .shorthand()
                    .map(|s| s.chars().take(GIT_STRINGS_MAX_LENGTH).collect());
                if let Ok(commit) = head.peel_to_commit() {
                    deployment_req.git_commit_id = Some(commit.id().to_string());
                    // Summary is None if error or invalid utf-8
                    deployment_req.git_commit_msg = commit
                        .summary()
                        .map(|s| s.chars().take(GIT_STRINGS_MAX_LENGTH).collect());
                }
            }
        }

        if self.beta {
            eprintln!("Packing files...");
        }
        let archive = self.make_archive(args.secret_args.secrets.clone(), self.beta)?;

        if let Some(path) = args.output_archive {
            eprintln!("Writing archive to {}", path.display());
            std::fs::write(path, archive).context("writing archive")?;

            return Ok(());
        }

        if !self.beta && archive.len() > CREATE_SERVICE_BODY_LIMIT {
            bail!(
                r#"The project is too large - the limit is {} MB. \
                Your project archive is {:.1} MB. \
                Run with `cargo shuttle --debug` to see which files are being packed."#,
                CREATE_SERVICE_BODY_LIMIT / 1_000_000,
                archive.len() as f32 / 1_000_000f32,
            );
        }

        // End early for beta
        if self.beta {
            // TODO: upload secrets separately

            let pid = self.ctx.project_id();

            eprintln!("Uploading code...");
            let arch = client.upload_archive_beta(pid, archive).await?;
            deployment_req_beta.archive_version_id = arch.archive_version_id;
            deployment_req_beta.build_meta = Some(BuildMetaBeta {
                git_commit_id: deployment_req.git_commit_id,
                git_commit_msg: deployment_req.git_commit_msg,
                git_branch: deployment_req.git_branch,
                git_dirty: deployment_req.git_dirty,
            });

            eprintln!("Creating deployment...");
            let deployment = client
                .deploy_beta(
                    pid,
                    DeploymentRequestBeta::BuildArchive(deployment_req_beta),
                )
                .await?;

            if args.no_follow {
                println!("{}", deployment.to_string_colored());
                return Ok(());
            }

            if self
                .track_deployment_status_beta(pid, &deployment.id)
                .await?
            {
                for log in client
                    .get_deployment_logs_beta(pid, &deployment.id)
                    .await?
                    .logs
                {
                    if args.raw {
                        println!("{}", log.line);
                    } else {
                        println!("{log}");
                    }
                }
            }

            return Ok(());
        }

        deployment_req.data = archive;
        let deployment = client
            .deploy(project_name, deployment_req)
            .await
            .map_err(suggestions::deploy::deploy_request_failure)?;

        let mut stream = client
            .get_logs_ws(project_name, &deployment.id.to_string(), LogsRange::All)
            .await
            .map_err(|err| {
                suggestions::deploy::deployment_setup_failure(
                    err,
                    "Connecting to the deployment logs failed",
                )
            })?;

        let mut deployer_version_checked = false;
        let mut runtime_version_checked = false;
        while let Ok(Some(s)) = read_ws_until_text(&mut stream).await {
            let log_item = match serde_json::from_str::<LogItem>(&s) {
                Ok(log_item) => log_item,
                Err(err) => {
                    debug!(error = %err, "failed to parse message into log item");

                    let message = if let Ok(err) = serde_json::from_str::<ApiError>(&s) {
                        err.to_string()
                    } else {
                        "failed to parse logs, is your cargo-shuttle outdated?".to_string()
                    };

                    bail!(message);
                }
            };

            if args.raw {
                println!("{}", log_item.get_raw_line())
            } else {
                println!("{log_item}")
            }

            // Detect versions of deployer and runtime, and print warnings of outdated.
            if !deployer_version_checked
                && self.version_info.is_some()
                && log_item.line.contains("Deployer version: ")
            {
                deployer_version_checked = true;
                let my_version = &log_item
                    .line
                    .split_once("Deployer version: ")
                    .unwrap()
                    .1
                    .parse::<semver::Version>()
                    .context("parsing deployer version in log stream")?;
                let latest_version = &self.version_info.as_ref().unwrap().deployer;
                if latest_version > my_version {
                    self.version_warnings.push(
                                formatdoc! {"
                                    Warning:
                                        A newer version of shuttle-deployer is available ({latest_version}).
                                        Use `cargo shuttle project restart` to upgrade."
                                }
                                .yellow()
                                .to_string(),
                            )
                }
            }
            if !runtime_version_checked
                && self.version_info.is_some()
                && log_item
                    .line
                    .contains("shuttle-runtime executable started (version ")
            {
                runtime_version_checked = true;
                let my_version = &log_item
                    .line
                    .split_once("shuttle-runtime executable started (version ")
                    .unwrap()
                    .1
                    .split_once(')')
                    .unwrap()
                    .0
                    .parse::<semver::Version>()
                    .context("parsing runtime version in log stream")?;
                let latest_version = &self.version_info.as_ref().unwrap().runtime;
                if latest_version > my_version {
                    self.version_warnings.push(
                                formatdoc! {"
                                    Warning:
                                        A newer version of shuttle-runtime is available ({latest_version}).
                                        Update it and any other shuttle dependencies in Cargo.toml."
                                }
                                .yellow()
                                .to_string(),
                            )
                }
            }

            // Determine when to stop listening to the log stream
            if DEPLOYER_END_MESSAGES_BAD
                .iter()
                .any(|m| log_item.line.contains(m))
            {
                println!();
                println!("{}", "Deployment crashed".red());
                println!();
                println!("Run the following for more details");
                println!();
                println!("cargo shuttle logs {}", &deployment.id);

                bail!("");
            }
            if DEPLOYER_END_MESSAGES_GOOD
                .iter()
                .any(|m| log_item.line.contains(m))
            {
                debug!("received end message, breaking deployment stream");
                break;
            }
        }

        // Temporary fix.
        // TODO: Make get_service_summary endpoint wait for a bit and see if it entered Running/Crashed state.
        // Note: Will otherwise be possible when health checks are supported
        sleep(Duration::from_millis(500)).await;

        let deployment = client
            .get_deployment_details(project_name, &deployment.id)
            .await
            .map_err(|err| {
                suggestions::deploy::deployment_setup_failure(
                    err,
                    "Assessing deployment state failed",
                )
            })?;

        // A deployment will only exist if there is currently one in the running state
        if deployment.state != shuttle_common::deployment::State::Running {
            println!("{}", "Deployment has not entered the running state".red());
            println!();

            match deployment.state {
                shuttle_common::deployment::State::Stopped => {
                    println!("State: Stopped - Deployment was running, but has been stopped by the user.")
                }
                shuttle_common::deployment::State::Completed => {
                    println!("State: Completed - Deployment was running, but stopped running all by itself.")
                }
                shuttle_common::deployment::State::Unknown => {
                    println!("State: Unknown - Deployment was in an unknown state. We never expect this state and entering this state should be considered a bug.")
                }
                shuttle_common::deployment::State::Crashed => {
                    println!(
                        "{}",
                        "State: Crashed - Deployment crashed after startup.".red()
                    );
                }
                state => {
                    debug!("deployment logs stream received state: {state} when it expected to receive running state");
                    println!(
                        "Deployment entered an unexpected state - Please create a ticket to report this."
                    );
                }
            }

            println!();
            println!("Run the following for more details");
            println!();
            println!("cargo shuttle logs {}", &deployment.id);

            bail!("");
        }

        let service = client.get_service(project_name).await?;
        let resources = client.get_service_resources(project_name).await?;
        let resources = get_resource_tables(&resources, project_name, false, false);

        println!("{resources}{service}");

        Ok(())
    }

    /// Returns true if the deployment failed
    async fn track_deployment_status_beta(&self, pid: &str, id: &str) -> Result<bool> {
        let client = self.client.as_ref().unwrap();
        let failed = wait_with_spinner(2000, |_, pb| async move {
            let deployment = client.get_deployment_beta(pid, id).await?;

            let state = deployment.state.clone();
            pb.set_message(deployment.to_string_summary_colored());
            let failed = state == DeploymentStateBeta::Failed;
            let cleanup = move || {
                println!("{}", deployment.to_string_colored());
                failed
            };
            match state {
                DeploymentStateBeta::Pending
                | DeploymentStateBeta::Building
                | DeploymentStateBeta::InProgress => Ok(None),
                DeploymentStateBeta::Running
                | DeploymentStateBeta::Stopped
                | DeploymentStateBeta::Stopping
                | DeploymentStateBeta::Unknown
                | DeploymentStateBeta::Failed => Ok(Some(cleanup)),
            }
        })
        .await?;

        Ok(failed)
    }

    async fn project_start(&self, idle_minutes: u64) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let config = &project::Config { idle_minutes };

        let p = self.ctx.project_name();
        wait_with_spinner(500, |i, pb| async move {
            let project = if i == 0 {
                client.create_project(p, config).await?
            } else {
                client.get_project(p).await?
            };
            pb.set_message(format!("{project}"));

            let done = [
                project::State::Ready,
                project::State::Errored {
                    message: Default::default(),
                },
            ]
            .contains(&project.state);

            if done {
                Ok(Some(move || {
                    println!("{project}");
                }))
            } else {
                Ok(None)
            }
        })
        .await
        .map_err(|err| {
            suggestions::project::project_request_failure(
                err,
                "Project creation failed",
                true,
                "the project creation or retrieving the status fails repeatedly",
            )
        })?;

        if idle_minutes > 0 && !self.beta {
            let idle_msg = format!(
                "Your project will sleep if it is idle for {} minutes.",
                idle_minutes
            );
            println!("{}", idle_msg.yellow());
            println!("To change the idle time refer to the docs: {SHUTTLE_IDLE_DOCS_URL}");
            println!();
        }

        println!("Run `cargo shuttle deploy --allow-dirty` to deploy your Shuttle service.");

        Ok(())
    }

    async fn project_create_beta(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let name = self.ctx.project_name();
        let project = client.create_project_beta(name).await?;

        println!("Created project '{}' with id {}", project.name, project.id);

        Ok(())
    }
    async fn project_rename_beta(&self, name: String) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        let project = client
            .update_project_beta(
                self.ctx.project_id(),
                ProjectUpdateRequestBeta {
                    name: Some(name),
                    ..Default::default()
                },
            )
            .await?;

        println!("Renamed project {} to {}", project.id, project.name);

        Ok(())
    }

    async fn project_restart(&self, idle_minutes: u64) -> Result<()> {
        self.project_stop()
            .await
            .map_err(suggestions::project::project_restart_failure)?;
        self.project_start(idle_minutes)
            .await
            .map_err(suggestions::project::project_restart_failure)?;

        Ok(())
    }

    async fn projects_list(&self, table_args: TableArgs) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        let projects_table = if self.beta {
            project::get_projects_table_beta(
                &client.get_projects_list_beta().await?.projects,
                table_args.raw,
            )
        } else {
            project::get_projects_table(
                &client.get_projects_list().await.map_err(|err| {
                    suggestions::project::project_request_failure(
                        err,
                        "Getting projects list failed",
                        false,
                        "getting the projects list fails repeatedly",
                    )
                })?,
                table_args.raw,
            )
        };

        println!("{}", "Personal Projects".bold());
        println!("{projects_table}\n");

        if !self.beta {
            let teams = client.get_teams_list().await?;

            for team in teams {
                let team_projects = client.get_team_projects_list(&team.id).await?;
                let team_projects_table =
                    project::get_projects_table(&team_projects, table_args.raw);

                println!("{}", format!("{}'s Projects", team.display_name).bold());
                println!("{team_projects_table}\n");
            }
        }

        Ok(())
    }

    async fn project_status(&self, follow: bool) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        if follow {
            let p = self.ctx.project_name();
            wait_with_spinner(500, |_, pb| async move {
                let project = client.get_project(p).await?;
                pb.set_message(format!("{project}"));

                let done = [
                    project::State::Ready,
                    project::State::Destroyed,
                    project::State::Errored {
                        message: Default::default(),
                    },
                ]
                .contains(&project.state);

                if done {
                    Ok(Some(move || {
                        println!("{project}");
                    }))
                } else {
                    Ok(None)
                }
            })
            .await?;
        } else {
            let project = client
                .get_project(self.ctx.project_name())
                .await
                .map_err(|err| {
                    suggestions::project::project_request_failure(
                        err,
                        "Getting project status failed",
                        false,
                        "getting project status failed repeatedly",
                    )
                })?;
            println!(
                "{project}\nIdle minutes: {}",
                project
                    .idle_minutes
                    .map(|i| i.to_string())
                    .unwrap_or("<unknown>".to_owned())
            );
        }

        Ok(())
    }
    async fn project_status_beta(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let project = client.get_project_beta(self.ctx.project_id()).await?;
        print!("{}", project.to_string_colored());

        Ok(())
    }

    async fn project_stop(&self) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        let p = self.ctx.project_name();
        wait_with_spinner(500, |i, pb| async move {
            let project = if i == 0 {
                client.stop_project(p).await?
            } else {
                client.get_project(p).await?
            };
            pb.set_message(format!("{project}"));

            let done = [
                project::State::Destroyed,
                project::State::Errored {
                    message: Default::default(),
                },
            ]
            .contains(&project.state);

            if done {
                Ok(Some(move || {
                    println!("{project}");
                }))
            } else {
                Ok(None)
            }
        })
        .await
        .map_err(|err| {
            suggestions::project::project_request_failure(
                err,
                "Project stop failed",
                true,
                "stopping the project or getting project status fails repeatedly",
            )
        })?;
        println!("Run `cargo shuttle project start` to recreate project environment on Shuttle.");

        Ok(())
    }

    async fn project_delete_beta(&self, no_confirm: bool) -> Result<()> {
        let client = self.client.as_ref().unwrap();
        let pid = self.ctx.project_id();

        if !no_confirm {
            println!(
                "{}",
                formatdoc!(
                    r#"
                    WARNING:
                        Are you sure you want to delete "{pid}"?
                        This will...
                        - Shut down you service.
                        - Delete any databases and secrets in this project.
                        - Delete any custom domains linked to this project.
                        This action is permanent."#
                )
                .bold()
                .red()
            );
            if !Confirm::with_theme(&ColorfulTheme::default())
                .with_prompt("Are you sure?")
                .default(false)
                .interact()
                .unwrap()
            {
                return Ok(());
            }
        }

        let res = client.delete_project_beta(pid).await?;

        println!("{res}");

        Ok(())
    }

    async fn project_delete(&self, no_confirm: bool) -> Result<()> {
        let client = self.client.as_ref().unwrap();

        if !no_confirm {
            println!(
                "{}",
                formatdoc!(
                    r#"
                    WARNING:
                        Are you sure you want to delete "{}"?
                        This will...
                        - Delete any databases, secrets, and shuttle-persist data in this project.
                        - Delete any custom domains linked to this project.
                        - Release the project name from your account.
                        This action is permanent."#,
                    self.ctx.project_name()
                )
                .bold()
                .red()
            );
            if !Confirm::with_theme(&ColorfulTheme::default())
                .with_prompt("Are you sure?")
                .default(false)
                .interact()
                .unwrap()
            {
                return Ok(());
            }
        }

        client
            .delete_project(self.ctx.project_name())
            .await
            .map_err(|err| {
                suggestions::project::project_request_failure(
                    err,
                    "Project delete failed",
                    true,
                    "deleting the project or getting project status fails repeatedly",
                )
            })?;

        println!("Deleted project");

        Ok(())
    }

    fn make_archive(&self, secrets_file: Option<PathBuf>, zip: bool) -> Result<Vec<u8>> {
        let include_patterns = self.ctx.include();

        let working_directory = self.ctx.working_directory();

        //
        // Mixing include and exclude overrides messes up the .ignore and .gitignore etc,
        // therefore these "ignore" walk and the "include" walk are separate.
        //
        let mut entries = Vec::new();

        // Default excludes
        let ignore_overrides = OverrideBuilder::new(working_directory)
            .add("!.git/")
            .context("adding override `!.git/`")?
            .add("!target/")
            .context("adding override `!target/`")?
            // these should always be ignored when unpacked in deployment, so ignore them here as well
            .add(&format!("!{EXECUTABLE_DIRNAME}/"))
            .context(format!("adding override `!{EXECUTABLE_DIRNAME}/`"))?
            .add(&format!("!{STORAGE_DIRNAME}/"))
            .context(format!("adding override `!{STORAGE_DIRNAME}/`"))?
            .build()
            .context("building archive override rules")?;
        for r in WalkBuilder::new(working_directory)
            .hidden(false)
            .overrides(ignore_overrides)
            .build()
        {
            entries.push(r.context("list dir entry")?.into_path())
        }

        let mut globs = GlobSetBuilder::new();

        if let Some(secrets_file) = secrets_file.clone() {
            entries.push(secrets_file);
        } else {
            // Default: Include all Secrets.toml files
            globs.add(Glob::new("**/Secrets.toml").unwrap());
        }

        // User provided includes
        if let Some(rules) = include_patterns {
            for r in rules {
                globs.add(Glob::new(r.as_str()).context(format!("parsing glob pattern {:?}", r))?);
            }
        }

        // Find the files
        let globs = globs.build().context("glob glob")?;
        for entry in walkdir::WalkDir::new(working_directory) {
            let path = entry.context("list dir")?.into_path();
            if globs.is_match(
                path.strip_prefix(working_directory)
                    .context("strip prefix of path")?,
            ) {
                entries.push(path);
            }
        }

        let mut archive_files = BTreeMap::new();
        for path in entries {
            // It's not possible to add a directory to an archive
            if path.is_dir() {
                trace!("Skipping {:?}: is a directory", path);
                continue;
            }
            // symlinks == chaos
            if path.is_symlink() {
                trace!("Skipping {:?}: is a symlink", path);
                continue;
            }

            // zip file puts all files in root, tar puts all files nested in a dir at root level
            let prefix = if zip {
                working_directory
            } else {
                working_directory.parent().context("get parent dir")?
            };
            let mut name = path
                .strip_prefix(prefix)
                .context("strip prefix of path")?
                .to_owned();

            // if this is the custom secrets file, rename it to Secrets.toml
            if secrets_file.as_ref().is_some_and(|sf| sf == &path) {
                name.pop();
                name.push("Secrets.toml");
            }

            archive_files.insert(path, name);
        }

        if archive_files.is_empty() {
            error!("No files included in upload. Aborting...");
            bail!("No files included in upload.");
        }

        let bytes = if zip {
            debug!("making zip archive");
            let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
            for (path, name) in archive_files {
                debug!("Packing {path:?}");

                // windows things
                let name = name.to_str().expect("valid filename").replace('\\', "/");
                zip.start_file(name, FileOptions::<()>::default())?;

                let mut b = Vec::new();
                File::open(path)?.read_to_end(&mut b)?;
                zip.write_all(&b)?;
            }
            let r = zip.finish().context("finish encoding zip archive")?;

            r.into_inner()
        } else {
            debug!("making tar archive");
            let encoder = GzEncoder::new(Vec::new(), Compression::new(3));
            let mut tar = Builder::new(encoder);
            for (path, name) in archive_files {
                debug!("Packing {path:?}");
                tar.append_path_with_name(path, name)?;
            }
            let encoder = tar.into_inner().context("get encoder from tar archive")?;

            encoder.finish().context("finish encoding tar archive")?
        };
        debug!("Archive size: {} bytes", bytes.len());

        Ok(bytes)
    }
}

/// Calls async function `f` in a loop with `millis` sleep between iterations,
/// providing iteration count and reference to update the progress bar.
/// `f` returns Some with a cleanup function if done.
/// The cleanup function is called after teardown of progress bar,
/// and its return value is returned from here.
async fn wait_with_spinner<Fut, C, O>(
    millis: u64,
    f: impl Fn(usize, ProgressBar) -> Fut,
) -> Result<O, anyhow::Error>
where
    Fut: std::future::Future<Output = Result<Option<C>>>,
    C: FnOnce() -> O,
{
    let progress_bar = create_spinner();
    let mut count = 0usize;
    let cleanup = loop {
        if let Some(cleanup) = f(count, progress_bar.clone()).await? {
            break cleanup;
        }
        count += 1;
        sleep(Duration::from_millis(millis)).await;
    };
    progress_bar.finish_and_clear();

    Ok(cleanup())
}

fn create_spinner() -> ProgressBar {
    let pb = indicatif::ProgressBar::new_spinner();
    pb.enable_steady_tick(std::time::Duration::from_millis(250));
    pb.set_style(
        indicatif::ProgressStyle::with_template("{spinner:.orange} {msg}")
            .unwrap()
            .tick_strings(&[
                "( ●    )",
                "(  ●   )",
                "(   ●  )",
                "(    ● )",
                "(     ●)",
                "(    ● )",
                "(   ●  )",
                "(  ●   )",
                "( ●    )",
                "(●     )",
                "(●●●●●●)",
            ]),
    );

    pb
}

#[cfg(test)]
mod tests {
    use flate2::read::GzDecoder;
    use tar::Archive;
    use zip::ZipArchive;

    use crate::args::{DeployArgs, ProjectArgs, SecretsArgs};
    use crate::Shuttle;
    use std::fs::{self, canonicalize};
    use std::io::Cursor;
    use std::path::PathBuf;

    pub fn path_from_workspace_root(path: &str) -> PathBuf {
        let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
            .join("..")
            .join(path);

        dunce::canonicalize(path).unwrap()
    }

    async fn get_archive_entries(
        project_args: ProjectArgs,
        deploy_args: DeployArgs,
        zip: bool,
    ) -> Vec<String> {
        let mut shuttle = Shuttle::new(crate::Binary::CargoShuttle).unwrap();
        shuttle
            .load_project(&project_args, false, false)
            .await
            .unwrap();

        let archive = shuttle
            .make_archive(deploy_args.secret_args.secrets, zip)
            .unwrap();

        if zip {
            let mut zip = ZipArchive::new(Cursor::new(archive)).unwrap();
            (0..zip.len())
                .map(|i| zip.by_index(i).unwrap().name().to_owned())
                .collect()
        } else {
            let tar = GzDecoder::new(&archive[..]);
            let mut archive = Archive::new(tar);

            archive
                .entries()
                .unwrap()
                .map(|entry| {
                    entry
                        .unwrap()
                        .path()
                        .unwrap()
                        .components()
                        .skip(1)
                        .collect::<PathBuf>()
                        .display()
                        .to_string()
                })
                .collect()
        }
    }

    #[tokio::test]
    async fn make_archive_respect_rules() {
        let working_directory = canonicalize(path_from_workspace_root(
            "cargo-shuttle/tests/resources/archiving",
        ))
        .unwrap();

        fs::write(working_directory.join("Secrets.toml"), "KEY = 'value'").unwrap();
        fs::write(working_directory.join("Secrets.dev.toml"), "KEY = 'dev'").unwrap();
        fs::write(working_directory.join("asset2"), "").unwrap();
        fs::write(working_directory.join("asset4"), "").unwrap();
        fs::create_dir_all(working_directory.join("dist")).unwrap();
        fs::write(working_directory.join("dist").join("dist1"), "").unwrap();

        fs::create_dir_all(working_directory.join("target")).unwrap();
        fs::write(working_directory.join("target").join("binary"), b"12345").unwrap();

        let project_args = ProjectArgs {
            working_directory: working_directory.clone(),
            name_or_id: Some("archiving-test".to_owned()),
        };
        let mut entries =
            get_archive_entries(project_args.clone(), Default::default(), false).await;
        entries.sort();

        let expected = vec![
            ".gitignore",
            ".ignore",
            "Cargo.toml",
            "Secrets.toml", // always included by default
            "Secrets.toml.example",
            "Shuttle.toml",
            "asset1", // normal file
            "asset2", // .gitignore'd, but included in Shuttle.toml
            // asset3 is .ignore'd
            "asset4",                // .gitignore'd, but un-ignored in .ignore
            "asset5",                // .ignore'd, but included in Shuttle.toml
            "dist/dist1",            // .gitignore'd, but included in Shuttle.toml
            "nested/static/nested1", // normal file
            // nested/static/nestedignore is .gitignore'd
            "src/main.rs",
        ];
        assert_eq!(entries, expected);

        // check that zip behaves the same way
        let mut entries = get_archive_entries(project_args.clone(), Default::default(), true).await;
        entries.sort();
        assert_eq!(entries, expected);

        fs::remove_file(working_directory.join("Secrets.toml")).unwrap();
        let mut entries = get_archive_entries(
            project_args,
            DeployArgs {
                secret_args: SecretsArgs {
                    secrets: Some(working_directory.join("Secrets.toml.example")),
                },
                ..Default::default()
            },
            false,
        )
        .await;
        entries.sort();

        assert_eq!(
            entries,
            vec![
                ".gitignore",
                ".ignore",
                "Cargo.toml",
                "Secrets.toml", // got moved here
                // Secrets.toml.example was the given secrets file, so it got moved
                "Shuttle.toml",
                "asset1", // normal file
                "asset2", // .gitignore'd, but included in Shuttle.toml
                // asset3 is .ignore'd
                "asset4",                // .gitignore'd, but un-ignored in .ignore
                "asset5",                // .ignore'd, but included in Shuttle.toml
                "dist/dist1",            // .gitignore'd, but included in Shuttle.toml
                "nested/static/nested1", // normal file
                // nested/static/nestedignore is .gitignore'd
                "src/main.rs",
            ]
        );
    }

    #[tokio::test]
    async fn finds_workspace_root() {
        let project_args = ProjectArgs {
            working_directory: path_from_workspace_root("examples/axum/hello-world/src"),
            name_or_id: None,
        };

        let mut shuttle = Shuttle::new(crate::Binary::CargoShuttle).unwrap();
        shuttle
            .load_project(&project_args, false, false)
            .await
            .unwrap();

        assert_eq!(
            project_args.working_directory,
            path_from_workspace_root("examples/axum/hello-world/src")
        );
        assert_eq!(
            project_args.workspace_path().unwrap(),
            path_from_workspace_root("examples/axum/hello-world")
        );
    }
}