-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathLinuxComponent.cs
More file actions
125 lines (101 loc) · 3.5 KB
/
LinuxComponent.cs
File metadata and controls
125 lines (101 loc) · 3.5 KB
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
#nullable disable
namespace Microsoft.ComponentDetection.Contracts.TypedComponent;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using PackageUrl;
public class LinuxComponent : TypedComponent
{
public LinuxComponent()
{
/* Reserved for deserialization */
}
public LinuxComponent(string distribution, string release, string name, string version, string license = null, string author = null)
{
this.Distribution = this.ValidateRequiredInput(distribution, nameof(this.Distribution), nameof(ComponentType.Linux));
this.Release = this.ValidateRequiredInput(release, nameof(this.Release), nameof(ComponentType.Linux));
this.Name = this.ValidateRequiredInput(name, nameof(this.Name), nameof(ComponentType.Linux));
this.Version = this.ValidateRequiredInput(version, nameof(this.Version), nameof(ComponentType.Linux));
this.License = license;
this.Author = author;
}
[JsonPropertyName("distribution")]
public string Distribution { get; set; }
[JsonPropertyName("release")]
public string Release { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("version")]
public string Version { get; set; }
#nullable enable
[JsonPropertyName("license")]
public string? License { get; set; }
[JsonPropertyName("author")]
public string? Author { get; set; }
#nullable disable
[JsonIgnore]
public override ComponentType Type => ComponentType.Linux;
[JsonPropertyName("packageUrl")]
public override PackageUrl PackageUrl
{
get
{
string packageType = null;
if (this.IsUbuntu() || this.IsDebian())
{
packageType = "deb";
}
else if (this.IsCentOS() || this.IsFedora() || this.IsRHEL())
{
packageType = "rpm";
}
else if (this.IsAlpine())
{
packageType = "apk";
}
if (packageType != null)
{
var distroId = this.GetDistroId();
var qualifiers = new SortedDictionary<string, string>
{
{ "distro", $"{distroId}-{this.Release}" },
};
return new PackageUrl(packageType, distroId, this.Name, this.Version, qualifiers, null);
}
return null;
}
}
protected override string ComputeBaseId() => $"{this.Distribution} {this.Release} {this.Name} {this.Version} - {this.Type}";
private bool IsUbuntu()
{
return this.Distribution.Equals("UBUNTU", StringComparison.OrdinalIgnoreCase);
}
private bool IsDebian()
{
return this.Distribution.Equals("DEBIAN", StringComparison.OrdinalIgnoreCase);
}
private bool IsCentOS()
{
return this.Distribution.Equals("CENTOS", StringComparison.OrdinalIgnoreCase);
}
private bool IsFedora()
{
return this.Distribution.Equals("FEDORA", StringComparison.OrdinalIgnoreCase);
}
private bool IsRHEL()
{
return this.Distribution.Equals("RED HAT ENTERPRISE LINUX", StringComparison.OrdinalIgnoreCase);
}
private bool IsAlpine()
{
return this.Distribution.Equals("ALPINE", StringComparison.OrdinalIgnoreCase);
}
private string GetDistroId()
{
if (this.IsRHEL())
{
return "redhat";
}
return this.Distribution.ToLowerInvariant();
}
}