-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathConfigureCiCommand.cs
93 lines (75 loc) · 3.19 KB
/
ConfigureCiCommand.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
// <copyright file="ConfigureCiCommand.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
using Spectre.Console;
namespace Datadog.Trace.Tools.Runner
{
internal class ConfigureCiCommand : CommandWithExamples
{
private static readonly IReadOnlyDictionary<string, CIName> CiNamesMapping
= new Dictionary<string, CIName>(StringComparer.OrdinalIgnoreCase)
{
["azp"] = CIName.AzurePipelines,
["jenkins"] = CIName.Jenkins,
["github"] = CIName.GithubActions,
};
private readonly ApplicationContext _applicationContext;
private readonly CommonTracerSettings _commonTracerSettings;
private readonly Argument<string> _nameArgument = new("ci-name") { Arity = ArgumentArity.ZeroOrOne };
public ConfigureCiCommand(ApplicationContext applicationContext)
: base("configure", "Set the environment variables for the CI")
{
_applicationContext = applicationContext;
AddArgument(_nameArgument);
_commonTracerSettings = new(this);
AddExample("dd-trace ci configure azp");
AddExample("dd-trace ci configure jenkins");
AddExample("dd-trace ci configure github");
this.SetHandler(ExecuteAsync);
}
private static bool TryExtractCiName(string name, out CIName? ciName)
{
ciName = null;
if (string.IsNullOrEmpty(name))
{
return true;
}
if (CiNamesMapping.TryGetValue(name, out var mappedCiName))
{
ciName = mappedCiName;
return true;
}
Utils.WriteError($"Unsupported CI name: {name}. The supported values are: {string.Join(", ", CiNamesMapping.Keys)}.");
return false;
}
private async Task ExecuteAsync(InvocationContext context)
{
var name = _nameArgument.GetValue(context);
// Initialize and configure CI Visibility for this command
var initResults = await CiUtils.InitializeCiCommandsAsync(_applicationContext, context, _commonTracerSettings, null, string.Empty, [], true).ConfigureAwait(false);
if (!initResults.Success)
{
return;
}
await initResults.UploadRepositoryChangesTask().ConfigureAwait(false);
if (!TryExtractCiName(name, out var ciName))
{
context.ExitCode = 1;
return;
}
AnsiConsole.WriteLine("Setting up the environment variables.");
if (!CIConfiguration.SetupCIEnvironmentVariables(initResults.ProfilerEnvironmentVariables, ciName))
{
context.ExitCode = 1;
return;
}
context.ExitCode = 0;
}
}
}