-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCommandLine.cs
More file actions
151 lines (134 loc) · 5.26 KB
/
CommandLine.cs
File metadata and controls
151 lines (134 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Collections.Frozen;
using System.CommandLine;
using System.CommandLine.Parsing;
namespace CreateMatrix;
internal sealed class CommandLine
{
private static readonly Option<FileInfo> s_versionPathOption = new Option<FileInfo>(
"--versionpath"
)
{
Description = "Version JSON file path.",
}.AcceptLegalFilePathsOnly();
private static readonly Option<FileInfo> s_matrixPathOption = new Option<FileInfo>(
"--matrixpath"
)
{
Description = "Matrix JSON file path.",
}.AcceptLegalFilePathsOnly();
private static readonly Option<bool> s_updateVersionOption = new("--updateversion")
{
Description = "Update version information from online sources.",
DefaultValueFactory = _ => false,
};
private static readonly Option<DirectoryInfo> s_makeDirectoryOption = new Option<DirectoryInfo>(
"--makedirectory"
)
{
Description = "Make directory path.",
}.AcceptExistingOnly();
private static readonly Option<DirectoryInfo> s_dockerDirectoryOption =
new Option<DirectoryInfo>("--dockerdirectory")
{
Description = "Docker directory path.",
}.AcceptExistingOnly();
private static readonly Option<VersionInfo.LabelType> s_versionLabelOption = new(
"--versionlabel"
)
{
Description = "Version label to apply.",
DefaultValueFactory = _ => VersionInfo.LabelType.Latest,
};
internal CommandLine(string[] args)
{
Root = CreateRootCommand();
Result = Root.Parse(args, new ParserConfiguration { EnablePosixBundling = false });
Result.InvocationConfiguration.EnableDefaultExceptionHandler = false;
}
private static readonly FrozenSet<string> s_cliBypassList = FrozenSet.Create(
StringComparer.OrdinalIgnoreCase,
"--help",
"--version"
);
internal RootCommand Root { get; }
internal ParseResult Result { get; }
internal static RootCommand CreateRootCommand()
{
RootCommand rootCommand = new(
"CreateMatrix utility to create a matrix of builds from online product versions"
);
rootCommand.Subcommands.Add(CreateVersionCommand());
rootCommand.Subcommands.Add(CreateMatrixCommand());
rootCommand.Subcommands.Add(CreateMakeCommand());
return rootCommand;
}
private static Command CreateVersionCommand()
{
Command versionCommand = new(
"version",
"Create version information file from online sources"
)
{
s_versionPathOption,
};
versionCommand.SetAction(
(parseResult, cancellationToken) =>
new Program(CreateOptions(parseResult), cancellationToken).ExecuteVersionAsync()
);
return versionCommand;
}
private static Command CreateMatrixCommand()
{
Command matrixCommand = new("matrix", "Create matrix information file from online sources")
{
s_versionPathOption.AcceptExistingOnly(),
s_matrixPathOption,
s_updateVersionOption,
};
matrixCommand.SetAction(
(parseResult, cancellationToken) =>
new Program(CreateOptions(parseResult), cancellationToken).ExecuteMatrixAsync()
);
return matrixCommand;
}
private static Command CreateMakeCommand()
{
Command makeCommand = new("make", "Create Docker and Compose files from version file")
{
s_versionPathOption.AcceptExistingOnly(),
s_makeDirectoryOption,
s_dockerDirectoryOption,
s_versionLabelOption,
};
makeCommand.SetAction(
(parseResult, cancellationToken) =>
new Program(CreateOptions(parseResult), cancellationToken).ExecuteMakeAsync()
);
return makeCommand;
}
internal static Options CreateOptions(ParseResult parseResult) =>
new()
{
VersionPath = parseResult.GetValue(s_versionPathOption),
MatrixPath = parseResult.GetValue(s_matrixPathOption),
UpdateVersion = parseResult.GetValue(s_updateVersionOption),
MakeDirectory = parseResult.GetValue(s_makeDirectoryOption),
DockerDirectory = parseResult.GetValue(s_dockerDirectoryOption),
VersionLabel = parseResult.GetValue(s_versionLabelOption),
};
internal static bool BypassStartup(ParseResult parseResult) =>
parseResult.Errors.Count > 0
|| parseResult.CommandResult.Children.Any(symbolResult =>
symbolResult is OptionResult optionResult
&& s_cliBypassList.Contains(optionResult.Option.Name)
);
internal sealed class Options
{
internal required FileInfo? VersionPath { get; init; }
internal required FileInfo? MatrixPath { get; init; }
internal required bool UpdateVersion { get; init; }
internal required DirectoryInfo? MakeDirectory { get; init; }
internal required DirectoryInfo? DockerDirectory { get; init; }
internal required VersionInfo.LabelType VersionLabel { get; init; }
}
}