-
Notifications
You must be signed in to change notification settings - Fork 46
/
ImageBuilder.cs
85 lines (74 loc) · 3.11 KB
/
ImageBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.CommandLine;
using System.CommandLine.Binding;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.DotNet.ImageBuilder.Commands;
using ICommand = Microsoft.DotNet.ImageBuilder.Commands.ICommand;
namespace Microsoft.DotNet.ImageBuilder
{
public static class ImageBuilder
{
private static CompositionContainer s_container;
private static CompositionContainer Container
{
get
{
if (s_container == null)
{
string dllLocation = Assembly.GetExecutingAssembly().Location;
DirectoryCatalog catalog = new(Path.GetDirectoryName(dllLocation), Path.GetFileName(dllLocation));
s_container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
}
return s_container;
}
}
public static ICommand[] Commands => Container.GetExportedValues<ICommand>().ToArray();
public static int Main(string[] args)
{
int result = 0;
try
{
RootCommand rootCliCommand = new();
foreach (ICommand command in Commands)
{
rootCliCommand.AddCommand(command.GetCliCommand());
}
Parser parser = new CommandLineBuilder(rootCliCommand)
.UseDefaults()
.UseMiddleware(context =>
{
if (context.ParseResult.CommandResult.Command != rootCliCommand)
{
// Capture the Docker version and info in the output.
ExecuteHelper.Execute(fileName: "docker", args: "version", isDryRun: false);
ExecuteHelper.Execute(fileName: "docker", args: "info", isDryRun: false);
}
})
.UseMiddleware(context =>
{
context.BindingContext.AddModelBinder(new ModelBinder<AzdoOptions>());
context.BindingContext.AddModelBinder(new ModelBinder<GitOptions>());
context.BindingContext.AddModelBinder(new ModelBinder<ManifestFilterOptions>());
context.BindingContext.AddModelBinder(new ModelBinder<RegistryCredentialsOptions>());
context.BindingContext.AddModelBinder(new ModelBinder<SubscriptionOptions>());
})
.Build();
return parser.Invoke(args);
}
catch (Exception e)
{
Logger.WriteError(e.ToString());
result = 1;
}
return result;
}
}
}