-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
173 lines (150 loc) · 6.58 KB
/
Program.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Autofac;
using BbGit.BitBucket;
using BbGit.ConsoleApp;
using BbGit.ConsoleUtils;
using BbGit.Git;
using Bitbucket.Net;
using CommandDotNet;
using CommandDotNet.DataAnnotations;
using CommandDotNet.Diagnostics;
using CommandDotNet.Execution;
using CommandDotNet.IoC.Autofac;
using CommandDotNet.NameCasing;
using CommandDotNet.TypeDescriptors;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
using Spectre.Console;
using MiddlewareSteps = CommandDotNet.Execution.MiddlewareSteps;
namespace BbGit
{
internal static class Program
{
private static int Main(string[] args)
{
Debugger.AttachIfDebugDirective(args);
AppDomain.CurrentDomain.UnhandledException += (_, eventArgs)
=> ((Exception)eventArgs.ExceptionObject).Print();
try
{
var configs = AppConfigs.Load();
var appSettings = new AppSettings
{
Arguments = {DefaultOptionSplit = ',', DefaultPipeTargetSymbol = "$*"},
ArgumentTypeDescriptors =
{
new DelegatedTypeDescriptor<Regex>("regex", p =>
{
// make it easier to specify NOT matching
if (p.StartsWith("$!"))
{
p = $"^((?!{p.Substring(2)}).)*$";
}
return new Regex(p, RegexOptions.Compiled);
})
}
};
var appRunner = new AppRunner<GitApplication>(appSettings)
.UseDefaultMiddleware()
.GiveCancellationTokenToFlurl()
.Configure(c => c.UseParameterResolver(_ => AnsiConsole.Console))
.UseNameCasing(Case.KebabCase)
.UseDataAnnotationValidations(showHelpOnError: true)
.UseTimerDirective()
.UseDefaultsFromAppSetting(configs.Settings, true)
.UseErrorHandler((ctx, ex) =>
{
var errorWriter = (ctx?.Console.Error ?? Console.Error);
ex.Print(errorWriter.WriteLine,
includeProperties: true,
includeData: true,
includeStackTrace: false);
// use CommandLogger if it has not already logged for this CommandContext
if (ctx is not null && !CommandLogger.HasLoggedFor(ctx))
{
CommandLogger.Log(ctx,
writer: errorWriter.WriteLine,
includeSystemInfo: true,
includeAppConfig: false
);
errorWriter.WriteLine();
}
// print help for the target command or root command
// if the exception occurred before a command could be parsed
ctx?.PrintHelp();
return ExitCodes.Error.Result;
})
.UseCommandLogger()
.RegisterContainer(configs);
return appRunner.Run(args);
}
catch (OperationCanceledException)
{
return 1;
}
catch (Exception e)
{
e.Print();
return 1;
}
}
private class CommandContextHolder
{
public CommandContext Context { get; set; } = null!;
}
private static AppRunner RegisterContainer(this AppRunner appRunner, AppConfigs configs)
{
var config = configs.Default ?? new AppConfig();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterInstance(configs);
containerBuilder.RegisterInstance(config);
containerBuilder.RegisterType<BitBucketRepoCommand>().InstancePerLifetimeScope();
containerBuilder.RegisterType<GlobalConfigCommand>().InstancePerLifetimeScope();
containerBuilder.RegisterType<LocalRepoCommand>().InstancePerLifetimeScope();
containerBuilder.RegisterType<BbService>().InstancePerLifetimeScope();
containerBuilder.RegisterType<GitService>().InstancePerLifetimeScope();
containerBuilder.RegisterServerBbApi(config);
containerBuilder.RegisterType<CommandContextHolder>().InstancePerLifetimeScope();
containerBuilder.Register(c => c.Resolve<CommandContextHolder>().Context)
.As<CommandContext>()
.InstancePerLifetimeScope();
containerBuilder
.Register(c => c.Resolve<CommandContext>().Console)
.As<IConsole>()
.InstancePerLifetimeScope();
appRunner.Configure(r => r.UseMiddleware(
SetCommandContextForDependencyResolver,
MiddlewareSteps.DependencyResolver.BeginScope + 1));
return appRunner.UseAutofac(containerBuilder.Build());
}
private static Task<int> SetCommandContextForDependencyResolver(CommandContext context, ExecutionDelegate next)
{
var holder = (CommandContextHolder?)context!.DependencyResolver!.Resolve(typeof(CommandContextHolder));
if (holder is not null)
{
holder.Context = context;
}
return next(context);
}
private static void RegisterServerBbApi(this ContainerBuilder containerBuilder, AppConfig config)
{
BitbucketClient bbClient;
switch (config.AuthType)
{
case AppConfig.AuthTypes.Basic:
bbClient = new BitbucketClient(config.BaseUrl, config.Username, config.AppPassword);
break;
case AppConfig.AuthTypes.OAuth:
bbClient = new BitbucketClient(config.BaseUrl, () => config.AppPassword);
break;
default:
throw new ArgumentOutOfRangeException();
}
containerBuilder.RegisterInstance<CredentialsHandler>((url, fromUrl, types) =>
new UsernamePasswordCredentials { Username = config.Username, Password = config.AppPassword });
containerBuilder.RegisterInstance(bbClient);
}
}
}