Skip to content

Commit

Permalink
If TF_WORKSPACE is specified, that value takes precedence.
Browse files Browse the repository at this point in the history
TF_WORKSPACE allows you to switch workspaces without an environment file.
  • Loading branch information
shmokmt committed Nov 13, 2024
1 parent 4b5e629 commit 6337c78
Showing 1 changed file with 10 additions and 4 deletions.
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

0 comments on commit 6337c78

Please sign in to comment.