简体   繁体   English

使用鱼壳完成 AWS CLI 命令

[英]AWS CLI command completion with fish shell

Has anybody been able to set up auto-complete for the AWS CLI with fish shell?有没有人能够使用鱼壳为 AWS CLI 设置自动完成? The AWS documentation only offers the guide for bash, tcsh, and zsh. AWS 文档仅提供 bash、tcsh 和 zsh 的指南。

Bash exports the variables COMP_LINE and COMP_POINT that is used by the aws_completer script provided by the Amazon. Bash 导出由 Amazon 提供的 aws_completer 脚本使用的变量COMP_LINECOMP_POINT Is there any equivalent for fish?有鱼的等价物吗? I'm new with the fish shell and I'm giving it a try.我是鱼壳的新手,我正在尝试。

Building upon David Roussel 's answers I cooked up the following:David Roussel的回答基础上,我总结了以下内容:

function __fish_complete_aws
    env COMP_LINE=(commandline -pc) aws_completer | tr -d ' '
end

complete -c aws -f -a "(__fish_complete_aws)"

Put this in a file $HOME/.config/fish/completions/aws.fish so fish can autoload it when necessary.把它放在一个文件$HOME/.config/fish/completions/aws.fish这样 fish 就可以在必要时自动加载它。

aws_completer appends a space after every option it prints and that gets escaped as \\ so trimming it solves the trailing backslashes. aws_completer在它打印的每个选项之后附加一个空格,并作为\\转义,因此修剪它可以解决尾随反斜杠。

Now we can test the completion with the following:现在我们可以使用以下内容测试完成情况:

> complete -C'aws co'
codebuild
codecommit
codepipeline
codestar
cognito-identity
cognito-idp
cognito-sync
comprehend
comprehendmedical
connect
configure
configservice

Using the commandline -c helps if you move back the cursor since it cuts the command line at the cursor so aws_completer can offer the right completions.如果您将光标移回,使用commandline -c aws_completer帮助,因为它会在光标处剪切命令行,因此aws_completer可以提供正确的aws_completer

I also want to get his to work, and I've made some progress, but it's not perfect.我也想让他工作,我已经取得了一些进展,但并不完美。

First I look some advise from here which helps to seem how to emulate the bash environment variables that as_completer expects.首先,我从这里查看一些建议,这有助于了解如何模拟 as_completer 期望的 bash 环境变量。

Putting it together I get this:把它放在一起我得到这个:

complete -c aws -f -a '(begin; set -lx COMP_SHELL fish; set -lx COMP_LINE (commandline); /usr/local/bin/aws_completer; end)'

That mostly works but I get spurious extra slashes, so if I try to complete "aws ec2 describe-instances --" I get:这主要有效,但我得到了虚假的额外斜线,所以如果我尝试完成“aws ec2 describe-instances --”我得到:

dave@retino ~> aws ec2 describe-instances --
--ca-bundle\             --color\          --filters\               --no-dry-run\        --output\      --region\
--cli-connect-timeout\   --debug\          --generate-cli-skeleton  --no-paginate\       --page-size\   --starting-token\
--cli-input-json\        --dry-run\        --instance-ids\          --no-sign-request\   --profile\     --version\
--cli-read-timeout\      --endpoint-url\   --max-items\             --no-verify-ssl\     --query\

It looks to me like there is a trailing whitespace char, but I tried to remove it using sed:在我看来,有一个尾随空格字符,但我尝试使用 sed 将其删除:

complete -c aws -f -a '(begin; set -lx COMP_SHELL fish; set -lx COMP_LINE (commandline); echo (/usr/local/bin/aws_completer | sed -e \'s/[ ]*//\') ; end)'

But this doesn't seem to help.但这似乎没有帮助。 It seems that fish expects a different output format than bash for it's completer.似乎 fish 期望与 bash 不同的输出格式,因为它更完整。 And indeed the fish decimation for the complete builtin doe say that it expects a space separated list.事实上,完整内置doe 的fish decimation说它需要一个空格分隔的列表。

So I tried joining the lines with xargs:所以我尝试用 xargs 加入这些行:

complete -c aws -f -a '(begin; set -lx COMP_SHELL fish; set -lx COMP_LINE (commandline); echo (/usr/local/bin/aws_completer | sed -e \'s/[ ]*//\') | xargs echo ; end)'

But this doesn't work either.但这也行不通。 I just get one completion我只完成一个

This is annoying, I'm so close, but it doesn't work!这很烦人,我很接近,但它不起作用!

While the provided answer doesn't answer the question directly about the using fish ;虽然提供的答案没有直接回答有关使用fish的问题; I intend to provide an answer to help in the context of auto-completion & shell .我打算在auto-completion & shell的上下文中提供一个答案来提供帮助。

Amazon has launched a new CLI based tool forked from AWSCLI .亚马逊推出了一个新的基于 CLI 的工具,从AWSCLI分叉出来。

aws-shell is a command-line shell program that provides convenience and productivity features to help both new and advanced users of the AWS Command Line Interface. aws-shell 是一个命令行 shell 程序,它提供便利和高效的功能,以帮助 AWS 命令​​行界面的新用户和高级用户。 Key features include the following.主要功能包括以下内容。

Fuzzy auto-completion模糊自动完成

  • Commands (eg ec2, describe-instances, sms, create-queue)命令(例如 ec2、describe-instances、sms、create-queue)
  • Options (eg --instance-ids, --queue-url)选项(例如--instance-ids、--queue-url)
  • Resource identifiers (eg Amazon EC2 instance IDs, Amazon SQS queue URLs, Amazon SNS topic names)资源标识符(例如 Amazon EC2 实例 ID、Amazon SQS 队列 URL、Amazon SNS 主题名称) 在此处输入图片说明

Dynamic in-line documentation动态内嵌文档

  • Documentation for commands and options are displayed as you type命令和选项的文档在您键入时显示在此处输入图片说明

Execution of OS shell commands执行 OS shell 命令

  • Use common OS commands such as cat, ls, and cp and pipe inputs and outputs without leaving the shell无需离开外壳即可使用常见的 OS 命令,例如 cat、ls 和 cp 以及管道输入和输出在此处输入图片说明
  • Export executed commands to a text editor To find out more, check out the related blog post on AWS Command Line Interface blog .将执行的命令导出到文本编辑器要了解更多信息,请查看AWS 命令​​行界面博客上的相关博文

在此处输入图片说明

Add this line to your .config/fish/config.fish将此行添加到您的.config/fish/config.fish

complete --command aws --no-files --arguments '(begin; set --local --export COMP_SHELL fish; set --local --export COMP_LINE (commandline); aws_completer | sed \'s/ $//\'; end)'

In case you want to make sure that aws-cli is installed:如果您想确保安装了 aws-cli:

test -x (which aws_completer); and complete --command aws --no-files --arguments '(begin; set --local --export COMP_SHELL fish; set --local --export COMP_LINE (commandline); aws_completer | sed \'s/ $//\'; end)'

All credits belong to this issue thread and a comment by an awesome SO contributor @scooter-dangle.所有学分都属于这个问题线程和一个很棒的 SO 贡献者@scooter-dangle 的评论。

It's actually possible to map bash's completion to fish's.实际上可以将bash的完成映射到fish的完成。

See the npm completions .请参阅npm 完成

However it's probably still better to write a real fish script (it's not hard!).然而,编写一个真正的鱼脚本可能仍然更好(这并不难!)。

The command I use in my virtualenv/bin/activate is this:我在 virtualenv/bin/activate 中使用的命令是这样的:

complete -C aws_completer aws

Looks like aws-cli has fish support too.看起来 aws-cli 也有鱼支持。 There is a bundled installer provided with aws-cli that might be worth checking out: activate.fish . aws-cli 提供了一个捆绑安装程序,可能值得一试: activate.fish I found it in the same bin directory as the aws command.我在 aws 命令所在的 bin 目录中找到了它。

For example:例如:

ubuntu@ip-xxx-xx-x-xx:/data/src$ tail -n1 ~/venv/bin/activate
complete -C aws_completer aws
ubuntu@ip-xxx-xx-x-xx:/data/src$ source ~/venv/bin/activate
(venv) ubuntu@ip-xxx-xx-x-xx:/data/src$ aws s3         <- hitting TAB here
cp        ls        mb        mv        presign   rb        rm        sync      website
(venv) ubuntu@ip-xxx-xx-x-xx:/data/src$ aws s3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM