Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json output for the kiota info command #3531

Merged
merged 4 commits into from
Oct 20, 2023
Merged
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
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.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, I just noticed this, I need a better description for this.

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