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
//! Sliderule is an implementation of the Distributed OSHW (Open Source Hardware) Framework ([`DOF`])
//! being developed by Mach 30 Foundation for Space Development.
//!
//! [`DOF`]: https://github.com/Mach30/dof
//!
//! Sliderule wraps the `git` and `npm` commands and uses them to manage DOF/Sliderule projects, both
//! on the local file system, and on a remote server. At this time only structure management is provided,
//! there is no capability to render models for documentation like assembly instructions out into their
//! distributable form.
//!
//! Central to understanding Sliderule is the concept of _local_ and _remote_ components. _Local_ components
//! are stored with a project, which is the top-level, enclosing component. Local components do not
//! have a repository associated with them, they are only stored in the `components` directory of a project.
//! Remote components, on the other hand, are stored in a remote repository, and are only installed into
//! the local file system if the user requests it. Remote components are intended to be shared, local components
//! are intended to only be used within their parent project. A local component can be converted into a remote
//! component later, if desired.
//!
//! The following is a list of the major operations available through this crate.
//! - _create_ - Creates a top level component unless creating a component inside of an existing component.
//!   In that case the new component is placed within the `components` directory of the parent "project" component.
//! - _add_ - Adds a remote component from a repository into the `node_modules` directory of the current component.
//! - _download_ - Downloads a copy of a component form a remote repository.
//! - _update_ - Downloads the latest changes to the component and/or its remote components (dependencies)
//! - _remove_ - Removes a component, whether it is local or remote.
//! - _upload_ - Uploads component changes on the local file system to its remote repository.
//! - _refactor_ - Converts a local component to a remote component so that it may be more easily shared.
//!
//! There are also various helper functions to do things like getting what level a component is in a hierarchy and
//! compiling the licenses of all components in a project.

#![allow(dead_code)]

extern crate liquid;
extern crate os_info;
extern crate walkdir;

use std::cmp::Ordering;
use std::fs;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

pub struct SROutput {
    pub status: i32,
    pub wrapped_status: i32,
    pub stdout: Vec<String>,
    pub stderr: Vec<String>,
}

/// Creates a new component or converts an existing directory into a component.
///
/// If `target_dir` is not a component directory, a new, top-level project component will be created.
/// If `target_dir` is a component directory, a new component is created in the existing `components`
/// directory. The name of the component is determine by the `name` parameter. Names are not allowed
/// to include dots. The source materials license `source_license` and documentation license (`doc_license`)
/// must be specified and must be from the [`SPDX`] license list.
///
/// [`SPDX`]: https://spdx.org/licenses/
///
/// # Examples
///
/// Creating a new top-level project component:
/// ```
/// extern crate sliderule;
///
/// let temp_dir = std::env::temp_dir();
///
/// let output = sliderule::create_component(
///     &temp_dir,
///     String::from("newproject"),
///     String::from("TestSourceLicense"),
///     String::from("TestDocLicense"),
/// );
///
/// assert!(temp_dir.join("newproject").exists());
/// ```
/// Creating a new local component:
/// ```
/// extern crate sliderule;
///
/// let temp_dir = std::env::temp_dir().join("newproject");
///
/// let output = sliderule::create_component(
///     &temp_dir,
///     String::from("localcomponent"),
///     String::from("TestSourceLicense"),
///     String::from("TestDocLicense"),
/// );
///
/// assert!(temp_dir.join("components").join("localcomponent").exists());
/// ```

pub fn create_component(
    target_dir: &Path,
    name: String,
    source_license: String,
    doc_license: String,
) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    // The path can either lead to a top level component (project), or a component nested within a project
    let component_dir: PathBuf;

    // This is a top level component (project)
    if target_dir.join(".sr").exists() {
        component_dir = target_dir.join("components").join(&name);
    } else {
        component_dir = target_dir.join(&name);
    }

    // Create a directory for our component
    match fs::create_dir(&component_dir) {
        Ok(_) => (),
        Err(e) => {
            output.status = 11;
            output.stderr.push(format!(
                "ERROR: Could not create component directory: {}",
                e
            ));
        }
    };

    // Create the components directory, if needed
    if !component_dir.join("components").exists() {
        match fs::create_dir(component_dir.join("components")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 12;
                output.stderr.push(format!(
                    "ERROR: Could not create components directory: {}",
                    e
                ));
            }
        };

        // Create a placeholder file to ensure that the directory gets pushed to the repo
        match fs::File::create(component_dir.join("components").join(".ph")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 21;
                output.stderr.push(format!(
                    "ERROR: Could not create placeholder file in components directory: {}",
                    e
                ));
            }
        };
    } else {
        output.stdout.push(String::from(
            "components directory already exists, using existing directory.",
        ));
    }

    // Create the dist directory, if needed
    if !component_dir.join("dist").exists() {
        match fs::create_dir(component_dir.join("dist")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 13;
                output
                    .stderr
                    .push(format!("ERROR: Could not create dist directory: {}", e));
            }
        };

        // Create a placeholder file to ensure that the directory gets pushed to the repo
        match fs::File::create(component_dir.join("dist").join(".ph")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 21;
                output.stderr.push(format!(
                    "ERROR: Could not create placeholder file in dist directory: {}",
                    e
                ));
            }
        };
    } else {
        output.stdout.push(String::from(
            "dist directory already exists, using existing directory.",
        ));
    }

    // Create the docs directory, if needed
    if !component_dir.join("docs").exists() {
        match fs::create_dir(component_dir.join("docs")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 14;
                output
                    .stderr
                    .push(format!("ERROR: Could not create docs directory: {}", e));
            }
        };

        // Create a placeholder file to ensure that the directory gets pushed to the repo
        match fs::File::create(component_dir.join("docs").join(".ph")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 21;
                output.stderr.push(format!(
                    "ERROR: Could not create placeholder file in docs directory: {}",
                    e
                ));
            }
        };
    } else {
        output.stdout.push(String::from(
            "docs directory already exists, using existing directory.",
        ));
    }

    //Create the source directory, if needed
    if !component_dir.join("source").exists() {
        match fs::create_dir(component_dir.join("source")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 15;
                output
                    .stderr
                    .push(format!("ERROR: Could not create source directory: {}", e));
            }
        };

        // Create a placeholder file to ensure that the directory gets pushed to the repo
        match fs::File::create(component_dir.join("source").join(".ph")) {
            Ok(_) => (),
            Err(e) => {
                output.status = 21;
                output.stderr.push(format!(
                    "ERROR: Could not create placeholder file in source directory: {}",
                    e
                ));
            }
        };
    } else {
        output.stdout.push(String::from(
            "source directory already exists, using existing directory.",
        ));
    }

    // Generate the template readme file
    let file_output = generate_readme(&component_dir, &name);
    output = combine_sroutputs(output, file_output);

    // Generate bom_data.yaml
    let file_output = generate_bom(&component_dir, &name);
    output = combine_sroutputs(output, file_output);

    // Generate package.json, if needed
    let file_output = generate_package_json(&component_dir, &name, &source_license);
    output = combine_sroutputs(output, file_output);

    // Generate the .sr file that provides extra information about this component
    let file_output = generate_dot_file(&component_dir, &source_license, &doc_license);
    output = combine_sroutputs(output, file_output);

    // Make sure that our package.json file is updated with all the license info
    let amal_output = amalgamate_licenses(&component_dir);
    output = combine_sroutputs(output, amal_output);

    output
        .stdout
        .push(String::from("Finished setting up component."));

    output
}

/// Allows a user to set the username and password for a component's remote URL.
/// This can be a security risk on multi-user systems since the password is stored in plain text inside
/// the .git/config file. Users should be encouraged to use ssh instead of https to avoid this security issue.
pub fn remote_login(
    target_dir: &Path,
    url: Option<String>,
    username: Option<String>,
    password: Option<String>,
) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    let mut final_url = url.unwrap().to_owned();
    if final_url.contains("https") {
        // Format the https string properly to contain the username and password
        final_url = add_user_pass_to_https(final_url, username, password);
    }

    // Initialize as a repo only if needed
    if !target_dir.join(".git").exists() {
        // Initialize the git repository and set the remote URL to push to
        let git_output = git_sr::git_init(target_dir, &final_url);
        output = combine_sroutputs(output, git_output);
    } else {
        // Change/set the remote URL of the component
        let git_output = git_sr::git_set_remote_url(target_dir, &final_url);
        output = combine_sroutputs(output, git_output);
    }

    output
}

/// Uploads any changes to the project/component to a remote repository.
///
/// The remote repository at `url` must exist before trying to upload changes to it.
/// `target_dir` must be a valid Sliderule component directory.
/// `messages` should describe the changes that were made since the last upload.
///
/// # Examples
///
/// ```no_run
/// let temp_dir = std::env::temp_dir();
///
/// let output = sliderule::upload_component(
///     &temp_dir.join("newproject"),
///     String::from("Initial commit"),
///     String::from("https://repo.com/user/newproject"),
///     None,
///     None
/// );
/// ```
pub fn upload_component(
    target_dir: &Path,
    message: String,
    url: String,
    username: Option<String>,
    password: Option<String>,
) -> SROutput {
    // Make sure that our package.json file is updated with all the license info
    let mut output = amalgamate_licenses(&target_dir);

    // Initialize as a repo only if needed
    if !target_dir.join(".git").exists() {
        let mut final_url = url.to_owned();
        if final_url.contains("https") {
            // Format the https string properly to contain the username and password
            final_url = add_user_pass_to_https(final_url, username, password);
        }

        // Initialize the git repository and set the remote URL to push to
        let git_output = git_sr::git_init(target_dir, &final_url);
        output = combine_sroutputs(output, git_output);
    }

    // Create the gitignore file only if we need to
    if !target_dir.join(".gitignore").exists() {
        // Generate gitignore file so that we don't commit and push things we shouldn't be
        let file_output = generate_gitignore(&target_dir);
        output = combine_sroutputs(output, file_output);
    }

    // Add all changes, commit and push
    let git_output = git_sr::git_add_and_commit(target_dir, message);

    // Combine the outputs together
    output = combine_sroutputs(output, git_output);

    output
        .stdout
        .push(String::from("Done uploading component."));

    output
}

fn add_user_pass_to_https(
    url: String,
    username: Option<String>,
    password: Option<String>,
) -> String {
    let mut userpass = String::new();
    let mut final_url = String::new();

    // If we have a username and password, rework the URL to store them
    if username.is_some() && password.is_some() {
        userpass.push_str("https://");
        userpass.push_str(&username.unwrap());
        userpass.push_str(":");
        userpass.push_str(&password.unwrap());
        userpass.push_str("@");

        final_url = url.replace("https://", &userpass);
    }

    final_url
}

/// Converts a local component into a remote component, uploading it to the remote repo and then
/// installing via npm.
///
/// `target_dir` must be a valid Sliderule component directory.
/// `name` is the name of the component in the `components` directory to refactor.
/// `url` is the remote URL to push the component to. This URL must exist before this is called.
///
/// # Examples
///
/// ```no_run
/// let temp_dir = std::env::temp_dir();
///
/// let output = sliderule::refactor(
///     &temp_dir.join("newproject"),
///     String::from("level1_component"),
///     String::from("https://repo.com/user/level1_component"),
///     None,
///     None
/// );
/// ```
pub fn refactor(
    target_dir: &Path,
    name: String,
    url: String,
    username: Option<String>,
    password: Option<String>,
) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    let component_dir = target_dir.join("components").join(&name);

    let mut remote_url = String::new();
    if url.starts_with("git@") {
        remote_url.push_str("git+ssh://");
        remote_url.push_str(&url);
    } else {
        remote_url = url.to_owned();
    }

    if component_dir.exists() {
        // Upload the current component to the remote repo
        output = upload_component(
            &component_dir,
            String::from("Initial commit, refactoring component"),
            url.to_owned(),
            username,
            password,
        );

        // Remove the local component
        let remove_output = remove(&target_dir, &name);
        output = combine_sroutputs(output, remove_output);

        // Install the newly minted remote component using npm
        let add_output = add_remote_component(&target_dir, &remote_url, None);
        output = combine_sroutputs(output, add_output);

        // Shouldn't need it here, but make sure that our package.json file is updated with all the license info
        let amal_output = amalgamate_licenses(&target_dir);
        output = combine_sroutputs(output, amal_output);
    } else {
        output.status = 10;
        output.stderr.push(String::from(
            "ERROR: The component does not exist in the components directory.",
        ));
        return output;
    }

    output.stdout.push(String::from(
        "Finished refactoring local component to remote repository.",
    ));

    output
}

/// Removes a component (local or remote) from the project directory structure.
///
/// `target_dir` must be a valid Sliderule component directory.
/// `name` must be a valid name for a component in either the `components` or
/// the `node_modules` directories.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// // Remove a local component so we can test it
/// let output = sliderule::remove(&test_dir.join("toplevel"), "level1");
///
/// // Make sure that the level1 directory was removed
/// assert!(!&test_dir
///         .join("toplevel")
///         .join("components")
///         .join("level1")
///         .exists());
/// ```
pub fn remove(target_dir: &Path, name: &str) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    let component_dir = target_dir.join("components").join(name);

    // If the component exists as a subdirectory of components delete the directory directly otherwise use npm to remove it.
    if component_dir.exists() {
        output
            .stdout
            .push(format!("Deleting component directory {}.", name));

        // Step through every file and directory in the path to be deleted and make sure that none are read-only
        for entry in walkdir::WalkDir::new(&component_dir) {
            let entry = match entry {
                Ok(ent) => ent,
                Err(e) => {
                    output.status = 6;
                    output.stderr.push(format!(
                        "ERROR: Could not handle entry while walking components directory tree: {}",
                        e
                    ));
                    return output;
                }
            };

            // Remove read-only permissions on every entry
            let md = match entry.path().metadata() {
                Ok(m) => m,
                Err(e) => {
                    output.status = 7;
                    output.stderr.push(format!(
                        "ERROR: Could not get metadata for a .git directory entry: {}",
                        e
                    ));
                    return output;
                }
            };

            // Set the permissions on the directory to make sure that we can delete it when the time comes
            let mut perms = md.permissions();
            perms.set_readonly(false);
            match fs::set_permissions(&entry.path(), perms) {
                Ok(_) => (),
                Err(e) => {
                    output.status = 8;
                    output.stderr.push(format!(
                        "ERROR: Failed to set permissions on .git directory: {}",
                        e
                    ));
                    return output;
                }
            };
        }

        // Delete the directory recursively
        match fs::remove_dir_all(component_dir) {
            Ok(_) => (),
            Err(e) => {
                output.status = 9;
                output.stderr.push(format!(
                    "ERROR: not able to delete component directory: {}",
                    e
                ));
                return output;
            }
        };
    } else {
        output = remove_remote_component(&target_dir, name, None);
    }

    // Make sure that our package.json file is updated with all the license info
    let amal_output = amalgamate_licenses(&target_dir);

    // Roll the amalgamation output in with what we have already
    let mut output = combine_sroutputs(output, amal_output);

    // Let the caller know the component was removed successfully
    output
        .stdout
        .push(format!("Component {} was successfully removed.", name));

    output
}

/// Allows the user to change the source and/or documentation licenses for the project.
///
/// `target_dir` must be a valid Sliderule component directory.
/// `source_license` Must be an SPDX compliant string for the component's source files (mechanical/electrical CAD, etc)
/// `doc_license` Must be an SPDX compliant string for the documentation content of the component
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let output = sliderule::change_licenses(
///    &test_dir.join("toplevel"),
///    String::from("TestSourceLicense"),
///    String::from("TestDocLicense"),
///    );
///
/// assert_eq!(0, output.status);
/// assert!(output.stderr.is_empty());
/// let content = fs::read_to_string(test_dir.join("toplevel")
///    .join(".sr"))
///    .expect("Unable to read file");
///
/// assert!(content.contains("TestSourceLicense"));
/// assert!(content.contains("TestDocLicense"));
/// ```
pub fn change_licenses(target_dir: &Path, source_license: String, doc_license: String) -> SROutput {
    // Update the source and documentation licenses
    let output = update_yaml_value(&target_dir.join(".sr"), "source_license", &source_license);
    let secondary_output = update_yaml_value(
        &target_dir.join(".sr"),
        "documentation_license",
        &doc_license,
    );

    // Combine the outputs from the attempts to change the source and documentation licenses
    let output = combine_sroutputs(output, secondary_output);

    // Make sure our new licenses are up to date in package.json
    let amal_output = amalgamate_licenses(&target_dir);

    // Combine the previously combined output with the new output from the license amalgamation
    let output = combine_sroutputs(output, amal_output);

    output
}

/*
 *
*/
/// Adds a component from the remote repository at the provided URL to the node_modules directory.
///
/// `target_dir` must be a valid Sliderule component directory.
/// `url` URL of the repository the remote component resides in.
/// 'cache` Allows a user to specify a temporary cache for npm to use. Mostly for testing purposes.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
/// # let cache_dir = temp_dir.join(format!("cache_{}", uuid::Uuid::new_v4()));
///
/// let output = sliderule::add_remote_component(
///     &test_dir.join("toplevel"),
///     "https://github.com/jmwright/arduino-sr.git",
///     Some(cache_dir.to_string_lossy().to_string()),
/// );
///
/// assert_eq!(0, output.status);
///
/// let component_path = test_dir
///     .join("toplevel")
///     .join("node_modules")
///     .join("arduino-sr");
///
/// assert!(component_path.exists());
/// ```
pub fn add_remote_component(target_dir: &Path, url: &str, cache: Option<String>) -> SROutput {
    let mut output = npm_sr::npm_install(target_dir, &url, cache);

    // Make sure that our package.json file is updated with all the license info
    let amal_output = amalgamate_licenses(&target_dir);
    output = combine_sroutputs(output, amal_output);

    if output.status != 0 || output.wrapped_status != 0 {
        output.stderr.push(String::from(
            "ERROR: Remote component was not successfully added",
        ));
    }

    if output.status == 0 && output.wrapped_status == 0 {
        output
            .stdout
            .push(String::from("Remote component was added successfully."));
    }

    output
}

/// Removes a remote component via the name.
///
/// `target_dir` must be a valid Sliderule component directory.
/// `name` name of the component to remove. The node_modules directory is assumed, so name conflicts
/// with local components are ignored.
/// 'cache` Allows a user to specify a temporary cache for npm to use. Mostly for testing purposes.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
/// # let cache_dir = temp_dir.join(format!("cache_{}", uuid::Uuid::new_v4()));
///
/// let output = sliderule::remove_remote_component(
///            &test_dir.join("toplevel"),
///            "blink_firmware",
///            Some(cache_dir.to_string_lossy().to_string()),
///        );
///
/// assert_eq!(0, output.status);
///
/// assert!(!test_dir
///     .join("toplevel")
///     .join("node_modules")
///     .join("blink_firmware")
///     .exists());
/// ```
pub fn remove_remote_component(target_dir: &Path, name: &str, cache: Option<String>) -> SROutput {
    // Use npm to remove the remote component
    let mut output = npm_sr::npm_uninstall(target_dir, name, cache);

    if output.status != 0 || output.wrapped_status != 0 {
        output.stderr.push(String::from(
            "ERROR: Component was not successfully removed",
        ));
    }

    if output.status == 0 && output.wrapped_status == 0 {
        output
            .stdout
            .push(String::from("Component was removed successfully."));
    }

    output
}

/// Downloads a copy of a component from the remote repository at the specified URL.
///
/// `target_dir` must be a valid Sliderule component directory.
/// `url` URL of the remote repository to download the component from.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let output = sliderule::download_component(
///             &test_dir.join("toplevel"),
///             "https://github.com/jmwright/toplevel.git",
///         );
///
/// assert_eq!(0, output.status);
///
/// assert!(output.stdout[1].contains("Component was downloaded successfully."));
/// ```
pub fn download_component(target_dir: &Path, url: &str) -> SROutput {
    let mut output = git_sr::git_clone(target_dir, url);

    if output.status != 0 || output.wrapped_status != 0 {
        output.stderr.push(String::from(
            "ERROR: Component was not successfully downloaded",
        ));
    }

    if output.status == 0 && output.wrapped_status == 0 {
        output
            .stdout
            .push(String::from("Component was downloaded successfully."));
    }

    output
}

/// Updates all remote component in the node_modules directory.
///
/// `target_dir` must be a valid Sliderule component directory.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let output = sliderule::update_dependencies(&test_dir.join("toplevel"));
///
/// assert_eq!(0, output.status);
///
/// assert!(output.stdout[1].contains("Dependencies were updated successfully."));
/// ```
pub fn update_dependencies(target_dir: &Path) -> SROutput {
    let mut output = npm_sr::npm_install(target_dir, "", None);

    if output.status != 0 || output.wrapped_status != 0 {
        output.stderr.push(String::from(
            "ERROR: Dependencies were not successfully updated",
        ));
    }

    if output.status == 0 && output.wrapped_status == 0 {
        output
            .stdout
            .push(String::from("Dependencies were updated successfully."));
    }

    // Make sure that our package.json file is updated with all the license info
    let amal_output = amalgamate_licenses(&target_dir);
    output = combine_sroutputs(output, amal_output);

    output
}

/*
 * Updates the local component who's directory we're in
*/
/// Downloads updates from the remote repository that is set for this directory.
///
/// `target_dir` must be a valid Sliderule component directory.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let output = sliderule::update_local_component(&test_dir.join("toplevel"));
///
/// assert_eq!(0, output.status);
///
/// assert_eq!(output.stdout[0].trim(), "Already up to date.");
/// assert_eq!(output.stdout[1], "Component updated successfully.");
/// ```
pub fn update_local_component(target_dir: &Path) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    if target_dir.join(".git").exists() {
        output = git_sr::git_pull(target_dir);

        // Make sure that our package.json file is updated with all the license info
        let amal_output = amalgamate_licenses(&target_dir);
        output = combine_sroutputs(output, amal_output);

        // Give the user an idea of whether the update was successful or not
        if output.status == 0 {
            output
                .stdout
                .push(String::from("Component updated successfully."));
        } else {
            output
                .stdout
                .push(String::from("Component not updated successfully."));
        }
    } else {
        output.status = 1;
        output.stderr.push(String::from(
            "ERROR: Component is not set up as a repository, cannot update it.",
        ));
    }

    output
}

/// Prints out each of the licenses in the component's directory tree so that
/// users can see what licenses are in use and where they reside.
///
/// `target_dir` must be a valid Sliderule component directory.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let license_listing = sliderule::list_all_licenses(&test_dir.join("toplevel"));
///
/// assert!(license_listing.contains("Licenses Specified In This Component:"));
/// assert!(license_listing.contains("Unlicense"));
/// assert!(license_listing.contains("CC0-1.0"));
/// assert!(license_listing.contains("NotASourceLicense"));
/// assert!(license_listing.contains("NotADocLicense"));
/// assert!(license_listing.contains("CC-BY-4.0"));
/// ```
pub fn list_all_licenses(target_dir: &Path) -> String {
    let nl = get_newline();
    let mut license_listing = String::from("Licenses Specified In This Component:");
    license_listing.push_str(&nl);

    // Get the ordered listing of the component hierarchy
    let sr_entries = get_sr_paths(target_dir);

    // Compile the licenses of all the entries
    for entry in sr_entries {
        // We want the licenses from our current dot files
        let source_value = get_yaml_value(&entry, "source_license");
        let doc_value = get_yaml_value(&entry, "documentation_license");

        license_listing.push_str(&format!(
            "Path: {}, Source License: {}, Documentation License: {}{}",
            entry.display(),
            source_value,
            doc_value,
            nl
        ));
    }

    license_listing
}

/// Extracts the source and documentation licenses from a component's .sr file.
///
/// `target_dir` must be a valid Sliderule component directory.
///
/// # Examples
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let licenses = sliderule::get_licenses(&test_dir);
///
/// assert_eq!(licenses.0, "Unlicense");
/// assert_eq!(licenses.1, "CC0-1.0");
/// ```
pub fn get_licenses(target_dir: &Path) -> (String, String) {
    let sr_file: PathBuf;

    // We can hand back the default licenses, if nothing else
    let mut source_license = String::from("Unlicense");
    let mut doc_license = String::from("CC0-1.0");

    // If we're in a component directory, pull the license info from that
    sr_file = target_dir.join(".sr");

    // Safety check to make sure the file exists
    if sr_file.exists() {
        // Extract the licenses from the file
        source_license = get_yaml_value(&sr_file, "source_license");
        doc_license = get_yaml_value(&sr_file, "documentation_license");
    }

    (source_license, doc_license)
}

/// Figures out and returns what depth within another component's hierarchy
/// the component is at.
/// 0 = A top level component is probably being created
/// 1 = A top level component with no parent
/// 2 = A sub-component at depth n
///
/// `target_dir` must be a valid Sliderule component directory.
///
/// # Examples
///
/// ```
/// # use std::fs;
/// # let temp_dir = std::env::temp_dir();
/// # let url = "https://github.com/jmwright/toplevel.git";
/// # let uuid_dir = uuid::Uuid::new_v4();
/// # let test_dir_name = format!("temp_{}", uuid_dir);
/// # fs::create_dir(temp_dir.join(&test_dir_name)).expect("Unable to create temporary directory.");
/// # match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join("toplevel")) {
/// # Ok(repo) => repo,
/// # Err(e) => panic!("failed to clone: {}", e),
/// # };
/// # let test_dir = temp_dir.join(test_dir_name);
///
/// let level = sliderule::get_level(&test_dir.join("components").join("level1"));
///
/// assert_eq!(0, level)
/// ```
pub fn get_level(target_dir: &Path) -> u8 {
    let level: u8;

    // Allows us to check if there is a .sr file in the current directory
    let current_file = target_dir.join(".sr");

    // Allows us to check if there is a .sr file in the parent directory
    let parent_file = target_dir.join(".sr");

    // If the parent directory contains a .sr file, we have a sub-component, if not we have a top level component
    if !parent_file.exists() && !current_file.exists() {
        level = 0;
    } else if !parent_file.exists() && current_file.exists() {
        level = 1;
    } else {
        level = 2;
    }

    level
}

/*
 * Generates a template README.md file to help the user get started.
*/
fn generate_readme(target_dir: &Path, name: &str) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    if !target_dir.join("README.md").exists() {
        // Add the things that need to be put substituted into the README file
        let mut globals = liquid::value::Object::new();
        globals.insert("name".into(), liquid::value::Value::scalar(name.to_owned()));

        let contents = render_template("README.md.liquid", &mut globals);

        // Write the template text into the readme file
        match fs::write(target_dir.join("README.md"), contents) {
            Ok(_) => (),
            Err(e) => {
                output.status = 16;
                output
                    .stderr
                    .push(format!("Could not write to README.md file: {}", e));
            }
        };
    } else {
        output.stdout.push(String::from(
            "README.md already exists, using existing file and refusing to overwrite.",
        ));
    }

    output
}

/*
 * Generates a bill of materials from a template.
*/
fn generate_bom(target_dir: &Path, name: &str) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    if !target_dir.join("bom_data.yaml").exists() {
        // Add the things that need to be put substituted into the BoM file
        let mut globals = liquid::value::Object::new();
        globals.insert("name".into(), liquid::value::Value::scalar(name.to_owned()));

        let contents = render_template("bom_data.yaml.liquid", &mut globals);

        // Write the template text into the readme file
        match fs::write(target_dir.join("bom_data.yaml"), contents) {
            Ok(_) => (),
            Err(e) => {
                output.status = 17;
                output
                    .stderr
                    .push(format!("Could not write to bom_data.yaml: {}", e));
            }
        };
    } else {
        output.stdout.push(String::from(
            "bom_data.yaml already exists, using existing file and refusing to overwrite.",
        ));
    }

    output
}

/*
 * Generates a package.json file for npm based on a Liquid template.
*/
fn generate_package_json(target_dir: &Path, name: &str, license: &str) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    if !target_dir.join("package.json").exists() {
        // Add the things that need to be put substituted into the package file
        let mut globals = liquid::value::Object::new();
        globals.insert("name".into(), liquid::value::Value::scalar(name.to_owned()));
        globals.insert(
            "license".into(),
            liquid::value::Value::scalar(license.to_owned()),
        );

        let contents = render_template("package.json.liquid", &mut globals);

        // Write the contents into the file
        match fs::write(target_dir.join("package.json"), contents) {
            Ok(_) => (),
            Err(e) => {
                output.status = 18;
                output
                    .stderr
                    .push(format!("Could not write to package.json: {}", e));
            }
        };
    } else {
        output.stdout.push(String::from(
            "package.json already exists, using existing file and refusing to overwrite.",
        ));
    }

    output
}

/*
 * Generates the .gitignore file used by the git command to ignore files and directories.
*/
fn generate_gitignore(target_dir: &Path) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    if !target_dir.join(".gitignore").exists() {
        // Add the things that need to be put substituted into the gitignore file (none at this time)
        let mut globals = liquid::value::Object::new();

        let contents = render_template(".gitignore.liquid", &mut globals);

        // Write the contents to the file
        match fs::write(target_dir.join(".gitignore"), contents) {
            Ok(_) => (),
            Err(e) => {
                output.status = 19;
                output
                    .stderr
                    .push(format!("Could not write to .gitignore: {}", e));
            }
        };
    } else {
        output.stdout.push(String::from(
            ".gitignore already exists, using existing file and refusing to overwrite.",
        ));
    }

    output
}

/*
 * Generates the dot file that tracks whether this is a top level component/project or a sub-component
*/
fn generate_dot_file(target_dir: &Path, source_license: &str, doc_license: &str) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stderr: Vec::new(),
        stdout: Vec::new(),
    };

    if !target_dir.join(".sr").exists() {
        // Add the things that need to be put substituted into the .top file (none at this time)
        let mut globals = liquid::value::Object::new();
        globals.insert(
            "source_license".into(),
            liquid::value::Value::scalar(source_license.to_owned()),
        );
        globals.insert(
            "doc_license".into(),
            liquid::value::Value::scalar(doc_license.to_owned()),
        );

        let contents = render_template(".sr.liquid", &mut globals);

        // Write the contents to the file
        match fs::write(target_dir.join(".sr"), contents) {
            Ok(_) => (),
            Err(e) => {
                output.status = 20;
                output
                    .stderr
                    .push(format!("Could not write to .sr file: {}", e));
            }
        };
    } else {
        output.stdout.push(String::from(
            ".sr already exists, using existing file and refusing to overwrite.",
        ));
    }

    output
}

/*
 * Reads a template to a string so that it can be written to a new components directory structure.
*/
fn render_template(template_name: &str, globals: &mut liquid::value::Object) -> String {
    let mut contents = String::new();

    if template_name == ".sr.liquid" {
        contents = templates::sr_file_template();
    } else if template_name == ".gitignore.liquid" {
        contents = templates::gitignore_template();
    } else if template_name == "bom_data.yaml.liquid" {
        contents = templates::bom_data_yaml_template();
    } else if template_name == "package.json.liquid" {
        contents = templates::package_json_template();
    } else if template_name == "README.md.liquid" {
        contents = templates::readme_template();
    }

    // Render the output of the template using Liquid
    let template = liquid::ParserBuilder::with_liquid()
        .build()
        .parse(&contents)
        .expect("Could not parse template using Liquid.");

    let output = template
        .render(globals)
        .expect("Could not render template using Liquid.");

    output
}

/*
 * Walk the directory structure of the current component and combine the licenses per the SPDX naming conventions.
*/
fn amalgamate_licenses(target_dir: &Path) -> SROutput {
    let output = SROutput {
        status: 0,
        wrapped_status: 0,
        stdout: Vec::new(),
        stderr: Vec::new(),
    };

    let mut license_str = String::new();
    let mut source_licenses: Vec<String> = Vec::new();
    let mut doc_licenses: Vec<String> = Vec::new();

    // Get the ordered listing of the component hierarchy
    let sr_entries = get_sr_paths(target_dir);

    // Compile the licenses of all the entries
    for entry in sr_entries {
        // We want the licenses from our current dot files
        let source_value = get_yaml_value(&entry, "source_license");
        let doc_value = get_yaml_value(&entry, "documentation_license");

        // Keep track of the license strings, avoiding duplicates
        if !source_licenses.contains(&source_value) {
            source_licenses.push(source_value);
        }
        if !doc_licenses.contains(&doc_value) {
            doc_licenses.push(doc_value);
        }
    }

    // Make sure everything is enclosed in parentheses
    license_str.push_str("(");

    // Step through all of the source licenses and append them to the license string
    let mut i = 0;
    for lic in source_licenses {
        // Make sure that the list is AND-concatenated
        if i > 0 {
            license_str.push_str(" AND ")
        }

        license_str.push_str(&lic);

        i = i + 1;
    }

    // Make sure that there's an AND concatenation after the source license
    if doc_licenses.len() > 0 && i > 0 {
        license_str.push_str(" AND ");
    }

    // Step through all of the documentation licenses and append them to the license string
    let mut j = 0;
    for lic in doc_licenses {
        // Make sure that the list is AND-concatenated
        if j > 0 {
            license_str.push_str(" AND ");
        }

        license_str.push_str(&lic);

        j = j + 1;
    }

    // Make sure everything is enclosed in parentheses
    license_str.push_str(")");

    update_json_value(&target_dir.join("package.json"), "license", &license_str);

    output
}

// Yields all the paths to .sr files in the target component's directory structure
fn get_sr_paths(target_dir: &Path) -> Vec<PathBuf> {
    let mut sr_paths = Vec::new();

    let walker = globwalk::GlobWalkerBuilder::from_patterns(target_dir, &[".sr"])
        .max_depth(100)
        .follow_links(false)
        .sort_by(path_cmp)
        .build()
        .expect("Could not build globwalk directory walker.")
        .into_iter()
        .filter_map(Result::ok);

    for sr_file in walker {
        sr_paths.push(sr_file.path().to_path_buf());
    }

    sr_paths
}

// Hackey way of comparing two paths by comparing them as strings, but is the only cross-platform way
// that gives a reliable ordering of the paths.
fn path_cmp(a: &walkdir::DirEntry, b: &walkdir::DirEntry) -> Ordering {
    let order: Ordering;

    if a.to_owned().into_path().to_string_lossy() < b.to_owned().into_path().to_string_lossy() {
        order = Ordering::Less;
    } else {
        order = Ordering::Greater;
    }

    order
}

/*
 * Extracts a value from a JSON file based on a string key.
*/
fn get_json_value(json_file: &PathBuf, key: &str) -> String {
    let mut value = String::new();

    // If the file doesn't exist, we can't do anything
    if json_file.exists() {
        // Open the file for reading
        let mut file = fs::File::open(&json_file).expect("Error opening JSON file.");

        // Attempt to read the contents of the file
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("ERROR: Unable to read the JSON file for this component");

        let lines = contents.lines();
        for line in lines {
            // Make sure that we're extracting the proper license at the proper time
            if line.contains(&key) {
                let part: Vec<&str> = line.split(":").collect();
                value = part[1]
                    .replace("\"", "")
                    .replace(",", "")
                    .trim()
                    .to_string();
            }
        }
    } else {
        panic!(
            "JSON file {} not found, cannot extract data from it.",
            json_file.display()
        );
    }

    value
}

/*
 * Replaces the value corresponding to a key in a JSON file
*/
fn update_json_value(json_file: &PathBuf, key: &str, value: &str) {
    if json_file.exists() {
        // Open the file for reading
        let mut file = fs::File::open(&json_file).expect("Error opening JSON file.");

        // Attempt to read the contents of the component's .sr file
        let mut contents = String::new();
        let mut new_contents = String::new();
        file.read_to_string(&mut contents)
            .expect("ERROR: Unable to read the JSON file for this component");

        let lines = contents.lines();
        for line in lines {
            // Make sure that we're extracting the proper license at the proper time
            if line.contains(&key) {
                // Grab the original value
                let part: Vec<&str> = line.split(":").collect();
                let old_value = part[1]
                    .replace("\"", "")
                    .replace(",", "")
                    .trim()
                    .to_string();

                // Scope the change to matching line and replace the original line with the new one
                let new_line = line.replace(&old_value, &value);
                new_contents = contents.replace(line, &new_line);
            }
        }

        // Make sure there's a change to write
        if !new_contents.is_empty() {
            // Try to write the contents back to the file
            fs::write(json_file, new_contents).expect("Could not write to JSON file.");
        }
    }
}

/*
 * Extracts a value from a yaml file based on a string key.
*/
fn get_yaml_value(yaml_file: &PathBuf, key: &str) -> String {
    let mut value = String::new();

    // If the file doesn't exist, we can't do anything
    if yaml_file.exists() {
        // Open the file for reading
        let mut file = fs::File::open(&yaml_file).expect("Error opening yaml file.");

        // Attempt to read the contents of the file
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("ERROR: Unable to read the yaml file for this component");

        let lines = contents.lines();
        for line in lines {
            // Make sure that we're extracting the proper license at the proper time
            if line.contains(&key) {
                let part: Vec<&str> = line.split(":").collect();
                value = String::from(part[1].replace(",", "").trim());
            }
        }
    } else {
        panic!(
            "yaml file {} not found, cannot extract data from it.",
            yaml_file.display()
        );
    }

    value
}

/*
 * Replaces the value corresponding to a key in a yaml file
*/
fn update_yaml_value(yaml_file: &PathBuf, key: &str, value: &str) -> SROutput {
    let mut output = SROutput {
        status: 0,
        wrapped_status: 0,
        stdout: Vec::new(),
        stderr: Vec::new(),
    };

    // Make sure the file even exists
    if yaml_file.exists() {
        let mut new_contents = String::new();

        // Read the entire contents of the file into a string so we can parse the lines
        let contents = match fs::read_to_string(yaml_file) {
            Ok(cont) => cont,
            Err(e) => {
                output.status = 4;
                output.stderr.push(format!(
                    "ERROR: Could not update the contents of the YAML file: {}",
                    e
                ));
                return output;
            }
        };

        // Step through all the lines in the file
        for line in contents.lines() {
            // Make sure that we're extracting the proper license at the proper time
            if line.contains(&key) {
                // Grab the original value
                let part: Vec<&str> = line.split(":").collect();
                let old_value = String::from(part[1].replace(",", "").trim());

                // Scope the change to matching line and replace the original line with the new one
                let new_line = line.replace(&old_value, &value);
                new_contents = contents.replace(line, &new_line);
            }
        }

        // Make sure there's a change to write
        if !new_contents.is_empty() {
            // Try to write the contents back to the file
            match fs::write(yaml_file, new_contents) {
                Ok(_) => (),
                Err(e) => {
                    output.status = 5;
                    output
                        .stderr
                        .push(format!("ERROR: Could not write to the YAML file: {}", e));
                    return output;
                }
            }; //.expect("Could not write to yaml file.");
        }
    } else {
        output.status = 3;
        output.stderr.push(String::from(
            "ERROR: YAML file to be updated does not exist.",
        ));
    }

    output
}

/*
 * Gets the parent directory of the current component
*/
fn get_parent_dir(target_dir: &Path) -> PathBuf {
    // Get the parent directory of this component's directory
    let parent_dir = target_dir
        .parent()
        .expect("ERROR: Could not get the parent directory of the target component.");

    parent_dir.to_path_buf()
}

/*
 * Gets the line ending that's appropriate for the OS we are running on.
 */
fn get_newline() -> String {
    let info = os_info::get();

    if info.os_type() == os_info::Type::Windows {
        String::from("\r\n")
    } else {
        String::from("\n")
    }
}

/*
 * Convenience function to combine the contents of two SROutput objects into one
 */
fn combine_sroutputs(mut dest: SROutput, src: SROutput) -> SROutput {
    // Collect the stdout values into one
    for line in src.stdout {
        dest.stdout.push(line);
    }

    // Collect the stderr values into one
    for line in src.stderr {
        dest.stderr.push(line);
    }

    // Make sure that if there was an error condition, we catch at least one of them
    // Runs the risk of masking one of the errors.
    if dest.status == 0 && src.status != 0 {
        dest.status = src.status;
    }

    dest
}

pub mod git_sr;
pub mod npm_sr;
pub mod templates;

#[cfg(test)]
mod tests {
    use std::env;
    use std::ffi::OsStr;
    use std::fs;
    use std::path::{Component, Path};

    extern crate git2;
    extern crate uuid;
    use std::io::prelude::*;
    use std::path::PathBuf;
    use std::process::Command;

    /*
     * Tests whether or not we can accurately find the parent dir of a component dir
     */
    #[test]
    fn test_get_parent_dir() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        assert!(&test_dir.join("toplevel").exists());
        assert_eq!(super::get_parent_dir(&test_dir.join("toplevel")), test_dir);
    }

    /*
     * Tests whether we can get and set yaml file properties correctly
     */
    #[test]
    fn test_yaml_file_handling() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Read the source license from the sample directory
        let source_license =
            super::get_yaml_value(&test_dir.join("toplevel").join(".sr"), "source_license");
        assert_eq!(source_license, "Unlicense");

        // Change the source license from the sample directory
        super::update_yaml_value(
            &test_dir.join("toplevel").join(".sr"),
            "source_license",
            "NotASourceLicense",
        );

        // Make sure the source license changed
        let source_license =
            super::get_yaml_value(&test_dir.join("toplevel").join(".sr"), "source_license");
        assert_eq!(source_license, "NotASourceLicense");

        // Read a non-existent key from the sample directory
        let value = super::get_yaml_value(&test_dir.join("toplevel").join(".sr"), "not_a_key");
        assert_eq!(value, "");
    }

    /*
     * Tests whether we can get and set json file properties correctly
     */
    #[test]
    fn test_json_file_handling() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Read the component name from the package.json file
        let name = super::get_json_value(&test_dir.join("toplevel").join("package.json"), "name");
        assert_eq!(name, "toplevel");

        // Change the component name in the package.json file
        super::update_json_value(
            &test_dir.join("toplevel").join("package.json"),
            "name",
            "NotAName",
        );

        // Make sure the component name changed in package.json
        let name = super::get_json_value(&test_dir.join("toplevel").join("package.json"), "name");
        assert_eq!(name, "NotAName");

        // Read a non-existent key from package.json
        let name =
            super::get_json_value(&test_dir.join("toplevel").join("package.json"), "not_a_key");
        assert_eq!(name, "");
    }

    /*
     * Tests whether or not the licenses are collected into the license field of package.json correctly.
     */
    #[test]
    fn test_amalgamate_licenses() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Make sure the license field starts with something other than the string we are looking for
        super::update_json_value(
            &test_dir.join("toplevel").join("package.json"),
            "license",
            "NotALicense",
        );

        super::amalgamate_licenses(&test_dir.join("toplevel"));

        // Make sure that all of the licenses were outlined correctly
        let license =
            super::get_json_value(&test_dir.join("toplevel").join("package.json"), "license");

        assert_eq!(
            license,
            "(Unlicense AND NotASourceLicense AND CC0-1.0 AND NotADocLicense AND CC-BY-4.0)"
        );
    }

    #[test]
    fn test_get_licenses() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Make sure that we get the proper licenses back when requested
        let licenses = super::get_licenses(&test_dir);

        assert_eq!(licenses.0, "Unlicense");
        assert_eq!(licenses.1, "CC0-1.0");
    }

    #[test]
    fn test_list_all_licenses() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Make suer that we get a proper license listing when requested
        let license_listing = super::list_all_licenses(&test_dir.join("toplevel"));

        assert!(license_listing.contains("Licenses Specified In This Component:"));
        assert!(license_listing.contains("Unlicense"));
        assert!(license_listing.contains("CC0-1.0"));
        assert!(license_listing.contains("NotASourceLicense"));
        assert!(license_listing.contains("NotADocLicense"));
        assert!(license_listing.contains("CC-BY-4.0"));
    }

    #[test]
    fn test_gitignore_template() {
        let content = super::templates::gitignore_template();

        assert!(content.contains("# Dependency directories"));
        assert!(content.contains("node_modules/"));
        assert!(content.contains("# Distribution directory"));
        assert!(content.contains("dist/"));

        // Render the template and make sure we got what was expected
        let mut globals = liquid::value::Object::new();

        let render = super::render_template(".gitignore.liquid", &mut globals);

        assert!(render.contains("# Dependency directories"));
        assert!(render.contains("node_modules/"));
        assert!(render.contains("# Distribution directory"));
        assert!(render.contains("dist/"));
    }

    #[test]
    fn test_sr_file_template() {
        let content = super::templates::sr_file_template();

        assert!(content.contains("source_license: {{source_license}},"));
        assert!(content.contains("documentation_license: {{doc_license}}"));

        // Render the template and make sure we got was expected
        let mut globals = liquid::value::Object::new();
        globals.insert(
            "source_license".into(),
            liquid::value::Value::scalar("NotASourceLicense"),
        );
        globals.insert(
            "doc_license".into(),
            liquid::value::Value::scalar("NotADocLicense"),
        );

        let render = super::render_template(".sr.liquid", &mut globals);

        assert!(render.contains("source_license: NotASourceLicense,"));
        assert!(render.contains("documentation_license: NotADocLicense"));
    }

    #[test]
    fn test_bom_data_yaml_template() {
        let content = super::templates::bom_data_yaml_template();

        assert!(content.contains("# Bill of Materials Data for {{name}}"));
        assert!(content.contains("parts:"));
        assert!(content.contains("    - specific_component_variation"));
        assert!(content.contains("    notes: ''"));
        assert!(content.contains("order:"));
        assert!(content.contains("  -component_1"));

        // Render the template and make sure we got was expected
        let mut globals = liquid::value::Object::new();
        globals.insert("name".into(), liquid::value::Value::scalar("TopLevel"));

        let render = super::render_template("bom_data.yaml.liquid", &mut globals);

        assert!(render.contains("# Bill of Materials Data for TopLevel"));
        assert!(render.contains("parts:"));
        assert!(render.contains("    - specific_component_variation"));
        assert!(render.contains("    notes: ''"));
        assert!(render.contains("order:"));
        assert!(render.contains("  -component_1"));
    }

    #[test]
    fn test_package_json_template() {
        let content = super::templates::package_json_template();

        assert!(content.contains("  \"name\": \"{{name}}\","));
        assert!(content.contains("  \"license\": \"{{license}}\","));

        // Render the template and make sure we got was expected
        let mut globals = liquid::value::Object::new();
        globals.insert("name".into(), liquid::value::Value::scalar("TopLevel"));
        globals.insert(
            "license".into(),
            liquid::value::Value::scalar("(NotASourceLicense AND NotADocLicense)"),
        );

        let render = super::render_template("package.json.liquid", &mut globals);

        assert!(render.contains("  \"name\": \"TopLevel\","));
        assert!(render.contains("  \"license\": \"(NotASourceLicense AND NotADocLicense)\","));
    }

    #[test]
    fn test_readme_template() {
        let content = super::templates::readme_template();

        assert!(content.contains("# {{name}}"));
        assert!(content.contains("Developed in [Sliderule](http://sliderule.io) an implementation of the [Distributed OSHW Framework](http://dof.sliderule.io)."));

        // Render the template and make sure we got was expected
        let mut globals = liquid::value::Object::new();
        globals.insert("name".into(), liquid::value::Value::scalar("TopLevel"));

        let render = super::render_template("README.md.liquid", &mut globals);

        assert!(render.contains("# TopLevel"));
        assert!(render.contains("Developed in [Sliderule](http://sliderule.io) an implementation of the [Distributed OSHW Framework](http://dof.sliderule.io)."));
    }

    #[test]
    fn test_generate_dot_file() {
        let temp_dir = env::temp_dir();
        let uuid_dir = uuid::Uuid::new_v4();
        let test_dir_name = format!("temp_{}", uuid_dir);
        let temp_dir = temp_dir.join(test_dir_name);

        // Create the temporary directory we are going to be working with
        fs::create_dir(&temp_dir).expect("Could not create temporary directory for test.");

        super::generate_dot_file(&temp_dir, "NotASourceLicense", "NotADocLicense");

        let mut file = fs::File::open(&temp_dir.join(".sr")).expect("Unable to open the sr file");
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("Unable to read the sr file");

        assert!(contents.contains("source_license: NotASourceLicense,"));
        assert!(contents.contains("documentation_license: NotADocLicense"));
    }

    #[test]
    fn test_generate_gitignore() {
        let temp_dir = env::temp_dir();
        let uuid_dir = uuid::Uuid::new_v4();
        let test_dir_name = format!("temp_{}", uuid_dir);
        let temp_dir = temp_dir.join(test_dir_name);

        // Create the temporary directory we are going to be working with
        fs::create_dir(&temp_dir).expect("Could not create temporary directory for test.");

        super::generate_gitignore(&temp_dir);

        let mut file = fs::File::open(&temp_dir.join(".gitignore"))
            .expect("Unable to open the gitignore file");
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("Unable to read the gitignore file");

        assert!(contents.contains("node_modules/"));
        assert!(contents.contains("dist/"));
    }

    #[test]
    fn test_generate_package_json() {
        let temp_dir = env::temp_dir();
        let uuid_dir = uuid::Uuid::new_v4();
        let test_dir_name = format!("temp_{}", uuid_dir);
        let temp_dir = temp_dir.join(test_dir_name);

        // Create the temporary directory we are going to be working with
        fs::create_dir(&temp_dir).expect("Could not create temporary directory for test.");

        super::generate_package_json(&temp_dir, "TopLevel", "NotASourceLicense");

        let mut file = fs::File::open(&temp_dir.join("package.json"))
            .expect("Unable to open the package.json file");
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("Unable to read the package.json file");

        assert!(contents.contains("  \"name\": \"TopLevel\","));
        assert!(contents.contains("  \"license\": \"NotASourceLicense\","));
    }

    #[test]
    fn test_generate_bom() {
        let temp_dir = env::temp_dir();
        let uuid_dir = uuid::Uuid::new_v4();
        let test_dir_name = format!("temp_{}", uuid_dir);
        let temp_dir = temp_dir.join(test_dir_name);

        // Create the temporary directory we are going to be working with
        fs::create_dir(&temp_dir).expect("Could not create temporary directory for test.");

        super::generate_bom(&temp_dir, "TopLevel");

        let mut file = fs::File::open(&temp_dir.join("bom_data.yaml"))
            .expect("Unable to open the bom_data.yaml file");
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("Unable to read the package.json file");

        assert!(contents.contains("# Bill of Materials Data for TopLevel"));
    }

    #[test]
    fn test_generate_readme() {
        let temp_dir = env::temp_dir();
        let uuid_dir = uuid::Uuid::new_v4();
        let test_dir_name = format!("temp_{}", uuid_dir);
        let temp_dir = temp_dir.join(test_dir_name);

        // Create the temporary directory we are going to be working with
        fs::create_dir(&temp_dir).expect("Could not create temporary directory for test.");

        super::generate_readme(&temp_dir, "TopLevel");

        let mut file =
            fs::File::open(&temp_dir.join("README.md")).expect("Unable to open the README.md file");
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .expect("Unable to read the package.json file");

        assert!(contents.contains("# TopLevel"));
    }

    #[test]
    fn test_update_local_component() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let output = super::update_local_component(&test_dir.join("toplevel"));

        // We should not have gotten an error
        assert_eq!(0, output.status);

        assert_eq!(output.stdout[0].trim(), "Already up to date.");
        assert_eq!(output.stdout[1], "Component updated successfully.");
    }

    #[test]
    fn test_update_dependencies() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let output = super::update_dependencies(&test_dir.join("toplevel"));

        // We should not have gotten an error
        assert_eq!(0, output.status);

        assert!(output.stdout[1].contains("Dependencies were updated successfully."));
    }

    #[test]
    fn test_download_component() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let output = super::download_component(
            &test_dir.join("toplevel"),
            "https://github.com/jmwright/toplevel.git",
        );

        // We should not have gotten an error
        assert_eq!(0, output.status);

        assert!(output.stdout[1].contains("Component was downloaded successfully."));
    }

    #[test]
    fn test_remove_remote_component() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Set up a cache directory to keep the system npm cache from getting messed up by the tests
        let cache_dir = temp_dir.join(format!("cache_{}", uuid::Uuid::new_v4()));

        let output = super::remove_remote_component(
            &test_dir.join("toplevel"),
            "blink_firmware",
            Some(cache_dir.to_string_lossy().to_string()),
        );

        // We should not have gotten an error
        assert_eq!(0, output.status);

        assert!(!test_dir
            .join("toplevel")
            .join("node_modules")
            .join("blink_firmware")
            .exists());
    }

    #[test]
    fn test_add_remote_component() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Set up a cache directory to keep the system npm cache from getting messed up by the tests
        let cache_dir = temp_dir.join(format!("cache_{}", uuid::Uuid::new_v4()));

        let output = super::add_remote_component(
            &test_dir.join("toplevel"),
            "https://github.com/jmwright/arduino-sr.git",
            Some(cache_dir.to_string_lossy().to_string()),
        );

        let component_path = test_dir
            .join("toplevel")
            .join("node_modules")
            .join("arduino-sr");

        // We should not have gotten an error
        assert_eq!(0, output.status);

        // The arduino-sr directory should exist
        assert!(component_path.exists());

        // The arduino-sr directory should be a valid component
        assert!(is_valid_component(
            &component_path,
            "arduino-sr",
            "Unlicense",
            "CC0-1.0"
        ));
    }

    #[test]
    fn test_change_licenses() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let output = super::change_licenses(
            &test_dir.join("toplevel"),
            String::from("TestSourceLicense"),
            String::from("TestDocLicense"),
        );

        // We should not have gotten an error
        assert_eq!(0, output.status);
        assert!(output.stderr.is_empty());

        // Make sure that the package.json file license was changed
        assert!(file_contains_content(
            &test_dir.join("toplevel").join("package.json"),
            9999,
            "TestSourceLicense",
        ));
        assert!(file_contains_content(
            &test_dir.join("toplevel").join("package.json"),
            9999,
            "TestDocLicense",
        ));
        // Check to make sure the licenses were actually changed
        assert!(file_contains_content(
            &test_dir.join("toplevel").join(".sr"),
            9999,
            "source_license: TestSourceLicense,"
        ));
        assert!(file_contains_content(
            &test_dir.join("toplevel").join(".sr"),
            9999,
            "documentation_license: TestDocLicense"
        ));
    }

    #[test]
    fn test_remove() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Remove a local component so we can test it
        let output = super::remove(&test_dir.join("toplevel"), "level1");

        // We should not have gotten an error
        assert_eq!(0, output.status);
        assert!(output.stderr.is_empty());

        // Make sure that the level1 directory was removed
        assert!(!&test_dir
            .join("toplevel")
            .join("components")
            .join("level1")
            .exists());

        // Remove a remote component so we can test it
        let output = super::remove(&test_dir.join("toplevel"), "blink_firmware");

        // We should not have gotten an error
        assert_eq!(0, output.status);

        // Make sure that the level1 directory was removed
        assert!(!&test_dir
            .join("toplevel")
            .join("node_modules")
            .join("level1")
            .exists());
    }

    #[test]
    fn test_create_component() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        // Generate a new component
        let output = super::create_component(
            &test_dir,
            String::from("nextlevel"),
            String::from("TestSourceLicense"),
            String::from("TestDocLicense"),
        );

        // We should not have gotten an error
        assert_eq!(0, output.status);

        // We should have gotten a message that the component was finished being set up
        assert_eq!(
            "Finished setting up component.",
            output.stdout[output.stdout.len() - 1]
        );

        // We should have a valid component when all is said and done
        assert!(is_valid_component(
            &test_dir.join("nextlevel"),
            "nextlevel",
            "TestSourceLicense",
            "TestDocLicense"
        ));
    }

    #[test]
    fn test_refactor() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let demo_dir = test_dir.join("demo");
        let remote_dir = demo_dir.join("remote");

        // Create the demo directory
        fs::create_dir(&demo_dir).expect("Failed to create demo directory.");

        Command::new("git")
            .args(&["init", "--bare"])
            .current_dir(&demo_dir)
            .output()
            .expect("failed to initialize bare git repository in demo directory");

        // Create the remote directory for the nextlevel project
        fs::create_dir(&remote_dir).expect("Failed to create top component directory.");

        Command::new("git")
            .args(&["init", "--bare"])
            .current_dir(&remote_dir)
            .output()
            .expect("failed to initialize bare git repository in demo directory");

        // Start a new git daemon server in the current remote repository
        Command::new("git")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .args(&[
                "daemon",
                "--reuseaddr",
                "--export-all",
                "--base-path=.",
                "--verbose",
                "--enable=receive-pack",
                ".",
            ])
            .current_dir(demo_dir)
            .spawn()
            .expect("ERROR: Could not launch git daemon.");

        // Generate a new component
        let output = super::create_component(
            &test_dir.join("toplevel"),
            String::from("remote"),
            String::from("TestSourceLicense"),
            String::from("TestDocLicense"),
        );

        // Make sure the new directory exists and is a valid component
        assert!(is_valid_component(
            &test_dir.join("toplevel").join("components").join("remote"),
            "remote",
            "TestSourceLicense",
            "TestDocLicense"
        ));

        // Make sure we did not get any errors
        assert_eq!(0, output.stderr.len());

        let output = super::refactor(
            &test_dir.join("toplevel"),
            String::from("remote"),
            String::from("git://127.0.0.1/remote"),
            None,
            None,
        );

        if output.stderr.len() > 0 {
            for out in &output.stderr {
                println!("{:?}", out);
            }
        }

        assert_eq!(
            "Finished refactoring local component to remote repository.",
            output.stdout[output.stdout.len() - 1]
        );

        // Make sure the component was reinstalled in the node_modules directory
        assert!(is_valid_component(
            &test_dir
                .join("toplevel")
                .join("node_modules")
                .join("remote"),
            "remote",
            "TestSourceLicense",
            "TestDocLicense"
        ));

        // Make sure there are no git processes left around after we're done
        kill_git();
    }

    #[test]
    fn test_upload_component() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let demo_dir = test_dir.join("demo");
        let remote_dir = demo_dir.join("nextlevel");

        // Create the demo directory
        fs::create_dir(&demo_dir).expect("Failed to create demo directory.");

        Command::new("git")
            .args(&["init", "--bare"])
            .current_dir(&demo_dir)
            .output()
            .expect("failed to initialize bare git repository in demo directory");

        // Create the remote directory for the nextlevel project
        fs::create_dir(&remote_dir).expect("Failed to create top component directory.");

        Command::new("git")
            .args(&["init", "--bare"])
            .current_dir(&remote_dir)
            .output()
            .expect("failed to initialize bare git repository in demo directory");

        // Start a new git daemon server in the current remote repository
        Command::new("git")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .args(&[
                "daemon",
                "--reuseaddr",
                "--export-all",
                "--base-path=.",
                "--verbose",
                "--enable=receive-pack",
                ".",
            ])
            .current_dir(demo_dir)
            .spawn()
            .expect("ERROR: Could not launch git daemon.");

        // Generate a new component
        let output = super::create_component(
            &test_dir,
            String::from("nextlevel"),
            String::from("TestSourceLicense"),
            String::from("TestDocLicense"),
        );

        // Make sure we did not get any errors
        assert_eq!(0, output.stderr.len());

        let output = super::upload_component(
            &test_dir.join("nextlevel"),
            String::from("Initial commit"),
            String::from("git://127.0.0.1/nextlevel"),
            None,
            None,
        );

        if output.stderr.len() > 0 {
            for out in &output.stderr {
                println!("{:?}", out);
            }
        }

        assert_eq!(
            "Done uploading component.",
            output.stdout[output.stdout.len() - 1]
        );
        assert_eq!(
            "Changes pushed using git.",
            output.stdout[output.stdout.len() - 2]
        );

        // To test properly, we have to re-download the component and check if it's valid
        let output = super::download_component(
            &test_dir.join("toplevel"),
            &String::from("git://127.0.0.1/nextlevel"),
        );

        if output.stderr.len() > 0 {
            for out in &output.stderr {
                println!("{:?}", out);
            }
        }

        assert!(is_valid_component(
            &test_dir.join("toplevel").join("nextlevel"),
            "nextlevel",
            "TestSourceLicense",
            "TestDocLicense"
        ));

        // Make sure there are no git processes left around after we're done
        kill_git();
    }

    #[test]
    fn test_get_sr_paths() {
        let temp_dir = env::temp_dir();

        // Set up our temporary project directory for testing
        let test_dir = set_up(&temp_dir, "toplevel");

        let sr_paths = super::get_sr_paths(&test_dir.join("toplevel"));

        // This is in here to help us troubleshoot if this test fails on one of the CI OSes
        for sr_path in &sr_paths {
            println!("{:?}", sr_path);
        }

        let path_parts = sr_paths[0].components().collect::<Vec<_>>();
        assert_eq!(
            path_parts[path_parts.len() - 1],
            Component::Normal(OsStr::new(".sr"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 2],
            Component::Normal(OsStr::new("toplevel"))
        );

        let path_parts = sr_paths[1].components().collect::<Vec<_>>();
        assert_eq!(
            path_parts[path_parts.len() - 1],
            Component::Normal(OsStr::new(".sr"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 2],
            Component::Normal(OsStr::new("level1"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 3],
            Component::Normal(OsStr::new("components"))
        );

        let path_parts = sr_paths[2].components().collect::<Vec<_>>();
        assert_eq!(
            path_parts[path_parts.len() - 1],
            Component::Normal(OsStr::new(".sr"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 2],
            Component::Normal(OsStr::new("level2"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 3],
            Component::Normal(OsStr::new("components"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 4],
            Component::Normal(OsStr::new("level1"))
        );

        let path_parts = sr_paths[3].components().collect::<Vec<_>>();
        assert_eq!(
            path_parts[path_parts.len() - 1],
            Component::Normal(OsStr::new(".sr"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 2],
            Component::Normal(OsStr::new("level3"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 3],
            Component::Normal(OsStr::new("components"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 4],
            Component::Normal(OsStr::new("level2"))
        );

        let path_parts = sr_paths[4].components().collect::<Vec<_>>();
        assert_eq!(
            path_parts[path_parts.len() - 1],
            Component::Normal(OsStr::new(".sr"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 2],
            Component::Normal(OsStr::new("blink_firmware"))
        );
        assert_eq!(
            path_parts[path_parts.len() - 3],
            Component::Normal(OsStr::new("node_modules"))
        );
    }

    // Cleans up the git daemon processes after tests run
    fn kill_git() {
        let info = os_info::get();

        if info.os_type() == os_info::Type::Windows {
            //taskkill /f /t /im wwahost.exe
            Command::new("taskkill")
                .args(&["/f", "/t", "/im", "git"])
                .output()
                .unwrap();

            Command::new("taskkill")
                .args(&["/f", "/t", "/im", "git-daemon"])
                .output()
                .unwrap();
            Command::new("taskkill")
                .args(&["/f", "/t", "/im", "git.exe"])
                .output()
                .unwrap();

            Command::new("taskkill")
                .args(&["/f", "/t", "/im", "git-daemon.exe"])
                .output()
                .unwrap();
        } else {
            Command::new("killall")
                .args(&["git"])
                .output()
                .expect("failed to kill git process");
        }
    }

    /*
     * Sets up a test directory for our use.
     */
    fn set_up(temp_dir: &PathBuf, dir_name: &str) -> PathBuf {
        let url = "https://github.com/jmwright/toplevel.git";

        let uuid_dir = uuid::Uuid::new_v4();
        let test_dir_name = format!("temp_{}", uuid_dir);

        // Create the temporary test directory
        fs::create_dir(temp_dir.join(&test_dir_name))
            .expect("Unable to create temporary directory.");

        match git2::Repository::clone(&url, temp_dir.join(&test_dir_name).join(dir_name)) {
            Ok(repo) => repo,
            Err(e) => panic!("failed to clone: {}", e),
        };

        temp_dir.join(test_dir_name)
    }

    /*
     * Tests if a directory has the correct contents to be a component.
     */
    fn is_valid_component(
        component_path: &Path,
        component_name: &str,
        source_license: &str,
        doc_license: &str,
    ) -> bool {
        let mut is_valid = true;

        // Make sure that the component path exists
        if !component_path.exists() {
            is_valid = false;
            println!(
                "The component directory {:?} does not exist",
                component_path
            );
        } else {
            let paths = fs::read_dir(component_path).unwrap();

            for path in paths {
                println!("Component path: {}", path.unwrap().path().display());
            }
        }

        // Make sure the BoM data file exists
        if !component_path.join("bom_data.yaml").exists() {
            is_valid = false;
            println!(
                "The file {:?}/bom_data.yaml does not exist.",
                component_path
            );
        }

        // Make sure the component directory exists
        if !component_path.join("components").exists() {
            is_valid = false;
            println!(
                "The directory {:?}/components does not exist.",
                component_path
            );
        }

        // Make sure the docs directory exists
        if !component_path.join("docs").exists() {
            is_valid = false;
            println!("The directory {:?}/docs does not exist.", component_path);
        }

        // Make sure the package.json file exists
        if !component_path.join("package.json").exists() {
            is_valid = false;
            println!("The file {:?}/package.json does not exist.", component_path);
        }

        // Make sure the README.md file exists
        if !component_path.join("README.md").exists() {
            is_valid = false;
            println!("The file {:?}/README.md does not exist.", component_path);
        }

        // Make sure the source directory exists
        if !component_path.join("source").exists() {
            is_valid = false;
            println!("The directory {:?}/source does not exist.", component_path);
        }

        let bom_file = component_path.join("bom_data.yaml");
        let package_file = component_path.join("package.json");
        let readme_file = component_path.join("README.md");
        let dot_file = component_path.join(".sr");

        // Check the content of the files and directories as appropriate here
        if !file_contains_content(
            &bom_file,
            0,
            &format!("# Bill of Materials Data for {}", component_name),
        ) {
            is_valid = false;
            println!(
                "The bill to materials file in {:?} does not contain the correct header.",
                component_path
            );
        }
        if !file_contains_content(&bom_file, 12, "-component_1") {
            is_valid = false;
            println!("The bill to materials file in {:?} does not contain the '-component_1' entry in the right place.", component_path);
        }
        if !file_contains_content(
            &package_file,
            9999,
            &format!("\"name\": \"{}\",", component_name),
        ) {
            is_valid = false;
            println!("The package.json file in {:?} does not contain the component name entry in the right place.", component_path);
        }
        if !file_contains_content(
            &package_file,
            9999,
            &format!("\"license\": \"({} AND {})\",", source_license, doc_license),
        ) {
            is_valid = false;
            println!("The package.json file in {:?} does not contain the the correct license entry in the right place.", component_path);
        }
        if !file_contains_content(&readme_file, 0, &format!("# {}", component_name)) {
            is_valid = false;
            println!("The README.md file in {:?} does not contain the the correct header entry in the right place.", component_path);
        }
        if !file_contains_content(&readme_file, 1, "New Sliderule component.") {
            is_valid = false;
            println!("The README.md file in {:?} does not contain the the correct Sliderule mention in the right place.", component_path);
        }
        if !file_contains_content(
            &dot_file,
            0,
            &format!("source_license: {},", source_license),
        ) {
            is_valid = false;
            println!(
                "The .sr file in {:?} does not contain the the correct source license in the right place.",
                component_path
            );
        }
        if !file_contains_content(
            &dot_file,
            1,
            &format!("documentation_license: {}", doc_license),
        ) {
            is_valid = false;
            println!("The .sr file in {:?} does not contain the the correct documentation license in the right place.", component_path);
        }

        is_valid
    }

    /*
     * Helper function that checks to make sure that given text is present in the files.
     */
    fn file_contains_content(file_path: &Path, line: usize, text: &str) -> bool {
        let contains_content: bool;

        // Read the contents of the file
        let contents =
            fs::read_to_string(file_path).expect("ERROR: Cannot read the contents of the file.");

        // See if the user just wants to make sure the content is somewhere in the file
        if line == 9999 {
            contains_content = contents.contains(text);
        } else {
            // Break the file down into something we can index
            let contents: Vec<&str> = contents.lines().collect();

            // See if the line we are interested in is exactly the content specified
            contains_content = contents[line].trim() == text;
        }

        contains_content
    }
}