1
- using Octokit ;
2
- using Spectre . Console ;
3
- using System . CommandLine ;
4
- using System . CommandLine . Parsing ;
1
+ using System . CommandLine ;
5
2
using System . Diagnostics . CodeAnalysis ;
3
+ using Octokit ;
4
+ using Spectre . Console ;
6
5
7
6
namespace RepoMan ;
8
7
@@ -12,123 +11,143 @@ private static async Task<int> Main(string[] args)
12
11
{
13
12
bool runApp = false ;
14
13
15
- Argument < string ? > inputFile = new (
16
- name : "file" ,
17
- description : "Path to the RepoMan config file to process. Can be a local file or URL." ,
18
- isDefault : false ,
19
- parse : result => {
20
- if ( result . Tokens . Count == 0 )
21
- {
22
- result . ErrorMessage = "Invalid value passed to input parameter" ;
23
- return default ;
24
- }
14
+ Argument < string ? > inputFile = new ( "file" )
15
+ {
16
+ Description = "Path to the RepoMan config file to process. Can be a local file or URL." ,
17
+ CustomParser = result =>
18
+ {
19
+ if ( result . Tokens . Count == 0 )
20
+ {
21
+ result . AddError ( "Invalid value passed to input parameter" ) ;
22
+ return default ;
23
+ }
25
24
26
- string value = result . Tokens . Single ( ) . Value ;
25
+ string value = result . Tokens . Single ( ) . Value ;
27
26
28
- if ( Path . Exists ( value ) )
29
- return value ;
27
+ if ( Path . Exists ( value ) )
28
+ return value ;
30
29
31
- result . ErrorMessage = $ "Path is invalid: { value } ";
30
+ result . AddError ( $ "Path is invalid: { value } ") ;
32
31
33
- return null ;
34
- } ) ;
32
+ return null ;
33
+ }
34
+ } ;
35
35
36
- Argument < Uri ? > httpLink = new (
37
- name : "httpLink" ,
38
- description : "A URL to a rules file." ,
39
- isDefault : false ,
40
- parse : result => {
41
- if ( result . Tokens . Count == 0 )
42
- {
43
- result . ErrorMessage = "Invalid value passed to httpLink parameter" ;
44
- return null ;
45
- }
36
+ Argument < Uri ? > httpLink = new ( "httpLink" )
37
+ {
38
+ Description = "A URL to a rules file." ,
39
+ CustomParser = result =>
40
+ {
41
+ if ( result . Tokens . Count == 0 )
42
+ {
43
+ result . AddError ( "Invalid value passed to httpLink parameter" ) ;
44
+ return null ;
45
+ }
46
46
47
- string value = result . Tokens . Single ( ) . Value ;
47
+ string value = result . Tokens . Single ( ) . Value ;
48
48
49
- if ( Uri . TryCreate ( value , UriKind . Absolute , out Uri ? webUri ) )
50
- return webUri ;
49
+ if ( Uri . TryCreate ( value , UriKind . Absolute , out Uri ? webUri ) )
50
+ return webUri ;
51
51
52
- result . ErrorMessage = $ "URL is invalid: { value } ";
52
+ result . AddError ( $ "URL is invalid: { value } ") ;
53
53
54
- return null ;
55
- } ) ;
54
+ return null ;
55
+ }
56
+ } ;
56
57
57
- Argument < string ? > githubRepo = new (
58
- name : "repository" ,
59
- description : "The name of a GitHub repository." ,
60
- isDefault : false ,
61
- parse : result => {
62
- if ( result . Tokens . Count == 0 )
63
- {
64
- result . ErrorMessage = "Invalid value passed to repository parameter" ;
65
- return null ;
66
- }
58
+ Argument < string ? > githubRepo = new ( "repository" )
59
+ {
60
+ Description = "The name of a GitHub repository." ,
61
+ CustomParser = result =>
62
+ {
63
+ if ( result . Tokens . Count == 0 )
64
+ {
65
+ result . AddError ( "Invalid value passed to repository parameter" ) ;
66
+ return null ;
67
+ }
67
68
68
- string value = result . Tokens . Single ( ) . Value . Trim ( ) ;
69
+ string value = result . Tokens . Single ( ) . Value . Trim ( ) ;
69
70
70
- if ( ! string . IsNullOrEmpty ( value ) )
71
- return value ;
71
+ if ( ! string . IsNullOrEmpty ( value ) )
72
+ return value ;
72
73
73
- result . ErrorMessage = $ "Repository is invalid: { value } ";
74
+ result . AddError ( $ "Repository is invalid: { value } ") ;
74
75
75
- return null ;
76
- } ) ;
76
+ return null ;
77
+ }
78
+ } ;
77
79
78
- Argument < string ? > githubOwner = new (
79
- name : "owner" ,
80
- description : "The owner name of the GitHub repository." ,
81
- isDefault : false ,
82
- parse : result => {
83
- if ( result . Tokens . Count == 0 )
84
- {
85
- result . ErrorMessage = "Invalid value passed to owner parameter" ;
86
- return null ;
87
- }
80
+ Argument < string ? > githubOwner = new ( "owner" )
81
+ {
82
+ Description = "The owner name of the GitHub repository." ,
83
+ CustomParser = result =>
84
+ {
85
+ if ( result . Tokens . Count == 0 )
86
+ {
87
+ result . AddError ( "Invalid value passed to owner parameter" ) ;
88
+ return null ;
89
+ }
88
90
89
- string value = result . Tokens . Single ( ) . Value . Trim ( ) ;
91
+ string value = result . Tokens . Single ( ) . Value . Trim ( ) ;
90
92
91
- if ( ! string . IsNullOrEmpty ( value ) )
92
- return value ;
93
+ if ( ! string . IsNullOrEmpty ( value ) )
94
+ return value ;
93
95
94
- result . ErrorMessage = $ "Owner is invalid: { value } ";
96
+ result . AddError ( $ "Owner is invalid: { value } ") ;
95
97
96
- return null ;
97
- } ) ;
98
+ return null ;
99
+ }
100
+ } ;
98
101
99
102
RootCommand rootCommand = new ( "Tests and validates RepoMan config files." ) ;
100
103
101
104
// CHECK command
102
105
Command checkCommand = new ( "check" , "Validates a RepoMan config file" ) ;
103
- checkCommand . AddAlias ( "validate" ) ;
106
+ checkCommand . Aliases . Add ( "validate" ) ;
104
107
105
108
Command checkFileCommand = new ( "file" , "Loads the config from a local file." ) ;
106
- checkFileCommand . AddArgument ( inputFile ) ;
107
- checkFileCommand . SetHandler ( CommandCheck . HandlerFile , inputFile ) ;
108
- checkCommand . AddCommand ( checkFileCommand ) ;
109
+ checkFileCommand . Arguments . Add ( inputFile ) ;
110
+ checkFileCommand . SetAction ( ( ParseResult parseResult , CancellationToken token ) =>
111
+ {
112
+ CommandCheck . HandlerFile ( parseResult . GetValue ( inputFile ) ) ;
113
+ return Task . CompletedTask ;
114
+ } ) ;
115
+ checkCommand . Subcommands . Add ( checkFileCommand ) ;
109
116
110
117
Command checkHttpCommand = new ( "http" , "Loads the config from a URL." ) ;
111
- checkHttpCommand . AddArgument ( httpLink ) ;
112
- checkHttpCommand . SetHandler ( CommandCheck . HandlerHttp , httpLink ) ;
113
- checkCommand . AddCommand ( checkHttpCommand ) ;
118
+ checkHttpCommand . Arguments . Add ( httpLink ) ;
119
+ checkHttpCommand . SetAction ( ( ParseResult parseResult , CancellationToken token ) =>
120
+ {
121
+ CommandCheck . HandlerHttp ( parseResult . GetValue ( httpLink ) ) ;
122
+ return Task . CompletedTask ;
123
+ } ) ;
124
+ checkCommand . Subcommands . Add ( checkHttpCommand ) ;
114
125
115
126
Command checkGithubCommand = new ( "github" , "Loads the config from a GitHub repository." ) ;
116
- checkGithubCommand . AddArgument ( githubOwner ) ;
117
- checkGithubCommand . AddArgument ( githubRepo ) ;
118
- checkGithubCommand . SetHandler ( CommandCheck . HandlerGithub , githubOwner , githubRepo ) ;
119
- checkCommand . AddCommand ( checkGithubCommand ) ;
127
+ checkGithubCommand . Arguments . Add ( githubOwner ) ;
128
+ checkGithubCommand . Arguments . Add ( githubRepo ) ;
129
+ checkGithubCommand . SetAction ( ( ParseResult parseResult , CancellationToken token ) =>
130
+ {
131
+ CommandCheck . HandlerGithub ( parseResult . GetValue ( githubOwner ) , parseResult . GetValue ( githubRepo ) ) ;
132
+ return Task . CompletedTask ;
133
+ } ) ;
134
+ checkCommand . Subcommands . Add ( checkGithubCommand ) ;
120
135
121
- rootCommand . AddCommand ( checkCommand ) ;
136
+ rootCommand . Subcommands . Add ( checkCommand ) ;
122
137
123
138
// RUN command
124
139
Command runCommand = new ( "run" , "Processes a RepoMan config file in GitHub and simulate an action." ) ;
125
- runCommand . AddArgument ( githubRepo ) ;
126
- runCommand . AddArgument ( githubOwner ) ;
127
- runCommand . SetHandler ( CommandRun . HandlerGithub , githubRepo , githubOwner ) ;
140
+ runCommand . Arguments . Add ( githubRepo ) ;
141
+ runCommand . Arguments . Add ( githubOwner ) ;
142
+ runCommand . SetAction ( ( ParseResult parseResult , CancellationToken token ) =>
143
+ {
144
+ CommandRun . HandlerGithub ( parseResult . GetValue ( githubRepo ) , parseResult . GetValue ( githubOwner ) ) ;
145
+ return Task . CompletedTask ;
146
+ } ) ;
128
147
129
- rootCommand . AddCommand ( runCommand ) ;
148
+ rootCommand . Subcommands . Add ( runCommand ) ;
130
149
131
- return await rootCommand . InvokeAsync ( args ) ;
150
+ return await rootCommand . Parse ( args ) . InvokeAsync ( ) ;
132
151
}
133
152
134
153
internal static bool TryReadGithubContent ( string owner , string repository , State state , [ NotNullWhen ( true ) ] out string ? content )
@@ -265,4 +284,4 @@ internal static bool ReadFileContentIntoObject(string content, State state)
265
284
266
285
return grid . Width ;
267
286
}
268
- }
287
+ }
0 commit comments