|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.CommandLine; |
| 7 | +using System.CommandLine.Parsing; |
7 | 8 | using Microsoft.VisualStudio.Services.Common; |
8 | | -using static Microsoft.DotNet.ImageBuilder.Commands.CliHelper; |
9 | 9 |
|
10 | | -namespace Microsoft.DotNet.ImageBuilder.Commands |
| 10 | +namespace Microsoft.DotNet.ImageBuilder.Commands; |
| 11 | + |
| 12 | +public class AzdoOptions |
11 | 13 | { |
12 | | - public class AzdoOptions |
| 14 | + public string AccessToken { get; set; } = string.Empty; |
| 15 | + |
| 16 | + public string Organization { get; set; } = string.Empty; |
| 17 | + |
| 18 | + public string Project { get; set; } = string.Empty; |
| 19 | + |
| 20 | + public string? AzdoRepo { get; set; } |
| 21 | + |
| 22 | + public string? AzdoBranch { get; set; } |
| 23 | + |
| 24 | + public string? AzdoPath { get; set; } |
| 25 | + |
| 26 | + private static readonly Argument<string> AccessTokenArgument = new(nameof(AccessToken)) |
13 | 27 | { |
14 | | - public string AccessToken { get; set; } = string.Empty; |
| 28 | + Description = "Azure DevOps PAT" |
| 29 | + }; |
15 | 30 |
|
16 | | - public string Organization { get; set; } = string.Empty; |
| 31 | + private static readonly Argument<string> OrganizationArgument = new(nameof(Organization)) |
| 32 | + { |
| 33 | + Description = "Azure DevOps organization" |
| 34 | + }; |
17 | 35 |
|
18 | | - public string Project { get; set; } = string.Empty; |
| 36 | + private static readonly Argument<string> ProjectArgument = new(nameof(Project)) |
| 37 | + { |
| 38 | + Description = "Azure DevOps project" |
| 39 | + }; |
19 | 40 |
|
20 | | - public string? AzdoRepo { get; set; } |
| 41 | + private static readonly Option<string?> AzdoRepoOption = new(CliHelper.FormatAlias("azdo-repo")) |
| 42 | + { |
| 43 | + Description = "Azure DevOps repo" |
| 44 | + }; |
21 | 45 |
|
22 | | - public string? AzdoBranch { get; set; } |
| 46 | + private static readonly Option<string?> AzdoBranchOption = new(CliHelper.FormatAlias("azdo-branch")) |
| 47 | + { |
| 48 | + Description = "Azure DevOps branch (default: main)", |
| 49 | + DefaultValueFactory = _ => "main" |
| 50 | + }; |
23 | 51 |
|
24 | | - public string? AzdoPath { get; set; } |
| 52 | + private static readonly Option<string?> AzdoPathOption = new(CliHelper.FormatAlias("azdo-path")) |
| 53 | + { |
| 54 | + Description = "Azure DevOps path" |
| 55 | + }; |
25 | 56 |
|
26 | | - public (Uri BaseUrl, VssCredentials Credentials) GetConnectionDetails() => |
27 | | - (new Uri($"https://dev.azure.com/{Organization}"), new VssBasicCredential(string.Empty, AccessToken)); |
28 | | - } |
| 57 | + public IEnumerable<Argument> GetCliArguments() => |
| 58 | + [AccessTokenArgument, OrganizationArgument, ProjectArgument]; |
| 59 | + |
| 60 | + public IEnumerable<Option> GetCliOptions() => |
| 61 | + [AzdoRepoOption, AzdoBranchOption, AzdoPathOption]; |
29 | 62 |
|
30 | | - public class AzdoOptionsBuilder |
| 63 | + public void Bind(ParseResult result) |
31 | 64 | { |
32 | | - public IEnumerable<Argument> GetCliArguments() => |
33 | | - new Argument[] |
34 | | - { |
35 | | - new Argument<string>(nameof(AzdoOptions.AccessToken), "Azure DevOps PAT"), |
36 | | - new Argument<string>(nameof(AzdoOptions.Organization), "Azure DevOps organization"), |
37 | | - new Argument<string>(nameof(AzdoOptions.Project), "Azure DevOps project") |
38 | | - }; |
39 | | - |
40 | | - public IEnumerable<Option> GetCliOptions() => |
41 | | - new Option[] |
42 | | - { |
43 | | - CreateOption<string?>("azdo-repo", nameof(AzdoOptions.AzdoRepo), "Azure DevOps repo"), |
44 | | - CreateOption<string?>("azdo-branch", nameof(AzdoOptions.AzdoBranch), "Azure DevOps branch (default: main)", "main"), |
45 | | - CreateOption<string?>("azdo-path", nameof(AzdoOptions.AzdoPath), "Azure DevOps path"), |
46 | | - }; |
| 65 | + AccessToken = result.GetValue(AccessTokenArgument) ?? string.Empty; |
| 66 | + Organization = result.GetValue(OrganizationArgument) ?? string.Empty; |
| 67 | + Project = result.GetValue(ProjectArgument) ?? string.Empty; |
| 68 | + AzdoRepo = result.GetValue(AzdoRepoOption); |
| 69 | + AzdoBranch = result.GetValue(AzdoBranchOption); |
| 70 | + AzdoPath = result.GetValue(AzdoPathOption); |
47 | 71 | } |
| 72 | + |
| 73 | + public (Uri BaseUrl, VssCredentials Credentials) GetConnectionDetails() => |
| 74 | + (new Uri($"https://dev.azure.com/{Organization}"), new VssBasicCredential(string.Empty, AccessToken)); |
48 | 75 | } |
0 commit comments