hashicorp / terraform-config-inspect

A helper library for shallow inspection of Terraform configurations
Mozilla Public License 2.0
383 stars 76 forks source link

Allow parsing module from any filesystem #49

Closed radeksimko closed 4 years ago

radeksimko commented 4 years ago

Summary

This PR introduces two new interfaces to aid with accessing configuration in any "abstract" filesystem, such as in-memory FS which is used by the language server. In LSP the server should not read files from disk if there is copy in memory (as sent by the client).

type FS interface {
    Open(name string) (File, error)
    ReadFile(name string) ([]byte, error)
    ReadDir(dirname string) ([]os.FileInfo, error)
}
type File interface {
    Stat() (os.FileInfo, error)
    Read([]byte) (int, error)
    Close() error
}

I created interfaces pretty much identical to the ones recently published in the Go proposal for io/fs. We don't actually need Open nor File, but I believe that adding later anything to an interface is going to be much harder than removing anything. Also assuming that the proposal goes ahead and is implemented like this, it should be fairly easy to just swap the argument types for the stdlib ones and add type aliases.

Two new functions which use this new interface were also introduced:

Backwards Compatibility for Existing Consumers

The existing interfaces should work the same as before.