Skip to content

Commit

Permalink
Merge pull request #3531 from andreaTP/version-json-output
Browse files Browse the repository at this point in the history
Json output for the `kiota info` command
  • Loading branch information
baywet authored Oct 20, 2023
2 parents 5decc87 + 4f295d3 commit c6a4c0d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for multiple content type request bodies. [#3377](https://github.com/microsoft/kiota/issues/3377)
- Added support for multiple content type responses. [#3377](https://github.com/microsoft/kiota/issues/3377)
- Support for primary error message in Python [#3277](https://github.com/microsoft/kiota/issues/3277)
- Added a json output for the `kiota info` command

### Changed

Expand Down
29 changes: 23 additions & 6 deletions src/kiota/Handlers/KiotaInfoCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using System.CommandLine.IO;
using System.CommandLine.Rendering;
using System.CommandLine.Rendering.Views;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Kiota.Builder;
using Kiota.Builder.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Writers;

namespace kiota.Handlers;
internal class KiotaInfoCommandHandler : KiotaSearchBasedCommandHandler
Expand Down Expand Up @@ -39,6 +41,10 @@ public required Option<string> ManifestOption
{
get; init;
}
public required Option<bool> JsonOption
{
get; init;
}

public override async Task<int> InvokeAsync(InvocationContext context)
{
Expand All @@ -47,6 +53,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
bool clearCache = context.ParseResult.GetValueForOption(ClearCacheOption);
string searchTerm = context.ParseResult.GetValueForOption(SearchTermOption) ?? string.Empty;
string version = context.ParseResult.GetValueForOption(VersionOption) ?? string.Empty;
bool json = context.ParseResult.GetValueForOption(JsonOption);
GenerationLanguage? language = context.ParseResult.GetValueForOption(GenerationLanguage);
CancellationToken cancellationToken = context.BindingContext.GetService(typeof(CancellationToken)) is CancellationToken token ? token : CancellationToken.None;
var (loggerFactory, logger) = GetLoggerAndFactory<KiotaBuilder>(context);
Expand Down Expand Up @@ -94,7 +101,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
return 1;
#endif
}
ShowLanguageInformation(language.Value, instructions);
ShowLanguageInformation(language.Value, instructions, json);
return 0;
}
}
Expand All @@ -112,15 +119,25 @@ private void ShowLanguagesTable()
var layout = new StackLayoutView { view };
console.Append(layout);
}
private void ShowLanguageInformation(GenerationLanguage language, LanguagesInformation informationSource)
private void ShowLanguageInformation(GenerationLanguage language, LanguagesInformation informationSource, bool json)
{
if (informationSource.TryGetValue(language.ToString(), out var languageInformation))
{
DisplayInfo($"The language {language} is currently in {languageInformation.MaturityLevel} maturity level.",
"After generating code for this language, you need to install the following packages:");
foreach (var dependency in languageInformation.Dependencies)
if (!json)
{
DisplayInfo($"The language {language} is currently in {languageInformation.MaturityLevel} maturity level.",
"After generating code for this language, you need to install the following packages:");
foreach (var dependency in languageInformation.Dependencies)
{
DisplayInfo(string.Format(dependency.Name, dependency.Version));
}
}
else
{
DisplayInfo(string.Format(languageInformation.DependencyInstallCommand, dependency.Name, dependency.Version));
using TextWriter sWriter = new StringWriter();
OpenApiJsonWriter writer = new(sWriter);
languageInformation.SerializeAsV3(writer);
DisplayInfo(sWriter.ToString()!);
}
}
else
Expand Down
3 changes: 3 additions & 0 deletions src/kiota/KiotaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private static Command GetInfoCommand()
var clearCacheOption = GetClearCacheOption(defaultGenerationConfiguration.ClearCache);
var searchTermOption = GetSearchKeyOption();
var languageOption = new Option<GenerationLanguage?>("--language", "The target language for the dependencies instructions.");
var jsonOption = new Option<bool>("--json", "The target language for the dependencies instructions.");
languageOption.AddAlias("-l");
AddEnumValidator(languageOption, "language");
var infoCommand = new Command("info", "Displays information about the languages supported by kiota and dependencies to add in your project.") {
Expand All @@ -111,6 +112,7 @@ private static Command GetInfoCommand()
clearCacheOption,
searchTermOption,
languageOption,
jsonOption,
};
infoCommand.Handler = new KiotaInfoCommandHandler
{
Expand All @@ -121,6 +123,7 @@ private static Command GetInfoCommand()
ClearCacheOption = clearCacheOption,
SearchTermOption = searchTermOption,
GenerationLanguage = languageOption,
JsonOption = jsonOption,
};
return infoCommand;
}
Expand Down

0 comments on commit c6a4c0d

Please sign in to comment.