-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse .terraform-version files for stacks dirs
- Loading branch information
Showing
7 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package jobs | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/terraform-ls/internal/document" | ||
"github.com/hashicorp/terraform-ls/internal/features/stacks/state" | ||
"github.com/hashicorp/terraform-ls/internal/job" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/module/operation" | ||
) | ||
|
||
// TODO: find a place to register a watcher for this .terraform-version file (put this in a follow-up issue), for now reloading the window is okay enough | ||
// https://github.com/hashicorp/terraform-ls/blob/c47b2e51a0ca08628a596eca8d5cd11d005b7d87/internal/langserver/handlers/initialized.go#L25 | ||
|
||
// LoadTerraformVersion loads the terraform version from the .terraform-version | ||
// file in the stack directory. | ||
func LoadTerraformVersion(ctx context.Context, fs ReadOnlyFS, stackStore *state.StackStore, stackPath string) error { | ||
stackRecord, err := stackStore.StackRecordByPath(stackPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Avoid parsing if it is already in progress or already known | ||
if stackRecord.TerraformVersionState != operation.OpStateUnknown && !job.IgnoreState(ctx) { | ||
return job.StateNotChangedErr{Dir: document.DirHandleFromPath(stackPath)} | ||
} | ||
|
||
stackStore.SetTerraformVersionState(stackPath, operation.OpStateLoading) | ||
|
||
// read version file | ||
v, err := fs.ReadFile(filepath.Join(stackPath, ".terraform-version")) | ||
if err != nil { | ||
stackStore.SetTerraformVersionError(stackPath, err) | ||
return err | ||
} | ||
|
||
// parse version | ||
version, err := version.NewVersion(strings.TrimSpace(string(v))) | ||
if err != nil { | ||
stackStore.SetTerraformVersionError(stackPath, err) | ||
return err | ||
} | ||
|
||
stackStore.SetTerraformVersion(stackPath, version) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters