Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-Content -Head and -Tail disallow negative values and cleanup #19715

Merged
merged 15 commits into from
Jun 21, 2023
Next Next commit
TotalCount {get; set;}
  • Loading branch information
CarloToso committed May 23, 2023
commit ad5957e6c2015ad778244b38310cf725da073733
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,9 @@ public class GetContentCommand : ContentCommandBase
/// value is -1 which means read all the content.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true)]
[ValidateRange(0, long.MaxValue)]
[Alias("First", "Head")]
public long TotalCount
{
get
{
return _totalCount;
}

set
{
_totalCount = value;
_totalCountSpecified = true;
}
}

private bool _totalCountSpecified = false;
public long TotalCount { get; set; } = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, I wonder if this should be just be long? TotalCount


/// <summary>
/// The number of content items to retrieve from the back of the file.
Expand Down Expand Up @@ -98,15 +85,6 @@ internal override object GetDynamicParameters(CmdletProviderContext context)

#endregion Parameters

#region parameter data

/// <summary>
/// The number of content items to retrieve.
/// </summary>
private long _totalCount = -1;

#endregion parameter data

#region Command code

/// <summary>
Expand All @@ -116,7 +94,7 @@ protected override void ProcessRecord()
{
// TotalCount and Tail should not be specified at the same time.
// Throw out terminating error if this is the case.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment seems wrong - it doesn't look like a terminating error

if (_totalCountSpecified && _tailSpecified)
if (TotalCount != -1 && _tailSpecified)
{
string errMsg = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "TotalCount", "Tail");
ErrorRecord error = new(new InvalidOperationException(errMsg), "TailAndHeadCannotCoexist", ErrorCategory.InvalidOperation, null);
Expand Down Expand Up @@ -209,7 +187,7 @@ protected override void ProcessRecord()
// I am using TotalCount - countToRead so that I don't
// have to worry about overflow

if ((TotalCount > 0) && (countToRead == 0 || (TotalCount - countToRead < countRead)))
if (TotalCount > 0 && (countToRead == 0 || TotalCount - countToRead < countRead))
{
countToRead = TotalCount - countRead;
}
Expand Down Expand Up @@ -256,7 +234,7 @@ protected override void ProcessRecord()
WriteContentObject(results, countRead, holder.PathInfo, currentContext);
}
}
} while (results != null && results.Count > 0 && ((TotalCount < 0) || countRead < TotalCount));
} while (results != null && results.Count > 0 && (TotalCount < 0 || countRead < TotalCount));
}
}
}
Expand Down