Skip to content

Commit 077dc30

Browse files
Copilotalerickson
andcommitted
Make InstallPkgParams properties nullable to distinguish unset from false
This addresses the feedback to check if reqResourceParams properties are null. Now the code can distinguish between: - User didn't specify the property (null) -> use cmdlet-level parameter - User explicitly set the property to true/false -> use that value Changed properties to nullable (bool?, ScopeType?) and updated logic to only set them when explicitly provided in the RequiredResource hashtable. Co-authored-by: alerickson <25858831+alerickson@users.noreply.github.com>
1 parent 449530a commit 077dc30

2 files changed

Lines changed: 41 additions & 29 deletions

File tree

src/code/InstallPSResource.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ private void RequiredResourceHelper(Hashtable reqResourceHash)
487487
}
488488
}
489489

490-
if (pkgParams.Scope == ScopeType.AllUsers)
490+
if (pkgParams.Scope.HasValue && pkgParams.Scope.Value == ScopeType.AllUsers)
491491
{
492-
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, pkgParams.Scope);
492+
_pathsToInstallPkg = Utils.GetAllInstallationPaths(this, pkgParams.Scope.Value);
493493
}
494494

495495
pkgVersion = pkgInstallInfo["version"] == null ? String.Empty : pkgInstallInfo["version"].ToString();
@@ -566,14 +566,14 @@ private void ProcessInstallHelper(string[] pkgNames, string pkgVersion, bool pkg
566566
}
567567

568568
// When reqResourceParams is provided (via -RequiredResource), use its properties
569-
// instead of the cmdlet-level parameters
570-
bool acceptLicense = reqResourceParams != null ? reqResourceParams.AcceptLicense : AcceptLicense;
571-
bool quiet = reqResourceParams != null ? reqResourceParams.Quiet : Quiet;
572-
bool reinstall = reqResourceParams != null ? reqResourceParams.Reinstall : Reinstall;
573-
bool trustRepository = reqResourceParams != null ? reqResourceParams.TrustRepository : TrustRepository;
574-
bool noClobber = reqResourceParams != null ? reqResourceParams.NoClobber : NoClobber;
575-
bool skipDependencyCheck = reqResourceParams != null ? reqResourceParams.SkipDependencyCheck : SkipDependencyCheck;
576-
ScopeType scope = reqResourceParams != null ? reqResourceParams.Scope : Scope;
569+
// instead of the cmdlet-level parameters. Only use the property if it was explicitly set (not null).
570+
bool acceptLicense = reqResourceParams?.AcceptLicense ?? AcceptLicense;
571+
bool quiet = reqResourceParams?.Quiet ?? Quiet;
572+
bool reinstall = reqResourceParams?.Reinstall ?? Reinstall;
573+
bool trustRepository = reqResourceParams?.TrustRepository ?? TrustRepository;
574+
bool noClobber = reqResourceParams?.NoClobber ?? NoClobber;
575+
bool skipDependencyCheck = reqResourceParams?.SkipDependencyCheck ?? SkipDependencyCheck;
576+
ScopeType scope = reqResourceParams?.Scope ?? Scope;
577577

578578
IEnumerable<PSResourceInfo> installedPkgs = _installHelper.BeginInstallPackages(
579579
names: pkgNames,

src/code/InstallPkgParams.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public class InstallPkgParams
1111
public string Name { get; set; }
1212
public VersionRange Version { get; set; }
1313
public string Repository { get; set; }
14-
public bool AcceptLicense { get; set; }
14+
public bool? AcceptLicense { get; set; }
1515
public bool Prerelease { get; set; }
16-
public ScopeType Scope { get; set; }
17-
public bool Quiet { get; set; }
18-
public bool Reinstall { get; set; }
16+
public ScopeType? Scope { get; set; }
17+
public bool? Quiet { get; set; }
18+
public bool? Reinstall { get; set; }
1919
public bool Force { get; set; }
20-
public bool TrustRepository { get; set; }
21-
public bool NoClobber { get; set; }
22-
public bool SkipDependencyCheck { get; set; }
20+
public bool? TrustRepository { get; set; }
21+
public bool? NoClobber { get; set; }
22+
public bool? SkipDependencyCheck { get; set; }
2323

2424

2525

@@ -67,8 +67,10 @@ public void SetProperty(string propertyName, string propertyValue, out ErrorReco
6767
break;
6868

6969
case "acceptlicense":
70-
bool.TryParse(propertyValue, out bool acceptLicenseTmp);
71-
AcceptLicense = acceptLicenseTmp;
70+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool acceptLicenseTmp))
71+
{
72+
AcceptLicense = acceptLicenseTmp;
73+
}
7274
break;
7375

7476
case "prerelease":
@@ -82,28 +84,38 @@ public void SetProperty(string propertyName, string propertyValue, out ErrorReco
8284
break;
8385

8486
case "quiet":
85-
bool.TryParse(propertyValue, out bool quietTmp);
86-
Quiet = quietTmp;
87+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool quietTmp))
88+
{
89+
Quiet = quietTmp;
90+
}
8791
break;
8892

8993
case "reinstall":
90-
bool.TryParse(propertyValue, out bool reinstallTmp);
91-
Reinstall = reinstallTmp;
94+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool reinstallTmp))
95+
{
96+
Reinstall = reinstallTmp;
97+
}
9298
break;
9399

94100
case "trustrepository":
95-
bool.TryParse(propertyValue, out bool trustRepositoryTmp);
96-
TrustRepository = trustRepositoryTmp;
101+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool trustRepositoryTmp))
102+
{
103+
TrustRepository = trustRepositoryTmp;
104+
}
97105
break;
98106

99107
case "noclobber":
100-
bool.TryParse(propertyValue, out bool noClobberTmp);
101-
NoClobber = noClobberTmp;
108+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool noClobberTmp))
109+
{
110+
NoClobber = noClobberTmp;
111+
}
102112
break;
103113

104114
case "skipdependencycheck":
105-
bool.TryParse(propertyValue, out bool skipDependencyCheckTmp);
106-
SkipDependencyCheck = skipDependencyCheckTmp;
115+
if (!string.IsNullOrWhiteSpace(propertyValue) && bool.TryParse(propertyValue, out bool skipDependencyCheckTmp))
116+
{
117+
SkipDependencyCheck = skipDependencyCheckTmp;
118+
}
107119
break;
108120

109121
default:

0 commit comments

Comments
 (0)