-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathDscExecuteResult.cs
More file actions
102 lines (92 loc) · 3.79 KB
/
DscExecuteResult.cs
File metadata and controls
102 lines (92 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
namespace Microsoft.WingetCreateUnitTests.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.WingetCreateCLI.Models.DscModels;
using Newtonsoft.Json;
/// <summary>
/// Result of executing a DSC command.
/// </summary>
public class DscExecuteResult
{
/// <summary>
/// Initializes a new instance of the <see cref="DscExecuteResult"/> class.
/// </summary>
/// <param name="success">Value indicating whether the command execution was successful.</param>
/// <param name="output">Output stream content.</param>
/// <param name="error">Error stream content.</param>
public DscExecuteResult(bool success, string output, string error)
{
this.Success = success;
this.Output = output;
this.Error = error;
}
/// <summary>
/// Gets a value indicating whether the command execution was successful.
/// </summary>
public bool Success { get; }
/// <summary>
/// Gets the output stream content of the operation.
/// </summary>
public string Output { get; }
/// <summary>
/// Gets the error stream content of the operation.
/// </summary>
public string Error { get; }
/// <summary>
/// Gets the messages from the error stream.
/// </summary>
/// <returns>List of messages with their levels.</returns>
public List<(DscMessageLevel Level, string Message)> Messages()
{
var lines = this.Error.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries);
return lines.SelectMany(line =>
{
var map = JsonConvert.DeserializeObject<Dictionary<string, string>>(line);
return map.Select(kvp => (this.GetMessageLevel(kvp.Key), kvp.Value)).ToList();
}).ToList();
}
/// <summary>
/// Gets the output as settings state.
/// </summary>
/// <returns>Settings state.</returns>
public SettingsResourceObject OutputState()
{
var lines = this.Output.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries);
Debug.Assert(lines.Length == 1, "Output should contain exactly one line.");
return JsonConvert.DeserializeObject<SettingsResourceObject>(lines[0]);
}
/// <summary>
/// Gets the output as settings state and diff.
/// </summary>
/// <returns>Settings state and diff.</returns>
public (SettingsResourceObject State, List<string> Diff) OutputStateAndDiff()
{
var lines = this.Output.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries);
Debug.Assert(lines.Length == 2, "Output should contain exactly two lines.");
var settingsObject = JsonConvert.DeserializeObject<SettingsResourceObject>(lines[0]);
var diff = JsonConvert.DeserializeObject<List<string>>(lines[1]);
return (settingsObject, diff);
}
/// <summary>
/// Gets the message level from a string representation.
/// </summary>
/// <param name="level">The string representation of the message level.</param>
/// <returns>The level as <see cref="DscMessageLevel"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the level is unknown.</exception>
private DscMessageLevel GetMessageLevel(string level)
{
return level switch
{
"error" => DscMessageLevel.Error,
"warn" => DscMessageLevel.Warning,
"info" => DscMessageLevel.Info,
"debug" => DscMessageLevel.Debug,
"trace" => DscMessageLevel.Trace,
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "Unknown message level"),
};
}
}