Skip to content

Commit a5a66f1

Browse files
committed
Update the DockerfileComponentDetector to enable nullability and use the updated utilities to reduce code duplication.
1 parent 0059cf2 commit a5a66f1

1 file changed

Lines changed: 12 additions & 42 deletions

File tree

src/Microsoft.ComponentDetection.Detectors/dockerfile/DockerfileComponentDetector.cs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#nullable disable
1+
#nullable enable
22
namespace Microsoft.ComponentDetection.Detectors.Dockerfile;
33

44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7-
using System.Linq;
8-
using System.Text.RegularExpressions;
97
using System.Threading;
108
using System.Threading.Tasks;
119
using Microsoft.ComponentDetection.Common;
@@ -36,7 +34,7 @@ public DockerfileComponentDetector(
3634

3735
public override string Id { get; } = "DockerReference";
3836

39-
public override IEnumerable<string> Categories => [Enum.GetName(typeof(DetectorClass), DetectorClass.DockerReference)];
37+
public override IEnumerable<string> Categories => [nameof(DetectorClass.DockerReference)];
4038

4139
public override IList<string> SearchPatterns { get; } = ["dockerfile", "dockerfile.*", "*.dockerfile"];
4240

@@ -84,12 +82,12 @@ private Task ParseDockerFileAsync(string fileContents, string fileLocation, ISin
8482
return Task.CompletedTask;
8583
}
8684

87-
private DockerReference ProcessDockerfileConstruct(DockerfileConstruct construct, char escapeChar, Dictionary<string, string> stageNameMap)
85+
private DockerReference? ProcessDockerfileConstruct(DockerfileConstruct construct, char escapeChar, Dictionary<string, string> stageNameMap)
8886
{
8987
try
9088
{
9189
var instructionKeyword = construct.Type;
92-
DockerReference baseImage = null;
90+
DockerReference? baseImage = null;
9391
if (instructionKeyword == ConstructType.Instruction)
9492
{
9593
var constructType = construct.GetType().Name;
@@ -115,10 +113,9 @@ private DockerReference ProcessDockerfileConstruct(DockerfileConstruct construct
115113
}
116114
}
117115

118-
private DockerReference ParseFromInstruction(DockerfileConstruct construct, char escapeChar, Dictionary<string, string> stageNameMap)
116+
private DockerReference? ParseFromInstruction(DockerfileConstruct construct, char escapeChar, Dictionary<string, string> stageNameMap)
119117
{
120-
var tokens = construct.Tokens.ToArray();
121-
var resolvedFromStatement = construct.ResolveVariables(escapeChar).TrimEnd();
118+
var resolvedFromStatement = construct.ResolveVariables(escapeChar)?.TrimEnd();
122119
var fromInstruction = (FromInstruction)construct;
123120
var reference = fromInstruction.ImageName;
124121
if (string.IsNullOrWhiteSpace(resolvedFromStatement) || string.IsNullOrEmpty(reference))
@@ -143,25 +140,15 @@ private DockerReference ParseFromInstruction(DockerfileConstruct construct, char
143140

144141
if (!string.IsNullOrEmpty(stageNameReference))
145142
{
146-
if (this.HasUnresolvedVariables(stageNameReference))
147-
{
148-
return null;
149-
}
150-
151-
return DockerReferenceUtility.ParseFamiliarName(stageNameReference);
152-
}
153-
154-
if (this.HasUnresolvedVariables(reference))
155-
{
156-
return null;
143+
return DockerReferenceUtility.TryParseImageReference(stageNameReference);
157144
}
158145

159-
return DockerReferenceUtility.ParseFamiliarName(reference);
146+
return DockerReferenceUtility.TryParseImageReference(reference);
160147
}
161148

162-
private DockerReference ParseCopyInstruction(DockerfileConstruct construct, char escapeChar, Dictionary<string, string> stageNameMap)
149+
private DockerReference? ParseCopyInstruction(DockerfileConstruct construct, char escapeChar, Dictionary<string, string> stageNameMap)
163150
{
164-
var resolvedCopyStatement = construct.ResolveVariables(escapeChar).TrimEnd();
151+
var resolvedCopyStatement = construct.ResolveVariables(escapeChar)?.TrimEnd();
165152
var copyInstruction = (CopyInstruction)construct;
166153
var reference = copyInstruction.FromStageName;
167154
if (string.IsNullOrWhiteSpace(resolvedCopyStatement) || string.IsNullOrWhiteSpace(reference))
@@ -172,26 +159,9 @@ private DockerReference ParseCopyInstruction(DockerfileConstruct construct, char
172159
stageNameMap.TryGetValue(reference, out var stageNameReference);
173160
if (!string.IsNullOrEmpty(stageNameReference))
174161
{
175-
if (this.HasUnresolvedVariables(stageNameReference))
176-
{
177-
return null;
178-
}
179-
else
180-
{
181-
return DockerReferenceUtility.ParseFamiliarName(stageNameReference);
182-
}
162+
return DockerReferenceUtility.TryParseImageReference(stageNameReference);
183163
}
184164

185-
if (this.HasUnresolvedVariables(reference))
186-
{
187-
return null;
188-
}
189-
190-
return DockerReferenceUtility.ParseFamiliarName(reference);
191-
}
192-
193-
private bool HasUnresolvedVariables(string reference)
194-
{
195-
return new Regex("[${}]").IsMatch(reference);
165+
return DockerReferenceUtility.TryParseImageReference(reference);
196166
}
197167
}

0 commit comments

Comments
 (0)