aws / aws-cli

Universal Command Line Interface for Amazon Web Services
Other
15.14k stars 4.02k forks source link

Pager preference is not obeyed on Windows #8351

Open jimbo8098 opened 7 months ago

jimbo8098 commented 7 months ago

Describe the bug

The AWS_PAGER environment variable or cli_pager config file parameter is not obeyed specifically if you wish to disable pagination. It is obeyed if you set it to a paginator of your choice.

Expected Behavior

Per the AWS CLI documentation you can set the AWS_PAGER environment variable to be a blank string (i.e. not $null) or you can set the relevant option in config like so:

cli_pager=

Both of these would not result in paginated output.

Current Behavior

> $Env:AWS_PAGER=$null
> aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json
'more' is not recognized as an internal or external command,
operable program or batch file.
> $Env:AWS_PAGER="less"
> aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json
'less' is not recognized as an internal or external command,
operable program or batch file.
> $Env:AWS_PAGER=""
> aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json
'more' is not recognized as an internal or external command,
operable program or batch file.
> [System.Environment]::SetEnvironmentVariable("AWS_PAGER","")
> aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json
'more' is not recognized as an internal or external command,
operable program or batch file.
> [System.Environment]::SetEnvironmentVariable("AWS_PAGER","less")
> aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json
'less' is not recognized as an internal or external command,
operable program or batch file.
> [System.Environment]::SetEnvironmentVariable("AWS_PAGER",$null)
> aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json
'more' is not recognized as an internal or external command,
operable program or batch file.

Note that if I instead run the AWS CLI command with --no-cli-pager, like below, the command works as expected:

aws ec2 describe-instances --filters "Name=tag:Name,Values=Workstation" --query Reservations[].Instances[] --output json --no-cli-pager

Reproduction Steps

  1. Set ~/.aws/config's default profile to have cli_pager=
  2. Open a new PowerShell Window
  3. Run [System.Environment]::SetEnvironmentVariable("AWS_PAGER","")
  4. Assume your AWS profile of choice. I used an existing SSO session by setting $Env:AWS_PROFILE to the relevant profile name.
  5. Run a CLI paginated AWS CLI command, such as the describe-instances command above. The output is be paginated.

Possible Solution

I use both Windows and Linux and haven't seen this same behaviour exhibited by Linux. I therefore suspect that this is just an oversight in how Windows handles variables.

Additional Information/Context

CLI version used

aws-cli/2.13.25 Python/3.11.5 Windows/10 exe/AMD64 prompt/off

Environment details (OS name and version, etc.)

Windows 10 Business 10.0.19045

RyanFitzSimmonsAK commented 7 months ago

Hi @jimbo8098, thanks for reaching out. I'm still investigating this issue, but I wanted bring up that I think cli_pager = is actually working correctly. Your reproduction steps are setting both though, and from the environment variables docs, AWS_PAGER overrides cli_pager =.

I've been able to reproduce the issue with AWS_PAGER and am trying to root cause the problem.

jimbo8098 commented 7 months ago

My default Powershell profile .ps1 nor the default environment variables include a definition for AWS_PAGER. Here are the commands run in a newly opened Powershell window:

> echo "`"$Env:AWS_PAGER`""
""
> $Env:AWS_PAGER=""
> echo "`"$Env:AWS_PAGER`""
""
> $Env:AWS_PAGER="less"
> echo "`"$Env:AWS_PAGER`""
"less"

When cli_pager is not present in the AWS profile alongside $Env:AWS_PAGER set to "", paginated output is shown.

I suspect the environment variable is being interpreted incorrectly. Perhaps some oversight in how scopes in Windows might be different and the aspect of comparitors in Powershell specifically being a bit more advanced than a usual if[] block in Bash.

You have $Env:AWS_PAGER, sure, but you also have local scope with AWS_PAGER and also the machine level scope commonly set through setx or the environment variable dialog. Commonly, I'd expect that setting $Env:AWS_PAGER is most useful for a setting you wish to have persist temporarily for a Powershell terminal session, which is what I'd expect of this kind of definition. The cli_pager on the other hand would be a more persistent setting, remaining beyond just that session.

The presence of cli_pager= in the config file for the given profile works as expected. I was previously setting the default profile to use cli_pager= but this wasn't affecting the pagination but this is fine when set at a named profile scope. I had misread the documentation assuming this to set the default value for this which was incorrect.