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

Add terraform console for REPL #10093

Merged
merged 9 commits into from
Nov 14, 2016
Merged

Add terraform console for REPL #10093

merged 9 commits into from
Nov 14, 2016

Conversation

mitchellh
Copy link
Contributor

@mitchellh mitchellh commented Nov 14, 2016

Fixes #9750

This PR adds a new command terraform console that opens an interactive console for experimenting with interpolations. I forsee in the future the console being much more powerful and being to invoke Terraform, modify state, etc. But, for v1 of this feature, it is only used for interacting with interpolations.

The console command will read your state so you can access resources from the console. If you configure a remote state, it can read that as well.

There are a few use cases for this:

  • Learning: You can use the console to learn and play with interpolations.

  • Testing: You can try interpolations before you use them, or verify that they're working correctly.

  • Scripting: You can use interpolations to read values and transform them from the state in a Unix-friendly way. This is especially true for remote state.

Demo Gif

Interactive mode:

repl

Piped input:

$ echo "1+5" | terraform console
6

PR Contents

  • helper/wrappedreadline: New helper library for gaining access to the original standard streams and not the wrappers given to us by panicwrap. This is important for readline to work properly.

  • repl: New core Terraform library for REPL-behavior. This extraction allows us to unit test it very well instead of it all being stuffed in core. Also, by being a separate library there exists a future where we may be able to drop a REPL in scenarios where Terraform fails at runtime (with a debug flag or something). Maybe.

  • command/console: The console command.

  • terraform/interpolation.go changes: Improving the error messages that are now possible due to the REPL. This file continues to be a primary target for refactor and change in the future.

  • vendor update panicwrap: panicwrap needed to be updated to pass through raw standard streams.

Future

The following are some ideas I have for the future of this that won't be included in this PR or targeted even for 0.8:

  • Tab-completion: we get this from the readline library we use, but we need to complete the completer.

  • Setting variables

  • Listing resources

  • Actual resource configuration

  • Operations such as plan/apply

@mitchellh mitchellh added this to the Terraform 0.8 milestone Nov 14, 2016
The readline library doesn't support Solaris. For now, we'll just not
support console there.
@apparentlymart
Copy link
Contributor

Heh... when I was writing the new HIL parser I was thinking about whether I should add in a mode where it parses as if it were already inside ${ ... } for this purpose, but I suppose just wrapping the user input in that directly is a reasonable enough way to get there.

This would just be polish that could be handled separately, but might be worth considering using the new entry point I added to HIL that allows the caller to specify the start position, so you can set it to be something like "line 1, column -1" so that the user-entered expression ends up starting at line 1, column 1. Though probably easier to wait to do that until Terraform's interpolator itself is able to pass config positions into the HIL parser, since this layer can't "see" the HIL interface at all.

@mitchellh
Copy link
Contributor Author

@apparentlymart Yeah I thought about both of those options too, but there are a couple things holding me back:

1.) Parsing 'interpolation mode' first would require significant refactors of TF since config.RawConfig is currently the easiest way to make this work rather than parsing the HIL directly. So I'd rather rethink that first vs updating HIL

2.) ast.Pos is blocked by above too. :)

@mitchellh mitchellh merged commit aaf1ad0 into master Nov 14, 2016
@mitchellh mitchellh deleted the f-console branch November 14, 2016 19:53
@ghost
Copy link

ghost commented Apr 20, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function REPL-esque Evaluator?
4 participants