Skip to content

Commit 6ab75b4

Browse files
authored
Add the darc get-channel command (#3584)
1 parent 1dafa0b commit 6ab75b4

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 CommandLine;
5+
using Microsoft.DotNet.Darc.Operations;
6+
7+
namespace Microsoft.DotNet.Darc.Options;
8+
9+
[Verb("get-channel", HelpText = "Get a specific channel.")]
10+
internal class GetChannelCommandLineOptions : CommandLineOptions
11+
{
12+
[Option("id", Required = true, HelpText = "ID of the channel to show.")]
13+
public int Id { get; set; }
14+
15+
public override Operation GetOperation()
16+
{
17+
return new GetChannelOperation(this);
18+
}
19+
}

src/Microsoft.DotNet.Darc/Darc/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private static Type[] GetOptions() =>
8787
typeof(GatherDropCommandLineOptions),
8888
typeof(GetAssetCommandLineOptions),
8989
typeof(GetBuildCommandLineOptions),
90+
typeof(GetChannelCommandLineOptions),
9091
typeof(GetChannelsCommandLineOptions),
9192
typeof(GetDefaultChannelsCommandLineOptions),
9293
typeof(GetDependenciesCommandLineOptions),

0 commit comments

Comments
 (0)