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
// Copyright 2021 Oxide Computer Company

//! APIs for interacting with the Solaris service management facility.

#![deny(missing_docs)]

use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::str::FromStr;
use std::string::ToString;
use thiserror::Error;

/// The error type for parsing a bad status code while reading stdout.
#[derive(Error, Debug)]
#[error("{0}")]
pub struct CommandOutputError(String);

const PFEXEC: &str = "/usr/bin/pfexec";
const SVCPROP: &str = "/usr/bin/svcprop";
const SVCS: &str = "/usr/bin/svcs";
const SVCCFG: &str = "/usr/sbin/svccfg";
const SVCADM: &str = "/usr/sbin/svcadm";

trait OutputExt {
    fn read_stdout(&self) -> Result<String, CommandOutputError>;
}

impl OutputExt for std::process::Output {
    fn read_stdout(&self) -> Result<String, CommandOutputError> {
        let stdout = String::from_utf8_lossy(&self.stdout).trim().to_string();
        let stderr = String::from_utf8_lossy(&self.stderr).trim().to_string();

        if !self.status.success() {
            let exit_code = self
                .status
                .code()
                .map(|code| format!("{}", code))
                .unwrap_or_else(|| "<No exit code>".to_string());
            return Err(CommandOutputError(format!(
                "exit code {}\nstdout:\n{}\nstderr:\n{}",
                exit_code, stdout, stderr
            )));
        }
        Ok(stdout)
    }
}

/// Describes the state of a service.
///
/// For more information, refer to the "States" section of the smf man page.
#[derive(Clone, Debug, PartialEq)]
pub enum SmfState {
    /// The instance is disabled, must be explicitly enabled
    /// to later turn on.
    Disabled,
    /// The instance is enabled and running (or available to run).
    /// However, it is operating in limited capacity.
    Degraded,
    /// The instance is enabled, but not running. Administrative
    /// action (via the [AdmClear] / `svcadm clear` command) is
    /// required to restore the instance to an online state.
    Maintenance,
    /// The instance is enabled, but not yet running or able to run.
    Offline,
    /// The instance is enabled and running.
    Online,
    /// The instants represents a legacy service not managed by SMF.
    Legacy,
    /// The initial state for all service instances. The instance
    /// will be moved to another state by the restarter.
    Uninitialized,
}

impl SmfState {
    fn from_str(val: &str) -> Option<SmfState> {
        match val {
            "ON" => Some(SmfState::Online),
            "OFF" => Some(SmfState::Offline),
            "DGD" => Some(SmfState::Degraded),
            "DIS" => Some(SmfState::Disabled),
            "MNT" => Some(SmfState::Maintenance),
            "UN" => Some(SmfState::Uninitialized),
            "LRC" => Some(SmfState::Legacy),
            _ => None,
        }
    }
}

impl ToString for SmfState {
    fn to_string(&self) -> String {
        match self {
            SmfState::Disabled => "DIS",
            SmfState::Degraded => "DGD",
            SmfState::Maintenance => "MNT",
            SmfState::Offline => "OFF",
            SmfState::Online => "ON",
            SmfState::Legacy => "LRC",
            SmfState::Uninitialized => "UN",
        }
        .to_string()
    }
}

/*
 *
 * SVCS
 *
 */

/// The error code for any operation that fails during a query command.
#[derive(Error, Debug)]
pub enum QueryError {
    /// Failure to parse the output of a query command.
    #[error("Failed to parse output: {0}")]
    Parse(String),

    /// Failure to execute a subcommand.
    #[error("Failed to execute command: {0}")]
    Command(std::io::Error),

    /// Failure reading a command's stdout (or non-zero error code).
    #[error("Failed to parse command output: {0}")]
    CommandOutput(#[from] CommandOutputError),
}

/// Describes the status of an SMF service.
///
/// Refer to [Query] for information acquiring these structures.
#[derive(Clone, Debug, PartialEq)]
pub struct SvcStatus {
    /// The FMRI of a service (fault management resource identifier).
    /// Functionally acts as a service ID.
    pub fmri: String,
    /// The primary contract ID for the service instance.
    pub contract_id: Option<usize>,
    /// The instance name of the service instance.
    pub instance_name: String,
    /// The abbreviated name of the next state.
    /// If this field is `None`, the service is not changing states.
    pub next_state: Option<SmfState>,
    /// The scope name of the service instance.
    pub scope_name: String,
    /// The service name of the service instance.
    pub service_name: String,
    /// The service instance state.
    pub state: SmfState,
    /// The time the service transitioned to the current state.
    pub service_time: String,
    /// The zone in which the service exists.
    pub zone: String,
    /// A brief service description.
    pub description: Option<String>,
}

impl FromStr for SvcStatus {
    type Err = QueryError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split_whitespace();

        let status = || -> Result<SvcStatus, String> {
            let fmri = iter.next().ok_or("Missing FMRI")?.to_string();
            let contract_id = iter
                .next()
                .map::<Result<_, String>, _>(|s| match s {
                    "-" => Ok(None),
                    _ => Ok(Some(s.parse::<usize>().map_err(|e| e.to_string())?)),
                })
                .ok_or("Missing ContractID")??;
            let instance_name = iter.next().ok_or("Missing Instance Name")?.to_string();
            let next_state = SmfState::from_str(iter.next().ok_or("Missing Instance Name")?);
            let scope_name = iter.next().ok_or("Missing Scope Name")?.to_string();
            let service_name = iter.next().ok_or("Missing Service Name")?.to_string();
            let state =
                SmfState::from_str(iter.next().ok_or("Missing State")?).ok_or("Missing State")?;
            let service_time = iter.next().ok_or("Missing Service Time")?.to_string();
            let zone = iter.next().ok_or("Missing Zone")?.to_string();
            let description = iter
                .map(|s| s.to_owned())
                .collect::<Vec<String>>()
                .join(" ");
            let description = {
                if description == "-" || description.is_empty() {
                    None
                } else {
                    Some(description)
                }
            };
            Ok(SvcStatus {
                fmri,
                contract_id,
                instance_name,
                next_state,
                scope_name,
                service_name,
                state,
                service_time,
                zone,
                description,
            })
        }()
        .map_err(QueryError::Parse)?;

        Ok(status)
    }
}

#[derive(Copy, Clone)]
enum SvcColumn {
    Fmri,
    ContractId,
    InstanceName,
    NextState,
    ScopeName,
    ServiceName,
    State,
    ServiceTime,
    Zone,
    Description,
}

impl SvcColumn {
    fn to_str(&self) -> &str {
        match self {
            SvcColumn::Fmri => "FMRI",
            SvcColumn::ContractId => "CTID",
            SvcColumn::InstanceName => "INST",
            SvcColumn::NextState => "NSTA",
            SvcColumn::ScopeName => "SCOPE",
            SvcColumn::ServiceName => "SVC",
            SvcColumn::State => "STA",
            SvcColumn::ServiceTime => "STIME",
            SvcColumn::Zone => "ZONE",
            SvcColumn::Description => "DESC",
        }
    }
}

/// Determines which services are returned from [Query::get_status]
pub enum QuerySelection<S = String, I = Vec<String>>
where
    S: AsRef<str>,
    I: IntoIterator<Item = S>,
{
    /// All services instances.
    All,
    /// All service instances which have the provided service instance as their
    /// restarter.
    ByRestarter(S),
    /// All service instance which match the provided strings as either an FMRI
    /// or pattern (globs allowed) matching FMRIs.
    ByPattern(I),
}

/// Queries the underlying system to return [SvcStatus] structures.
///
/// Acts as a wrapper around the underlying 'svcs' command.
pub struct Query {
    zone: Option<String>,
}

impl Default for Query {
    fn default() -> Self {
        Self::new()
    }
}

impl Query {
    /// Creates a new query object.
    pub fn new() -> Query {
        Query { zone: None }
    }

    /// Requests a query be issued within a specific zone.
    pub fn zone<S: AsRef<str>>(&mut self, zone: S) -> &mut Query {
        self.zone.replace(zone.as_ref().into());
        self
    }

    fn add_zone_to_args(&self, args: &mut Vec<String>) {
        // TODO-feature: Add support for "-Z", all zones.
        if let Some(zone) = &self.zone {
            args.push("-z".to_string());
            args.push(zone.to_string());
        }
    }

    fn add_columns_to_args(&self, args: &mut Vec<String>) {
        // Provide query parameters
        args.push("-Ho".to_string());
        args.push(
            [
                SvcColumn::Fmri,
                SvcColumn::ContractId,
                SvcColumn::InstanceName,
                SvcColumn::NextState,
                SvcColumn::ScopeName,
                SvcColumn::ServiceName,
                SvcColumn::State,
                SvcColumn::ServiceTime,
                SvcColumn::Zone,
                SvcColumn::Description,
            ]
            .iter()
            .map(|col| col.to_str())
            .collect::<Vec<&str>>()
            .join(","),
        );
    }

    // Issues command, returns stdout.
    fn run(&self, args: Vec<String>) -> Result<String, QueryError> {
        Ok(std::process::Command::new(PFEXEC)
            .env_clear()
            .arg(SVCS)
            .args(args)
            .output()
            .map_err(QueryError::Command)?
            .read_stdout()?)
    }

    fn run_and_parse_output(
        &self,
        args: Vec<String>,
    ) -> Result<impl Iterator<Item = SvcStatus>, QueryError> {
        Ok(self
            .run(args)?
            .split('\n')
            .map(|s| s.parse::<SvcStatus>())
            .collect::<Result<Vec<SvcStatus>, _>>()?
            .into_iter())
    }

    // TODO: Probably should be able to fail.
    // TODO: Check that patterns != flags
    fn add_patterns<S, I>(&self, args: &mut Vec<String>, patterns: I)
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        args.append(
            &mut patterns
                .into_iter()
                .map(|p| p.as_ref().to_owned())
                .collect(),
        );
    }

    /// Syntactic sugar for [Query::get_status] with a selection of
    /// [QuerySelection::All].
    ///
    /// See: [The corresponding Rust issue on inferred default
    /// types](https://github.com/rust-lang/rust/issues/27336) for more context
    /// on why this method exists - it is more ergonomic than invoking
    /// [Self::get_status] with [QuerySelection::All] directly.
    pub fn get_status_all(&self) -> Result<impl Iterator<Item = SvcStatus>, QueryError> {
        // The `QuerySelection::All` variant of the enum doesn't actually use the
        // type parameters at all, so it doesn't care what types are supplied as
        // parameters.
        //
        // Rather than forcing the client of this library to deal with this
        // quirk, this helper provides reasonable default.
        self.get_status(QuerySelection::<String, Vec<String>>::All)
    }

    /// Queries for status information from the corresponding query.
    ///
    /// Returns status information for all services which match the
    /// [QuerySelection] argument.
    pub fn get_status<S, I>(
        &self,
        selection: QuerySelection<S, I>,
    ) -> Result<impl Iterator<Item = SvcStatus>, QueryError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        let mut args = vec![];

        self.add_zone_to_args(&mut args);
        self.add_columns_to_args(&mut args);

        match selection {
            QuerySelection::All => args.push("-a".to_string()),
            QuerySelection::ByRestarter(restarter) => {
                args.push(format!("-R {}", restarter.as_ref()));
            }
            QuerySelection::ByPattern(names) => self.add_patterns(&mut args, names),
        }

        self.run_and_parse_output(args)
    }

    // Shared implementation for getting dependencies and dependents.
    fn get_dep_impl<S, I>(
        &self,
        mut args: Vec<String>,
        patterns: I,
    ) -> Result<impl Iterator<Item = SvcStatus>, QueryError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        self.add_zone_to_args(&mut args);
        self.add_columns_to_args(&mut args);

        // XXX patterns need cleaning, same in other getters
        self.add_patterns(&mut args, patterns);

        self.run_and_parse_output(args)
    }

    /// Returns the statuses of service instances upon which the provided
    /// instances depend.
    ///
    /// ```no_run
    /// let service_statuses = smf::Query::new()
    ///     .get_dependencies_of(&["svcs:/system/filesystem/minimal"])
    ///     .unwrap();
    /// // `service_statuses` includes services which boot before the
    /// // minimal filesystem.
    /// ```
    pub fn get_dependencies_of<S, I>(
        &self,
        patterns: I,
    ) -> Result<impl Iterator<Item = SvcStatus>, QueryError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        let args = vec!["-d".to_string()];
        self.get_dep_impl(args, patterns)
    }

    /// Returns the statuses of service instances that depend on the
    /// provided instances.
    ///
    /// ```no_run
    /// let service_statuses = smf::Query::new()
    ///     .get_dependents_of(&["svcs:/system/filesystem/minimal"])
    ///     .unwrap();
    /// // `service_statuses` includes services which need a minimal
    /// // filesystem.
    /// ```
    pub fn get_dependents_of<S, I>(
        &self,
        patterns: I,
    ) -> Result<impl Iterator<Item = SvcStatus>, QueryError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        let args = vec!["-D".to_string()];
        self.get_dep_impl(args, patterns)
    }

    /// Acquires the log files for services which match the provided FMRIs
    /// or glob patterns.
    ///
    /// ```no_run
    /// let log_file = smf::Query::new()
    ///     .get_log_files(vec!["svc:/system/filesystem/minimal"]).unwrap()
    ///     .next().unwrap();
    /// ```
    pub fn get_log_files<S, I>(
        &self,
        patterns: I,
    ) -> Result<impl Iterator<Item = PathBuf>, QueryError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        let mut args = vec!["-L".to_string()];
        self.add_zone_to_args(&mut args);
        self.add_patterns(&mut args, patterns);
        Ok(self
            .run(args)?
            .split('\n')
            .map(|s| s.parse::<PathBuf>())
            .collect::<Result<Vec<PathBuf>, _>>()
            .unwrap()
            .into_iter())
    }
}

/*
 *
 * SVCCFG
 *
 */

/// The error code for any operation that fails during a config command.
#[derive(Error, Debug)]
pub enum ConfigError {
    /// Failure to execute a subcommand.
    #[error("Failed to execute command: {0}")]
    Command(std::io::Error),

    /// Failure reading a command's stdout (or non-zero error code).
    #[error("Failed to parse command output: {0}")]
    CommandOutput(#[from] CommandOutputError),
}

/// Provides an API to manipulate the configuration files for SMF services.
///
/// Acts as a wrapper around the underlying `svccfg` command.
pub struct Config {}

impl Config {
    /// Builds a [ConfigExport] object.
    ///
    /// ```no_run
    /// let manifest = smf::Config::export()
    ///     .archive()
    ///     .run("my-service")
    ///     .unwrap();
    /// ```
    pub fn export() -> ConfigExport {
        ConfigExport::new()
    }

    /// Builds a [ConfigImport] object.
    ///
    /// ```no_run
    /// smf::Config::import()
    ///     .run("/path/to/my/manifest.xml")
    ///     .unwrap();
    /// ```
    pub fn import() -> ConfigImport {
        ConfigImport::new()
    }

    /// Builds a [ConfigDelete] object.
    ///
    /// ```no_run
    /// smf::Config::delete()
    ///     .force()
    ///     .run("my-service")
    ///     .unwrap();
    /// ```
    pub fn delete() -> ConfigDelete {
        ConfigDelete::new()
    }

    /// Builds a [ConfigAdd] object.
    ///
    /// ```no_run
    /// smf::Config::add("svc:/system/service:parent")
    ///     .run("child")
    ///     .unwrap();
    pub fn add<S: AsRef<str>>(fmri: S) -> ConfigAdd {
        ConfigAdd::new(fmri.as_ref().into())
    }

    /// Builds a [ConfigSetProperty] object.
    ///
    /// ```no_run
    /// let property = smf::Property::new(
    ///     smf::PropertyName::new("group", "comment").unwrap(),
    ///     smf::PropertyValue::Astring("hello".to_string())
    /// );
    /// smf::Config::set_property("my_service:default")
    ///     .run(property)
    ///     .unwrap();
    /// ```
    pub fn set_property<S: AsRef<str>>(fmri: S) -> ConfigSetProperty {
        ConfigSetProperty::new(fmri.as_ref().into())
    }

    /// Builds a [ConfigAddPropertyValue] object.
    ///
    /// ```no_run
    /// let property = smf::Property::new(
    ///     smf::PropertyName::new("group", "comment").unwrap(),
    ///     smf::PropertyValue::Astring("hello".to_string())
    /// );
    /// smf::Config::add_property_value("my_service:default")
    ///     .run(property)
    ///     .unwrap();
    /// ```
    pub fn add_property_value<S: AsRef<str>>(fmri: S) -> ConfigAddPropertyValue {
        ConfigAddPropertyValue::new(fmri.as_ref().into())
    }

    /// Builds a [ConfigDeletePropertyValue] object.
    ///
    /// Note that the property value passed to
    /// [ConfigDeletePropertyValue::run()] is a glob pattern.
    ///
    /// ```no_run
    /// let property_name = smf::PropertyName::new("group", "comment").unwrap();
    /// smf::Config::delete_property_value("my_service:default")
    ///     .run(&property_name, "hello*")
    ///     .unwrap();
    /// ```
    pub fn delete_property_value<S: AsRef<str>>(fmri: S) -> ConfigDeletePropertyValue {
        ConfigDeletePropertyValue::new(fmri.as_ref().into())
    }
}

trait ConfigSubcommand {
    fn name(&self) -> &str;
    fn add_args(&self, args: &mut Vec<String>);
}

/// Created by [Config::export], represents a command to export
/// a service configuration.
pub struct ConfigExport {
    archive: bool,
}

impl ConfigExport {
    fn new() -> Self {
        ConfigExport { archive: false }
    }

    /// Archives all values, including protected information.
    pub fn archive(&mut self) -> &mut Self {
        self.archive = true;
        self
    }

    /// Returns the export command without running it.
    pub fn as_command<S: AsRef<str>>(&mut self, fmri: S) -> Command {
        let mut args = vec!["export"];
        if self.archive {
            args.push("-a");
        }
        args.push(fmri.as_ref());

        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the export command, returning the manifest output as a string.
    pub fn run<S: AsRef<str>>(&mut self, fmri: S) -> Result<String, ConfigError> {
        Ok(self
            .as_command(fmri)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()?)
    }
}

/// Created by [Config::import], represents a command to import
/// a service configuration.
pub struct ConfigImport {
    validate: bool,
}

impl ConfigImport {
    fn new() -> Self {
        ConfigImport { validate: true }
    }

    /// Requests that manifest data should not be validated before being
    /// imported. By default, this validation is enabled.
    pub fn no_validate(&mut self) -> &mut Self {
        self.validate = false;
        self
    }

    /// Returns the import command, without running it.
    pub fn as_command<P: AsRef<Path>>(&mut self, path: P) -> Command {
        let mut args = vec!["import"];
        if self.validate {
            args.push("-V");
        }
        let path_str = path.as_ref().to_string_lossy();
        args.push(&path_str);

        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the import command.
    pub fn run<P: AsRef<Path>>(&mut self, path: P) -> Result<(), ConfigError> {
        self.as_command(path)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()
            .map(|_| ())
            .map_err(|err| err.into())
    }
}

/// Created by [Config::delete], requests that a configuration be deleted.
pub struct ConfigDelete {
    force: bool,
}

impl ConfigDelete {
    fn new() -> Self {
        ConfigDelete { force: false }
    }

    /// Forcefully deletes the entity.
    ///
    /// Instances which are in the "online" or "degraded" state will
    /// not be deleted successfully unless this option is enabled.
    pub fn force(&mut self) -> &mut Self {
        self.force = true;
        self
    }

    /// Returns the deletion command, without running it.
    pub fn as_command<S: AsRef<str>>(&mut self, fmri: S) -> Command {
        let mut args = vec!["delete"];
        if self.force {
            args.push("-f");
        }
        args.push(fmri.as_ref());

        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the deletion command.
    pub fn run<S: AsRef<str>>(&mut self, fmri: S) -> Result<(), ConfigError> {
        self.as_command(fmri)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()
            .map(|_| ())
            .map_err(|err| err.into())
    }
}

/// Created by [Config::add], creates a new child instance of a service.
pub struct ConfigAdd {
    fmri: String,
}

impl ConfigAdd {
    fn new(fmri: String) -> Self {
        ConfigAdd { fmri }
    }

    /// Returns the command, without running it
    pub fn as_command<S: AsRef<str>>(&mut self, child: S) -> Command {
        let args = vec!["-s", &self.fmri, "add", child.as_ref()];
        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the add entity command.
    pub fn run<S: AsRef<str>>(&mut self, child: S) -> Result<(), ConfigError> {
        self.as_command(child)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()
            .map(|_| ())
            .map_err(|err| err.into())
    }
}

/// Created by [Config::set_property], sets a property for a service.
pub struct ConfigSetProperty {
    fmri: String,
}

impl ConfigSetProperty {
    fn new(fmri: String) -> Self {
        ConfigSetProperty { fmri }
    }

    /// Returns the command which would set a property
    pub fn as_command(&self, property: Property) -> Command {
        let prop = format!(
            "{} = {}",
            property.name.to_string(),
            property.value.to_string()
        );

        let args = vec!["-s", &self.fmri, "setprop", &prop];
        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the command to set a property
    pub fn run(&self, property: Property) -> Result<(), ConfigError> {
        self.as_command(property)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()
            .map(|_| ())
            .map_err(|err| err.into())
    }
}

/// Created by [Config::add_property_value], adds a property value for a
/// service.
pub struct ConfigAddPropertyValue {
    fmri: String,
}

impl ConfigAddPropertyValue {
    fn new(fmri: String) -> Self {
        Self { fmri }
    }

    /// Returns the command to add a property
    pub fn as_command(&self, property: Property) -> Command {
        let name = property.name.to_string();
        let value = property.value.to_string();
        let args = vec!["-s", &self.fmri, "addpropvalue", &name, &value];
        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the add property value command.
    pub fn run(&self, property: Property) -> Result<(), ConfigError> {
        self.as_command(property)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()
            .map(|_| ())
            .map_err(|err| err.into())
    }
}

/// Created by [Config::delete_property_value], adds a property value for a
/// service.
pub struct ConfigDeletePropertyValue {
    fmri: String,
}

impl ConfigDeletePropertyValue {
    fn new(fmri: String) -> Self {
        Self { fmri }
    }

    /// Returns the command without running it.
    pub fn as_command(&self, property_name: &PropertyName, value: &str) -> Command {
        let name = property_name.to_string();
        let args = vec!["-s", &self.fmri, "delpropvalue", &name, value];
        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCCFG).args(args);
        cmd
    }

    /// Runs the delete property value command.
    pub fn run(&self, property_name: &PropertyName, value: &str) -> Result<(), ConfigError> {
        self.as_command(property_name, value)
            .output()
            .map_err(ConfigError::Command)?
            .read_stdout()
            .map(|_| ())
            .map_err(|err| err.into())
    }
}

/*
 *
 * SVCADM
 *
 */

/// The error code for any operation that fails during an adm command.
#[derive(Error, Debug)]
pub enum AdmError {
    /// Failure to execute a subcommand.
    #[error("Failed to execute command: {0}")]
    Command(std::io::Error),

    /// Failure reading a command's stdout (or non-zero error code).
    #[error("Failed to parse command output: {0}")]
    CommandOutput(#[from] CommandOutputError),
}

/// Determines which services are returned from [Adm] operations.
pub enum AdmSelection<S, I>
where
    S: AsRef<str>,
    I: IntoIterator<Item = S>,
{
    /// Selects all services which are in the provided state.
    ByState(SmfState),
    /// All service instance which match the provided strings as either an FMRI
    /// or pattern (globs allowed) matching FMRIs.
    ByPattern(I),
}

/// Provides tools for changing the state of SMF services.
///
/// Acts as a wrapper around the underlying 'svcadm' command.
pub struct Adm {
    zone: Option<String>,
}

impl Default for Adm {
    fn default() -> Self {
        Self::new()
    }
}

impl Adm {
    /// Construct a new builder object.
    pub fn new() -> Adm {
        Adm { zone: None }
    }

    fn add_zone_to_args(&self, args: &mut Vec<String>) {
        // TODO-feature: Add support for "-Z", all zones.
        if let Some(zone) = &self.zone {
            args.push("-z".to_string());
            args.push(zone.to_string());
        }
    }

    /// Requests a command be issued within a specific zone.
    pub fn zone<S: AsRef<str>>(&mut self, zone: S) -> &mut Adm {
        self.zone.replace(zone.as_ref().into());
        self
    }

    /// Builds a [AdmEnable] object.
    ///
    /// ```no_run
    /// use smf::{Adm, AdmSelection};
    ///
    /// Adm::new()
    ///     .enable()
    ///     .synchronous()
    ///     .run(AdmSelection::ByPattern(&["service"]))
    ///     .unwrap();
    /// ```
    pub fn enable(&self) -> AdmEnable {
        AdmEnable::new(self)
    }

    /// Builds a [AdmDisable] object.
    ///
    /// ```no_run
    /// use smf::{Adm, AdmSelection};
    ///
    /// Adm::new()
    ///     .disable()
    ///     .synchronous()
    ///     .run(AdmSelection::ByPattern(&["service"]))
    ///     .unwrap();
    /// ```
    pub fn disable(&self) -> AdmDisable {
        AdmDisable::new(self)
    }

    /// Builds a [AdmRestart] object.
    ///
    /// ```no_run
    /// use smf::{Adm, AdmSelection};
    ///
    /// Adm::new()
    ///     .restart()
    ///     .abort()
    ///     .run(AdmSelection::ByPattern(&["service"]))
    ///     .unwrap();
    /// ```
    pub fn restart(&self) -> AdmRestart {
        AdmRestart::new(self)
    }

    /// Builds a [AdmRefresh] object.
    ///
    /// ```no_run
    /// use smf::{Adm, AdmSelection};
    ///
    /// Adm::new()
    ///     .refresh()
    ///     .run(AdmSelection::ByPattern(&["service"]))
    ///     .unwrap();
    /// ```
    pub fn refresh(&self) -> AdmRefresh {
        AdmRefresh::new(self)
    }

    /// Builds a [AdmClear] object.
    ///
    /// ```no_run
    /// use smf::{Adm, AdmSelection};
    ///
    /// Adm::new()
    ///     .clear()
    ///     .run(AdmSelection::ByPattern(&["service"]))
    ///     .unwrap();
    /// ```
    pub fn clear(&self) -> AdmClear {
        AdmClear::new(self)
    }

    // TODO: Mark, milestone
}

/// Private trait to help implement a subcommand.
trait AdmSubcommand {
    /// Returns the base Adm object.
    fn adm(&self) -> &Adm;

    /// Returns the name of the Adm subcommand.
    fn command_name(&self) -> &str;

    /// Adds subcommand specific arguments.
    fn add_to_args(&self, args: &mut Vec<String>);
}

/// Shared mechanism of running all subcommands created by [Adm].
fn as_adm_subcommand<C, S, I>(subcommand: &C, selection: AdmSelection<S, I>) -> Command
where
    C: AdmSubcommand,
    S: AsRef<str>,
    I: IntoIterator<Item = S>,
{
    let mut args = vec![];

    subcommand.adm().add_zone_to_args(&mut args);

    match selection {
        AdmSelection::ByState(state) => {
            args.push("-S".to_string());
            args.push(state.to_string());
            args.push(subcommand.command_name().to_string());
            subcommand.add_to_args(&mut args);
        }
        AdmSelection::ByPattern(pattern) => {
            args.push(subcommand.command_name().to_string());
            subcommand.add_to_args(&mut args);
            args.extend(pattern.into_iter().map(|s| s.as_ref().to_string()));
        }
    }
    let mut cmd = std::process::Command::new(PFEXEC);
    cmd.env_clear().arg(SVCADM).args(args);
    cmd
}

/// Shared mechanism of running all subcommands created by [Adm].
fn run_adm_subcommand<C, S, I>(
    subcommand: &C,
    selection: AdmSelection<S, I>,
) -> Result<(), AdmError>
where
    C: AdmSubcommand,
    S: AsRef<str>,
    I: IntoIterator<Item = S>,
{
    as_adm_subcommand(subcommand, selection)
        .output()
        .map_err(AdmError::Command)?
        .read_stdout()?;
    Ok(())
}

/// Created by [Adm::enable], enables the service instance(s).
///
/// The assigned restarter will attempt to bring the service to the online
/// state.
pub struct AdmEnable<'a> {
    adm: &'a Adm,
    recursive: bool,
    synchronous: bool,
    temporary: bool,
}

impl<'a> AdmSubcommand for AdmEnable<'a> {
    fn adm(&self) -> &Adm {
        &self.adm
    }
    fn command_name(&self) -> &str {
        "enable"
    }
    fn add_to_args(&self, args: &mut Vec<String>) {
        if self.recursive {
            args.push("-r".to_string())
        }
        if self.synchronous {
            args.push("-s".to_string())
        }
        if self.temporary {
            args.push("-t".to_string())
        }
    }
}

impl<'a> AdmEnable<'a> {
    fn new(adm: &'a Adm) -> Self {
        AdmEnable {
            adm,
            recursive: false,
            synchronous: false,
            temporary: false,
        }
    }

    /// Recursively enables dependencies of enabled services.
    pub fn recursive(&mut self) -> &mut Self {
        self.recursive = true;
        self
    }
    /// Waits for each enabled instance to enter either `online` or `degraded`
    /// state.
    pub fn synchronous(&mut self) -> &mut Self {
        self.synchronous = true;
        self
    }
    /// Temporarily enables each service instance, meaning that
    /// the decision to enable will only last until reboot.
    pub fn temporary(&mut self) -> &mut Self {
        self.temporary = true;
        self
    }

    /// Returns the command, without running it
    pub fn as_command<S, I>(&mut self, selection: AdmSelection<S, I>) -> Command
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        as_adm_subcommand(self, selection)
    }

    /// Runs the command.
    pub fn run<S, I>(&mut self, selection: AdmSelection<S, I>) -> Result<(), AdmError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        run_adm_subcommand(self, selection)
    }
}

/// Created by [Adm::disable], disables the service instance(s).
///
/// The assigned restarter will attempt to bring the service to the disabled
/// state.
pub struct AdmDisable<'a> {
    adm: &'a Adm,
    comment: Option<String>,
    synchronous: bool,
    temporary: bool,
}

impl<'a> AdmSubcommand for AdmDisable<'a> {
    fn adm(&self) -> &Adm {
        &self.adm
    }
    fn command_name(&self) -> &str {
        "disable"
    }
    fn add_to_args(&self, args: &mut Vec<String>) {
        if let Some(ref comment) = self.comment {
            args.push("-c".to_string());
            args.push(comment.to_string());
        }
        if self.synchronous {
            args.push("-s".to_string())
        }
        if self.temporary {
            args.push("-t".to_string())
        }
    }
}

impl<'a> AdmDisable<'a> {
    fn new(adm: &'a Adm) -> Self {
        AdmDisable {
            adm,
            comment: None,
            synchronous: false,
            temporary: false,
        }
    }

    /// Records a general free-form comment in the service configuration.
    pub fn comment<S: AsRef<str>>(&mut self, comment: S) -> &mut Self {
        self.comment = Some(comment.as_ref().to_owned());
        self
    }
    /// Waits for each instance to enter either the `disabled` state.
    pub fn synchronous(&mut self) -> &mut Self {
        self.synchronous = true;
        self
    }
    /// Temporarily disable each service instance, meaning that the decision to
    /// disable will only last until reboot.
    pub fn temporary(&mut self) -> &mut Self {
        self.temporary = true;
        self
    }

    /// Returns the command, without running it
    pub fn as_command<S, I>(&mut self, selection: AdmSelection<S, I>) -> Command
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        as_adm_subcommand(self, selection)
    }

    /// Runs the command.
    pub fn run<S, I>(&mut self, selection: AdmSelection<S, I>) -> Result<(), AdmError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        run_adm_subcommand(self, selection)
    }
}

/// Created by [Adm::restart], restarts the service instance(s).
pub struct AdmRestart<'a> {
    adm: &'a Adm,
    abort: bool,
}

impl<'a> AdmSubcommand for AdmRestart<'a> {
    fn adm(&self) -> &Adm {
        &self.adm
    }
    fn command_name(&self) -> &str {
        "restart"
    }
    fn add_to_args(&self, args: &mut Vec<String>) {
        if self.abort {
            args.push("-d".to_string())
        }
    }
}

impl<'a> AdmRestart<'a> {
    fn new(adm: &'a Adm) -> Self {
        Self { adm, abort: false }
    }
    /// Requests that the restarter should send a `SIGABRT` signal
    /// to all members of the contract before restarting the service.
    pub fn abort(&mut self) -> &mut Self {
        self.abort = true;
        self
    }

    /// Returns the command, without running it
    pub fn as_command<S, I>(&mut self, selection: AdmSelection<S, I>) -> Command
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        as_adm_subcommand(self, selection)
    }

    /// Runs the command.
    pub fn run<S, I>(&mut self, selection: AdmSelection<S, I>) -> Result<(), AdmError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        run_adm_subcommand(self, selection)
    }
}

/// Created by [Adm::refresh], refreshes a snapshot.
///
/// Requests that the restarter update the configuration snapshot of all
/// requested services, replacing it with values from the current configuration.
pub struct AdmRefresh<'a> {
    adm: &'a Adm,
}

impl<'a> AdmSubcommand for AdmRefresh<'a> {
    fn adm(&self) -> &Adm {
        &self.adm
    }
    fn command_name(&self) -> &str {
        "refresh"
    }
    fn add_to_args(&self, _args: &mut Vec<String>) {}
}

impl<'a> AdmRefresh<'a> {
    fn new(adm: &'a Adm) -> Self {
        Self { adm }
    }

    /// Returns the command, without running it
    pub fn as_command<S, I>(&mut self, selection: AdmSelection<S, I>) -> Command
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        as_adm_subcommand(self, selection)
    }

    /// Runs the command.
    pub fn run<S, I>(&mut self, selection: AdmSelection<S, I>) -> Result<(), AdmError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        run_adm_subcommand(self, selection)
    }
}

/// Created by [Adm::clear], clears services in a `degraded`/`maintenance` state.
///
/// Signals to the restarter that a service instance has been
/// repaired. If the instance is degraded, requests that the
/// restarter takes the service to the online state.
pub struct AdmClear<'a> {
    adm: &'a Adm,
}

impl<'a> AdmSubcommand for AdmClear<'a> {
    fn adm(&self) -> &Adm {
        &self.adm
    }
    fn command_name(&self) -> &str {
        "clear"
    }
    fn add_to_args(&self, _args: &mut Vec<String>) {}
}

impl<'a> AdmClear<'a> {
    fn new(adm: &'a Adm) -> Self {
        Self { adm }
    }

    /// Returns the command, without running it
    pub fn as_command<S, I>(&mut self, selection: AdmSelection<S, I>) -> Command
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        as_adm_subcommand(self, selection)
    }

    /// Runs the command.
    pub fn run<S, I>(&mut self, selection: AdmSelection<S, I>) -> Result<(), AdmError>
    where
        S: AsRef<str>,
        I: IntoIterator<Item = S>,
    {
        run_adm_subcommand(self, selection)
    }
}

/*
 *
 * SVCPROP
 *
 */

/// The error type represting a failure to parse a property or property group.
#[derive(Error, Debug)]
#[error("Invalid Property: {0}")]
pub struct PropertyParseError(String);

fn valid_property_substring(s: &str) -> bool {
    !s.contains(char::is_whitespace) && !s.contains('/')
}

/// The group portion of a property name.
///
/// Property names typically have the form:
///   group/property
/// This object represents the "group" portion of that object.
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyGroupName {
    group: String,
}

impl PropertyGroupName {
    /// Creates a new group name object, returning an error if it cannot be
    /// parsed as a group name.
    pub fn new<S>(group: S) -> Result<PropertyGroupName, PropertyParseError>
    where
        S: AsRef<str>,
    {
        if !valid_property_substring(group.as_ref()) {
            return Err(PropertyParseError("Invalid property group".to_string()));
        }
        Ok(PropertyGroupName {
            group: group.as_ref().into(),
        })
    }

    /// Returns the name of the group as a string.
    pub fn group(&self) -> &str {
        &self.group
    }
}

impl AsRef<str> for PropertyGroupName {
    fn as_ref(&self) -> &str {
        &self.group
    }
}

impl ToString for PropertyGroupName {
    fn to_string(&self) -> String {
        self.group.clone()
    }
}

/// The group and property portions of a property name.
///
/// Property names typically have the form:
///   group/property
/// This object represents that entire object.
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyName {
    group: PropertyGroupName,
    property: String,
}

impl PropertyName {
    /// Creates a new property name object, returning an error if it cannot be
    /// parsed.
    pub fn new<S1, S2>(group: S1, property: S2) -> Result<PropertyName, PropertyParseError>
    where
        S1: AsRef<str>,
        S2: AsRef<str>,
    {
        let group = PropertyGroupName::new(group)?;
        if !valid_property_substring(property.as_ref()) {
            return Err(PropertyParseError("Invalid property value".to_string()));
        }

        Ok(PropertyName {
            group,
            property: property.as_ref().into(),
        })
    }

    /// Returns the name of the group as a string.
    pub fn group(&self) -> &str {
        &self.group.as_ref()
    }
    /// Returns the name of the property as a string.
    pub fn property(&self) -> &str {
        &self.property
    }
}

impl FromStr for PropertyName {
    type Err = PropertyParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split('/');
        let group = iter
            .next()
            .ok_or("Missing Group")
            .map_err(|e| PropertyParseError(e.to_string()))?;
        let property = iter
            .next()
            .ok_or("Missing Property")
            .map_err(|e| PropertyParseError(e.to_string()))?;
        if let Some(s) = iter.next() {
            Err(PropertyParseError(format!(
                "Unexpected string in property name: {}",
                s
            )))
        } else {
            PropertyName::new(group, property)
        }
    }
}

impl ToString for PropertyName {
    fn to_string(&self) -> String {
        format!("{}/{}", self.group.as_ref(), self.property)
    }
}

/// Describes a Property, with both its name and value.
#[derive(Clone, Debug, PartialEq)]
pub struct Property {
    name: PropertyName,
    value: PropertyValue,
}

impl Property {
    /// Creates a new Property object from a name/value pair.
    pub fn new(name: PropertyName, value: PropertyValue) -> Self {
        Property { name, value }
    }

    /// Accesses the name of this property.
    pub fn name(&self) -> &PropertyName {
        &self.name
    }

    /// Accesses the value of this property.
    pub fn value(&self) -> &PropertyValue {
        &self.value
    }
}

impl FromStr for Property {
    type Err = PropertyParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split(' ');
        let name_str = iter
            .next()
            .ok_or("Missing FMRI")
            .map_err(|err| PropertyParseError(err.to_string()))?;
        let name = str::parse::<PropertyName>(name_str)?;
        let r = iter.collect::<Vec<&str>>().join(" ");
        let value = str::parse::<PropertyValue>(&r)?;

        Ok(Property { name, value })
    }
}

/// Defines the values properties may have.
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyValue {
    /// A boolean value.
    Boolean(bool),
    /// An unsigned integer value.
    Count(u64),
    /// An signed integer value.
    Integer(i64),
    /// An 8-bit NULL-terminated string.
    ///
    /// Disclaimer: This library treats Astring and Ustring objects
    /// identically, only distinguishing by the provided type name.
    Astring(String),
    /// An 8-bit UTF-8 string string.
    Ustring(String),
    /// One or more FMRI objects.
    Fmri(Vec<String>),
    /// Any other property type.
    Other(String),
}

impl FromStr for PropertyValue {
    type Err = PropertyParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split(' ');
        let value = || -> Result<PropertyValue, String> {
            let ty = iter.next().ok_or("Missing type")?;

            let value = iter.collect::<Vec<&str>>().join(" ");

            let pv = match ty {
                "boolean" => {
                    PropertyValue::Boolean(value.parse::<bool>().map_err(|err| err.to_string())?)
                }
                "count" => {
                    PropertyValue::Count(value.parse::<u64>().map_err(|err| err.to_string())?)
                }
                "integer" => {
                    PropertyValue::Integer(value.parse::<i64>().map_err(|err| err.to_string())?)
                }
                "astring" => PropertyValue::Astring(value),
                "ustring" => PropertyValue::Ustring(value),
                "fmri" => PropertyValue::Fmri(
                    value
                        .split_whitespace()
                        .map(|s| s.to_string())
                        .collect::<Vec<String>>(),
                ),
                _ => PropertyValue::Other(value),
            };
            Ok(pv)
        }()
        .map_err(PropertyParseError)?;

        Ok(value)
    }
}

impl ToString for PropertyValue {
    fn to_string(&self) -> String {
        match self {
            PropertyValue::Boolean(b) => format!("boolean: {}", b),
            PropertyValue::Count(c) => format!("count: {}", c),
            PropertyValue::Integer(i) => format!("integer: {}", i),
            PropertyValue::Astring(s) => format!("astring: {}", s),
            PropertyValue::Ustring(s) => format!("ustring: {}", s),
            PropertyValue::Fmri(fmris) => format!("fmri: {}", fmris.join(" ")),
            PropertyValue::Other(s) => s.to_string(),
        }
    }
}

/// Errors which can be returned when querying properties.
#[derive(Error, Debug)]
pub enum PropertyError {
    /// Failure to parse.
    #[error("Parse error: {0}")]
    ParseError(#[from] PropertyParseError),

    /// Failure to execute a subcommand.
    #[error("Failed to execute command: {0}")]
    Command(std::io::Error),

    /// Failure reading a command's stdout (or non-zero error code).
    #[error("Failed to parse command output: {0}")]
    CommandOutput(#[from] CommandOutputError),
}

/// Queries the properties of a service or instance.
pub struct Properties {
    zone: Option<String>,
}

impl Default for Properties {
    fn default() -> Self {
        Properties::new()
    }
}

impl Properties {
    /// Creates a new property-querying object.
    pub fn new() -> Self {
        Properties { zone: None }
    }

    /// Requests a command be issued within a specific zone.
    pub fn zone<S: AsRef<str>>(&mut self, zone: S) -> &mut Properties {
        self.zone.replace(zone.as_ref().into());
        self
    }

    fn add_zone_to_args(&self, args: &mut Vec<String>) {
        // TODO-feature: Add support for "-Z", all zones.
        if let Some(zone) = &self.zone {
            args.push("-z".to_string());
            args.push(zone.to_string());
        }
    }

    /// Acquires a [PropertyLookup] command, capable of listing properties.
    ///
    /// ```no_run
    /// let name = smf::PropertyName::new("general", "comment").unwrap();
    /// let property = smf::Properties::new()
    ///     .lookup()
    ///     .run(&name, "my-service")
    ///     .unwrap();
    /// ```
    pub fn lookup(&self) -> PropertyLookup {
        PropertyLookup::new(self)
    }

    /// Acquires a [PropertyWait] command, capable of waiting for properties
    /// to change.
    ///
    /// ```no_run
    /// let group = smf::PropertyGroupName::new("general").unwrap();
    /// let property = smf::Properties::new()
    ///     .wait()
    ///     .run(&group, "my-service")
    ///     .unwrap();
    /// ```
    pub fn wait(&self) -> PropertyWait {
        PropertyWait::new(self)
    }
}

/// The selection of properties for a service or instance.
///
/// Services always return directly attached properties; these
/// options only apply to service instances.
pub enum PropertyClass {
    /// Effective properties, relative to an optional snapshot.
    /// For instances: The composed view of the snapshot with all
    /// non-persistent properties.
    ///
    /// This is equivalent to `svcprop` with no arguments,
    /// or `svcprop -s <snapshot>` if a string is supplied.
    Effective(Option<String>),
    /// Directly attached properties, with no composition nor snapshots.
    ///
    /// This is equivalent to `svcprop -C`.
    DirectlyAttachedUncomposed,
    /// Directly attached properties, composed with the directly attached
    /// properties of the service.
    ///
    /// This is equivalent to `svcprop -c`.
    DirectlyAttachedComposed,
}

/// Created by [Properties::lookup], a builder object capable of listing properties.
pub struct PropertyLookup<'a> {
    property_base: &'a Properties,
    attachment: PropertyClass,
}

impl<'a> PropertyLookup<'a> {
    fn new(property_base: &'a Properties) -> Self {
        PropertyLookup {
            property_base,
            attachment: PropertyClass::Effective(None),
        }
    }

    /// Optionally requests properties with a particular view.
    /// Refer to [PropertyClass] for a more exhaustive explanation.
    pub fn attachment(&mut self, attachment: PropertyClass) -> &mut Self {
        self.attachment = attachment;
        self
    }

    fn add_attachment_to_args(&self, args: &mut Vec<String>) {
        match &self.attachment {
            PropertyClass::Effective(None) => (),
            PropertyClass::Effective(Some(snap)) => {
                args.push("-s".into());
                args.push(snap.to_string());
            }
            PropertyClass::DirectlyAttachedUncomposed => args.push("-C".into()),
            PropertyClass::DirectlyAttachedComposed => args.push("-c".into()),
        }
    }

    /// Returns the command to lookup a property
    pub fn as_command<S>(&mut self, property: &PropertyName, fmri: S) -> Command
    where
        S: AsRef<str>,
    {
        // Longer output, but more consistent parsing.
        let mut args = vec!["-t".to_string()];

        // [-C | -c | -s snapshot]
        self.add_attachment_to_args(&mut args);

        // [-z zone]
        self.property_base.add_zone_to_args(&mut args);

        // [-p [name/name]]
        // To simplify parsing, restrict access to a single property.
        //
        // For scripting, this is the most common use-case anyway.
        args.push("-p".to_string());
        args.push(property.to_string());

        // {FMRI | pattern}
        // Requests a single FMRI.
        args.push(fmri.as_ref().into());

        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCPROP).args(args);
        cmd
    }

    /// Parses the output for an executed command from [Self::as_command].
    pub fn parse_output(output: &Output) -> Result<Property, PropertyError> {
        let out = output.read_stdout()?;
        out.parse().map_err(|err: PropertyParseError| err.into())
    }

    /// Looks up a property for a specified FMRI.
    pub fn run<S>(&mut self, property: &PropertyName, fmri: S) -> Result<Property, PropertyError>
    where
        S: AsRef<str>,
    {
        let out = self
            .as_command(property, fmri)
            .output()
            .map_err(PropertyError::Command)?;
        Self::parse_output(&out)
    }
}

/// Created by [Properties::wait], a builder object waiting for a property group to change.
pub struct PropertyWait<'a> {
    property_base: &'a Properties,
}

impl<'a> PropertyWait<'a> {
    fn new(property_base: &'a Properties) -> Self {
        PropertyWait { property_base }
    }

    /// Returns the Command to wait for a property, without running it.
    pub fn as_command<S>(&mut self, property: &PropertyGroupName, fmri: S) -> Command
    where
        S: AsRef<str>,
    {
        let mut args = vec![
            "-w".to_string(),
            // Longer output, but more consistent parsing.
            "-t".to_string(),
        ];

        // [-z zone]
        self.property_base.add_zone_to_args(&mut args);

        // [-p [name/name]]
        // To simplify parsing, restrict access to a single property.
        //
        // For scripting, this is the most common use-case anyway.
        args.push("-p".to_string());
        args.push(property.to_string());

        // {FMRI | pattern}
        // Requests a single FMRI.
        args.push(fmri.as_ref().into());

        let mut cmd = std::process::Command::new(PFEXEC);
        cmd.env_clear().arg(SVCPROP).args(args);
        cmd
    }

    /// Parses the output for an executed command from [Self::as_command].
    pub fn parse_output(output: &Output) -> Result<Property, PropertyError> {
        let out = output.read_stdout()?;
        out.parse().map_err(|err: PropertyParseError| err.into())
    }

    /// Waits until a specified property group changes before printing.
    ///
    /// Returns requested property - note that it might not be the
    /// property which changed.
    pub fn run<S>(
        &mut self,
        property: &PropertyGroupName,
        fmri: S,
    ) -> Result<Property, PropertyError>
    where
        S: AsRef<str>,
    {
        let output = self
            .as_command(property, fmri)
            .output()
            .map_err(PropertyError::Command)?;
        Self::parse_output(&output)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_status() {
        let s = "svc:/system/device/local:default                        - default              -    localhost            system/device/local  ON   14:57:25 global           Standard Solaris device config";

        let status = SvcStatus::from_str(&s);
        assert!(status.is_ok());
        let status = status.unwrap();
        let expected = SvcStatus {
            fmri: "svc:/system/device/local:default".to_string(),
            contract_id: None,
            instance_name: "default".to_string(),
            next_state: None,
            scope_name: "localhost".to_string(),
            service_name: "system/device/local".to_string(),
            state: SmfState::Online,
            service_time: "14:57:25".to_string(),
            zone: "global".to_string(),
            description: Some("Standard Solaris device config".to_string()),
        };
        assert_eq!(status, expected);
    }

    #[test]
    fn test_svcs_query_one() {
        let inst = "default";
        let svc = "system/filesystem/root";
        let fmri = format!("svc:/{}:{}", svc, inst);
        let pattern = [&fmri];

        let query = Query::new().get_status(QuerySelection::ByPattern(&pattern));
        assert!(query.is_ok(), "Unexpected err: {}", query.err().unwrap());

        let mut results = query.unwrap();
        let first = results.next().unwrap();
        assert_eq!(first.fmri, fmri);
        assert_eq!(first.service_name, svc);
        assert_eq!(first.instance_name, inst);
        assert!(results.next().is_none());
    }

    #[test]
    fn test_svcs_query_multiple() {
        let svc_root = "system/filesystem/root";
        let svc_usr = "system/filesystem/usr";
        let pattern = [svc_usr, svc_root];

        let query = Query::new().get_status(QuerySelection::ByPattern(&pattern));
        assert!(query.is_ok(), "Unexpected err: {}", query.err().unwrap());

        let mut results = query.unwrap();
        let root = results.next().unwrap();
        assert_eq!(root.service_name, svc_root);
        let usr = results.next().unwrap();
        assert_eq!(usr.service_name, svc_usr);
        assert!(results.next().is_none());
    }

    #[test]
    fn test_svcs_get_status_all() {
        let query = Query::new().get_status_all();
        assert!(query.is_ok(), "Unexpected err: {}", query.err().unwrap());
    }

    #[test]
    fn test_svcs_get_status_all_global_zone() {
        let query = Query::new().zone("global").get_status_all();
        assert!(query.is_ok(), "Unexpected err: {}", query.err().unwrap());
    }

    #[test]
    fn test_svcprop_parse_property_value() {
        let input = "astring hello";
        let value = str::parse::<PropertyValue>(&input).unwrap();
        assert!(matches!(value, PropertyValue::Astring(s) if s == "hello"));
    }

    #[test]
    fn test_svcprop_parse_property() {
        let input = "general/comment astring hello";
        let property = str::parse::<Property>(&input).unwrap();
        assert_eq!(property.name.to_string(), "general/comment");
        assert!(matches!(property.value, PropertyValue::Astring(s) if s == "hello"));
    }

    #[test]
    fn test_svcprop_lookup_property_astring() {
        let property_name = PropertyName::new("restarter", "state").unwrap();
        let property = Properties::new()
            .lookup()
            .run(&property_name, "svc:/system/filesystem/root:default")
            .unwrap();
        assert_eq!(property_name, property.name);
        match &property.value {
            PropertyValue::Astring(val) => assert_eq!(&val[..], "online"),
            _ => panic!("Unexpected value: {:#?}", property.value),
        }
    }

    #[test]
    fn test_svcprop_lookup_property_integer() {
        let property_name = PropertyName::new("restarter", "start_pid").unwrap();
        let property = Properties::new()
            .lookup()
            .run(&property_name, "svc:/system/filesystem/root:default")
            .unwrap();
        assert_eq!(property_name, property.name);
        match &property.value {
            PropertyValue::Count(_) => (),
            _ => panic!("Unexpected value: {:#?}", property.value),
        }
    }

    // TODO-test: Queries w/flags in them? (Filter them out / flag as errors!)
    // TODO-test: Test zones?
    // TODO-test: Repeated names?
    // TODO-test: Test failures?
}