Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Localization/Resources/de-DE/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@ Warnung: Die Verwendung dieses Arguments kann dazu führen, dass das Token proto
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>Es wurde kein Vorgang angegeben. Verwenden Sie „--help“, um verfügbare Vorgänge anzuzeigen.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>Der Vorgang der DSC-Ressource ist fehlgeschlagen</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Gibt an, ob sich eine Instanz im gewünschten Zustand befindet</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/es-ES/Resources.es-ES.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@ Advertencia: El uso de este argumento puede resultar en que se registre el token
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>No se especificó ninguna operación. Utilice --help para ver las operaciones disponibles.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>La operación del recurso DSC ha fallado</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Indica si una instancia se encuentra en el estado deseado</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/fr-FR/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@ Avertissement : l’utilisation de cet argument peut entraîner la journalisati
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>Aucune opération n’est spécifiée. Utilisez --help pour voir les opérations disponibles.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>Nous n’avons pas pu effectuer l’opération de la ressource DSC</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Indique si un instance est dans l’état souhaité</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/it-IT/Resources.it-IT.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@ Avviso: l'uso di questo argomento può comportare la registrazione del token. Va
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>Nessuna operazione specificata. Usa --help per visualizzare le operazioni disponibili.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>Operazione sulla risorsa DSC non riuscita</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Indica se un'istanza si trova nello stato desiderato</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/ja-JP/Resources.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>エラー: 操作が指定されていません。--help を使用して、使用可能な操作を確認してください。</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>DSC リソース操作に失敗しました</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>インスタンスが目的の状態であるかどうかを示します</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/ko-KR/Resources.ko-KR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>지정된 작업이 없습니다. --help를 사용하여 사용 가능한 작업을 확인하세요.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>DSC 리소스 작업이 실패했습니다.</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>인스턴스가 원하는 상태인지 여부를 나타냅니다.</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/pt-BR/Resources.pt-BR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@ Aviso: o uso desse argumento pode fazer com que o token seja registrado. Consid
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>Nenhuma operação especificada. Use --help para ver as operações disponíveis.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>Falha na operação do recurso DSC</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Indica se uma instância está no estado desejado</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/ru-RU/Resources.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>Операция не указана. Используйте параметр --help для просмотра доступных операций.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>Сбой операции с ресурсом DSC</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Указывает, находится ли экземпляр в желаемом состоянии</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/zh-CN/Resources.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>未指定任何操作。使用 --help 查看可用操作。</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>DSC 资源操作失败</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>指示实例是否处于所需状态</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions Localization/Resources/zh-TW/Resources.zh-TW.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>未指定任何作業。使用 --help 查看可用的作業。</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>DSC 資源作業失敗</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>指出實例是否處於預期狀態</value>
</data>
Expand Down
8 changes: 1 addition & 7 deletions src/WingetCreateCLI/Commands/DscCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ public override async Task<bool> Execute()
{
if (methodFlag)
{
if (!methodAction())
{
Logger.ErrorLocalized(nameof(Resources.DscResourceOperationFailed_Message));
return false;
}

return true;
return methodAction();
}
}

Expand Down
42 changes: 38 additions & 4 deletions src/WingetCreateCLI/Commands/DscCommands/BaseDscCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,34 @@ public static List<string> GetAvailableCommands()
/// <returns>True if the command was successful; otherwise, false.</returns>
public abstract bool Schema();

/// <summary>
/// Writes a JSON output line to the console.
/// </summary>
/// <param name="token">The JSON token to be written.</param>
protected static void WriteJsonOutputLine(JToken token)
{
Console.WriteLine(token.ToString(Formatting.None));
}

/// <summary>
/// Writes a JSON output line to the error stream.
/// </summary>
/// <param name="level">The message level.</param>
/// <param name="message">The message to be written.</param>
protected static void WriteMessageOutputLine(DscMessageLevel level, string message)
{
var token = new JObject
{
[GetMessageLevel(level)] = message,
};
Console.Error.WriteLine(token.ToString(Formatting.None));
}

/// <summary>
/// Creates a Json schema for a DSC resource object.
/// </summary>
/// <typeparam name="T">The type of the resource object.</typeparam>
/// <param name="commandName">The name of the command for which the schema is being created.</param>
/// <returns>A Json object representing the schema.</returns>
protected JObject CreateSchema<T>(string commandName)
where T : BaseResourceObject, new()
Expand All @@ -106,11 +130,21 @@ protected JObject CreateSchema<T>(string commandName)
}

/// <summary>
/// Writes a JSON output line to the console.
/// Gets the message level as a string based on the provided DscMessageLevel enum value.
/// </summary>
/// <param name="token">The JSON token to be written.</param>
protected void WriteJsonOutputLine(JToken token)
/// <param name="level">The DscMessageLevel enum value.</param>
/// <returns>A string representation of the message level.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the provided message level is not recognized.</exception>
private static string GetMessageLevel(DscMessageLevel level)
{
Console.WriteLine(token.ToString(Formatting.None));
return level switch
{
DscMessageLevel.Error => "error",
DscMessageLevel.Warning => "warn",
DscMessageLevel.Info => "info",
DscMessageLevel.Debug => "debug",
DscMessageLevel.Trace => "trace",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null),
};
}
}
17 changes: 8 additions & 9 deletions src/WingetCreateCLI/Commands/DscCommands/DscSettingsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Microsoft.WingetCreateCLI.Commands.DscCommands;

using Microsoft.WingetCreateCLI.Logging;
using Microsoft.WingetCreateCLI.Models.DscModels;
using Microsoft.WingetCreateCLI.Properties;
using Newtonsoft.Json.Linq;
Expand All @@ -29,7 +28,7 @@ public override bool Set(JToken input)
{
if (input == null)
{
Logger.ErrorLocalized(nameof(Resources.DscInputRequired_Message), nameof(this.Set));
WriteMessageOutputLine(DscMessageLevel.Error, string.Format(Resources.DscInputRequired_Message, nameof(this.Set)));
return false;
}

Expand All @@ -45,8 +44,8 @@ public override bool Set(JToken input)
data.Set();
}

this.WriteJsonOutputLine(data.Output.ToJson());
this.WriteJsonOutputLine(diff);
WriteJsonOutputLine(data.Output.ToJson());
WriteJsonOutputLine(diff);
return true;
}

Expand All @@ -55,7 +54,7 @@ public override bool Test(JToken input)
{
if (input == null)
{
Logger.ErrorLocalized(nameof(Resources.DscInputRequired_Message), nameof(this.Test));
WriteMessageOutputLine(DscMessageLevel.Error, string.Format(Resources.DscInputRequired_Message, nameof(this.Test)));
return false;
}

Expand All @@ -64,8 +63,8 @@ public override bool Test(JToken input)
data.Get();
data.Output.InDesiredState = data.Test();

this.WriteJsonOutputLine(data.Output.ToJson());
this.WriteJsonOutputLine(data.DiffJson());
WriteJsonOutputLine(data.Output.ToJson());
WriteJsonOutputLine(data.DiffJson());
return true;
}

Expand All @@ -74,14 +73,14 @@ public override bool Export(JToken input)
{
var data = new SettingsFunctionData();
data.Get();
this.WriteJsonOutputLine(data.Output.ToJson());
WriteJsonOutputLine(data.Output.ToJson());
return true;
}

/// <inheritdoc/>
public override bool Schema()
{
this.WriteJsonOutputLine(this.CreateSchema<SettingsResourceObject>(CommandName));
WriteJsonOutputLine(this.CreateSchema<SettingsResourceObject>(CommandName));
return true;
}
}
35 changes: 35 additions & 0 deletions src/WingetCreateCLI/Models/DscModels/DscMessageLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.

namespace Microsoft.WingetCreateCLI.Models.DscModels;

/// <summary>
/// Specifies the severity level of a message.
/// </summary>
public enum DscMessageLevel
{
/// <summary>
/// Represents an error message.
/// </summary>
Error,

/// <summary>
/// Represents a warning message.
/// </summary>
Warning,

/// <summary>
/// Represents an informational message.
/// </summary>
Info,

/// <summary>
/// Represents a debug message.
/// </summary>
Debug,

/// <summary>
/// Represents a trace message.
/// </summary>
Trace,
}
9 changes: 0 additions & 9 deletions src/WingetCreateCLI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src/WingetCreateCLI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1441,9 +1441,6 @@ Warning: Using this argument may result in the token being logged. Consider an a
<data name="DscResourceOperationNotSpecified_Message" xml:space="preserve">
<value>No operation specified. Use --help to see available operations.</value>
</data>
<data name="DscResourceOperationFailed_Message" xml:space="preserve">
<value>DSC resource operation failed</value>
</data>
<data name="DscResourcePropertyDescriptionInDesiredState" xml:space="preserve">
<value>Indicates whether an instance is in the desired state</value>
</data>
Expand Down
4 changes: 0 additions & 4 deletions src/WingetCreateCLI/WingetCreateCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@
<ItemGroup>
<SettingSchema Include="$(ProjectDir)\Schemas\settings.schema.0.1.json" ModelName="Settings" />
</ItemGroup>

<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>

<!--Generate settings manifest-->
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Inputs="@(SettingSchema)" Outputs="@(SettingSchema -> '$(ProjectDir)Models\%(ModelName)Model.cs')">
Expand Down
47 changes: 44 additions & 3 deletions src/WingetCreateTests/WingetCreateTests/Models/DscExecuteResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.WingetCreateUnitTests.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.WingetCreateCLI.Models.DscModels;
using Newtonsoft.Json;

Expand All @@ -18,11 +19,13 @@ public class DscExecuteResult
/// 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 of the command execution.</param>
public DscExecuteResult(bool success, string output)
/// <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>
Expand All @@ -31,10 +34,29 @@ public DscExecuteResult(bool success, string output)
public bool Success { get; }

/// <summary>
/// Gets the output result of the operation.
/// 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>
Expand All @@ -58,4 +80,23 @@ public SettingsResourceObject OutputState()
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"),
};
}
}
Loading