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

Terraform command completions #3960

Merged
merged 7 commits into from
Apr 17, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions share/completions/terraform.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
function __fish_terraform_needs_command
set cmd (commandline -opc)
Copy link
Contributor

Choose a reason for hiding this comment

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

In general you should use local variables to avoid modifying a global var by the same name; i.e., set -l cmd....

Copy link
Contributor Author

@adambyrtek adambyrtek Apr 17, 2017

Choose a reason for hiding this comment

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

This function is not needed anymore.

if [ (count $cmd) -eq 1 ]
Copy link
Contributor

Choose a reason for hiding this comment

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

The idiomatic fish way to do this is if not set -q cmd[2] to avoid the subcommand and test. Also, we prefer test over [. The latter is there strictly to make it easier for people transitioning from POSIX 1003.1 shells.

Copy link
Contributor Author

@adambyrtek adambyrtek Apr 17, 2017

Choose a reason for hiding this comment

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

This function is not needed anymore.

return 0
end

for arg in $cmd[2..-1]
switch $arg
case '--*'
# ignore global flags
case '*'
return 1
end
end
return 0
end

function __fish_terraform_using_command
Copy link
Contributor

Choose a reason for hiding this comment

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

@faho should comment on this since I'm a completion newbie but I thought that functions in general should be using __fish_use_subcommand rather than rolling their own custom version. That there are existing completions that haven't been updated to use the global function is no surprise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I replaced this function with __fish_use_subcommand.

Copy link
Member

Choose a reason for hiding this comment

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

Well......

The issue with custom versions is that they are usually just

if test "$cmd[2]" = $argv[1]
     return 0
else
    return 1
end

(and maybe some annoying stuff about how $cmd[1] needs to be the name of the tool, which breaks aliases)

However, __fish_use_subcommand isn't much better:

function __fish_use_subcommand -d "Test if a non-switch argument has been given in the current commandline"
    set -l cmd (commandline -poc)
    set -e cmd[1]
    for i in $cmd
        switch $i
            case '-*'
                continue
        end
        return 1
    end
    return 0
end

This will break if

  • options are allowed before the subcommand

  • those options can be used like --option argument, i.e. the argument is another token that doesn't start with "-"

e.g. on something like git --git-dir /some/path.

Which is why the git completions have the __fish_git_needs_command function, that explicitly skips all the options, and skips the next token for options that can take a command.

Currently, I don't know of an easier way to be "fully" correct (you essentially need to recreate the command's argument parsing). __fish_use_subcommand is okay if no argument-taking options are allowed before the subcommand, something like not __fish_seen_subcommand_from $cmds is often good enough. Neither are perfect, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case this should be fine since options alowed before Terraform subcommand don't take any arguments (e.g. terraform --help plan). I get your point about Git completions thought.

set cmd (commandline -opc)
if [ (count $cmd) -eq 1 ]
return 0
end

for arg in $cmd[2..-1]
switch $arg
case '--*'
# ignore global flags
case $argv[1]
return 0
case '*'
return 1
end
end
return 1
end

# general options
complete -f -c terraform -l version -d 'Print version information'
complete -f -c terraform -l help -d 'Show help'

### apply
complete -f -c terraform -n '__fish_terraform_needs_command' -a apply -d 'Builds or changes infrastructure'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o backup -d 'Path to backup the existing state file'
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to my previous comment I believe the current best practice is to use __fish_seen_subcommand_from rather than define a custom function which does essentially the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

complete -f -c terraform -n '__fish_terraform_using_command apply' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o input -d 'Ask for input for variables if not directly set'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o parallelism -d 'Limit the number of concurrent operations'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o refresh -d 'Update state prior to checking for differences'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o state-out -d 'Path to write state'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o target -d 'Resource to target'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command apply' -o var-file -d 'Set variables from a file'

### console
complete -f -c terraform -n '__fish_terraform_needs_command' -a console -d 'Interactive console for Terraform interpolations'
complete -f -c terraform -n '__fish_terraform_using_command console' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command console' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command console' -o var-file -d 'Set variables from a file'

### destroy
complete -f -c terraform -n '__fish_terraform_needs_command' -a destroy -d 'Destroy Terraform-managed infrastructure'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o backup -d 'Path to backup the existing state file'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o force -d 'Don\'t ask for input for destroy confirmation'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o parallelism -d 'Limit the number of concurrent operations'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o refresh -d 'Update state prior to checking for differences'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o state-out -d 'Path to write state'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o target -d 'Resource to target'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command destroy' -o var-file -d 'Set variables from a file'

### env
complete -f -c terraform -n '__fish_terraform_needs_command' -a env -d 'Environment management'
complete -f -c terraform -n '__fish_terraform_using_command env' -a list -d 'List environments'
complete -f -c terraform -n '__fish_terraform_using_command env' -a select -d 'Select an environment'
complete -f -c terraform -n '__fish_terraform_using_command env' -a new -d 'Create a new environment'
complete -f -c terraform -n '__fish_terraform_using_command env' -a delete -d 'Delete an existing environment'

### fmt
complete -f -c terraform -n '__fish_terraform_needs_command' -a fmt -d 'Rewrites config files to canonical format'
complete -f -c terraform -n '__fish_terraform_using_command fmt' -o list -d 'List files whose formatting differs'
complete -f -c terraform -n '__fish_terraform_using_command fmt' -o write -d 'Write result to source file'
complete -f -c terraform -n '__fish_terraform_using_command fmt' -o diff -d 'Display diffs of formatting changes'

### get
complete -f -c terraform -n '__fish_terraform_needs_command' -a get -d 'Download and install modules for the configuration'
complete -f -c terraform -n '__fish_terraform_using_command get' -o update -d 'Check modules for updates'
complete -f -c terraform -n '__fish_terraform_using_command get' -o no-color -d 'If specified, output won\'t contain any color'

### graph
complete -f -c terraform -n '__fish_terraform_needs_command' -a graph -d 'Create a visual graph of Terraform resources'
complete -f -c terraform -n '__fish_terraform_using_command graph' -o draw-cycles -d 'Highlight any cycles in the graph'
complete -f -c terraform -n '__fish_terraform_using_command graph' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command graph' -o type -d 'Type of graph to output'

### import
complete -f -c terraform -n '__fish_terraform_needs_command' -a import -d 'Import existing infrastructure into Terraform'
complete -f -c terraform -n '__fish_terraform_using_command import' -o backup -d 'Path to backup the existing state file'
complete -f -c terraform -n '__fish_terraform_using_command import' -o config -d 'Path to a directory of configuration files'
complete -f -c terraform -n '__fish_terraform_using_command import' -o input -d 'Ask for input for variables if not directly set'
complete -f -c terraform -n '__fish_terraform_using_command import' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command import' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command import' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command import' -o provider -d 'Specific provider to use for import'
complete -f -c terraform -n '__fish_terraform_using_command import' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command import' -o state-out -d 'Path to write state'
complete -f -c terraform -n '__fish_terraform_using_command import' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command import' -o var-file -d 'Set variables from a file'

### init
complete -f -c terraform -n '__fish_terraform_needs_command' -a init -d 'Initialize a new or existing Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command init' -o backend -d 'Configure the backend for this environment'
complete -f -c terraform -n '__fish_terraform_using_command init' -o backend-config -d 'Backend configuration'
complete -f -c terraform -n '__fish_terraform_using_command init' -o get -d 'Download modules for this configuration'
complete -f -c terraform -n '__fish_terraform_using_command init' -o input -d 'Ask for input if necessary'
complete -f -c terraform -n '__fish_terraform_using_command init' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command init' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command init' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command init' -o force-copy -d 'Suppress prompts about copying state data'

### output
complete -f -c terraform -n '__fish_terraform_needs_command' -a output -d 'Read an output from a state file'
complete -f -c terraform -n '__fish_terraform_using_command output' -o state -d 'Path to the state file to read'
complete -f -c terraform -n '__fish_terraform_using_command output' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command output' -o module -d 'Return the outputs for a specific module'
complete -f -c terraform -n '__fish_terraform_using_command output' -o json -d 'Print output in JSON format'

### plan
complete -f -c terraform -n '__fish_terraform_needs_command' -a plan -d 'Generate and show an execution plan'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o destroy -d 'Generate a plan to destroy all resources'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o detailed-exitcode -d 'Return detailed exit codes'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o input -d 'Ask for input for variables if not directly set'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o module-depth -d 'Depth of modules to show in the output'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o out -d 'Write a plan file to the given path'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o parallelism -d 'Limit the number of concurrent operations'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o refresh -d 'Update state prior to checking for differences'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o target -d 'Resource to target'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command plan' -o var-file -d 'Set variables from a file'

### push
complete -f -c terraform -n '__fish_terraform_needs_command' -a push -d 'Upload this Terraform module to Atlas to run'
complete -f -c terraform -n '__fish_terraform_using_command push' -o atlas-address -d 'An alternate address to an Atlas instance'
complete -f -c terraform -n '__fish_terraform_using_command push' -o upload-modules -d 'Lock modules and upload completely'
complete -f -c terraform -n '__fish_terraform_using_command push' -o name -d 'Name of the configuration in Atlas'
complete -f -c terraform -n '__fish_terraform_using_command push' -o token -d 'Access token to use to upload'
complete -f -c terraform -n '__fish_terraform_using_command push' -o overwrite -d 'Variable keys that should overwrite values in Atlas'
complete -f -c terraform -n '__fish_terraform_using_command push' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command push' -o var-file -d 'Set variables from a file'
complete -f -c terraform -n '__fish_terraform_using_command push' -o vcs -d 'Upload only files committed to your VCS'
complete -f -c terraform -n '__fish_terraform_using_command push' -o no-color -d 'If specified, output won\'t contain any color'

### refresh
complete -f -c terraform -n '__fish_terraform_needs_command' -a refresh -d 'Update local state file against real resources'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o backup -d 'Path to backup the existing state file'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o input -d 'Ask for input for variables if not directly set'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o state-out -d 'Path to write state'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o target -d 'Resource to target'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o var -d 'Set a variable in the Terraform configuration'
complete -f -c terraform -n '__fish_terraform_using_command refresh' -o var-file -d 'Set variables from a file'

### show
complete -f -c terraform -n '__fish_terraform_needs_command' -a show -d 'Inspect Terraform state or plan'
complete -f -c terraform -n '__fish_terraform_using_command show' -o module-depth -d 'Depth of modules to show in the output'
complete -f -c terraform -n '__fish_terraform_using_command show' -o no-color -d 'If specified, output won\'t contain any color'

### taint
complete -f -c terraform -n '__fish_terraform_needs_command' -a taint -d 'Manually mark a resource for recreation'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o allow-missing -d 'Succeed even if resource is missing'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o backup -d 'Path to backup the existing state file'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o module -d 'The module path where the resource lives'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o state-out -d 'Path to write state'

### untaint
complete -f -c terraform -n '__fish_terraform_needs_command' -a untaint -d 'Manually unmark a resource as tainted'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o allow-missing -d 'Succeed even if resource is missing'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o backup -d 'Path to backup the existing state file'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o lock -d 'Lock the state file when locking is supported'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o lock-timeout -d 'Duration to retry a state lock'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o module -d 'The module path where the resource lives'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o no-color -d 'If specified, output won\'t contain any color'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o state -d 'Path to a Terraform state file'
complete -f -c terraform -n '__fish_terraform_using_command taint' -o state-out -d 'Path to write state'

### validate
complete -f -c terraform -n '__fish_terraform_needs_command' -a validate -d 'Validates the Terraform files'
complete -f -c terraform -n '__fish_terraform_using_command validate' -o no-color -d 'If specified, output won\'t contain any color'

### version
complete -f -c terraform -n '__fish_terraform_needs_command' -a version -d 'Prints the Terraform version'