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

Support for TF_WORKSPACE #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
14 changes: 10 additions & 4 deletions tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,23 @@ func ReadWithWorkspace(ctx context.Context, src io.Reader, ws string) (*TFState,
return &s, nil
}

// ReadFile reads terraform.tfstate from the file (a workspace reads from environment file in the same directory)
// ReadFile reads terraform.tfstate from the file
// (Firstly, a workspace reads TF_WORKSPACE environment variable. if it doesn't exist, it reads from environment file in the same directory)
func ReadFile(ctx context.Context, file string) (*TFState, error) {
ws, _ := os.ReadFile(filepath.Join(filepath.Dir(file), "environment"))
// if not exist, don't care (using default workspace)
ws := func() string {
if env := os.Getenv("TF_WORKSPACE"); env != "" {
return env
}
f, _ := os.ReadFile(filepath.Join(filepath.Dir(file), "environment"))
return string(f)

}()
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("failed to read tfstate from %s: %w", file, err)
}
defer f.Close()
return ReadWithWorkspace(ctx, f, string(ws))
return ReadWithWorkspace(ctx, f, ws)
}

// ReadURL reads terraform.tfstate from the URL.
Expand Down
Loading