|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.DotNet.Darc.Options; |
| 7 | +using Microsoft.DotNet.DarcLib; |
| 8 | +using Microsoft.DotNet.Maestro.Client; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Newtonsoft.Json; |
| 12 | + |
| 13 | +namespace Microsoft.DotNet.Darc.Operations; |
| 14 | + |
| 15 | +internal class GetChannelOperation : Operation |
| 16 | +{ |
| 17 | + private readonly GetChannelCommandLineOptions _options; |
| 18 | + public GetChannelOperation(GetChannelCommandLineOptions options) |
| 19 | + : base(options) |
| 20 | + { |
| 21 | + _options = options; |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Retrieve information about a specific channel |
| 26 | + /// </summary> |
| 27 | + /// <param name="options">Command line options</param> |
| 28 | + /// <returns>Process exit code.</returns> |
| 29 | + public override async Task<int> ExecuteAsync() |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + IBarApiClient barClient = Provider.GetRequiredService<IBarApiClient>(); |
| 34 | + |
| 35 | + var channel = await barClient.GetChannelAsync(_options.Id); |
| 36 | + |
| 37 | + if (channel == null) |
| 38 | + { |
| 39 | + Logger.LogError("Channel with id {channelId} not found", _options.Id); |
| 40 | + return Constants.ErrorCode; |
| 41 | + } |
| 42 | + |
| 43 | + switch (_options.OutputFormat) |
| 44 | + { |
| 45 | + case DarcOutputType.json: |
| 46 | + Console.WriteLine(JsonConvert.SerializeObject(channel, Formatting.Indented)); |
| 47 | + break; |
| 48 | + case DarcOutputType.text: |
| 49 | + Console.WriteLine($"({channel.Id}) {channel.Name}"); |
| 50 | + break; |
| 51 | + default: |
| 52 | + throw new NotImplementedException($"Output format {_options.OutputFormat} not supported for get-channel"); |
| 53 | + } |
| 54 | + |
| 55 | + return Constants.SuccessCode; |
| 56 | + } |
| 57 | + catch (AuthenticationException e) |
| 58 | + { |
| 59 | + Console.WriteLine(e.Message); |
| 60 | + return Constants.ErrorCode; |
| 61 | + } |
| 62 | + catch (Exception e) |
| 63 | + { |
| 64 | + Logger.LogError(e, "Error: Failed to retrieve the channel"); |
| 65 | + return Constants.ErrorCode; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected override bool IsOutputFormatSupported(DarcOutputType outputFormat) |
| 70 | + => outputFormat switch |
| 71 | + { |
| 72 | + DarcOutputType.json => true, |
| 73 | + _ => base.IsOutputFormatSupported(outputFormat), |
| 74 | + }; |
| 75 | +} |
0 commit comments