| 32 | } |
| 33 | |
| 34 | func LoadBackendConfig() (TerraformState, error) { |
| 35 | functionName := "LoadBackendConfig" |
| 36 | tfState := TerraformState{} |
| 37 | tfStatePath := filepath.Join(auxiliary.StateInstance.WorkingPath, ".terraform", "terraform.tfstate") |
| 38 | |
| 39 | // Check if backend config exists |
| 40 | if _, err := os.Stat(tfStatePath); errors.Is(err, os.ErrNotExist) { |
| 41 | ux.PrintFormatted(" ✘", []string{"yellow", "bold"}) |
| 42 | fmt.Println(" Couldn't sync diagram to remote backend. No remote backend detected.") |
| 43 | return tfState, nil |
| 44 | } |
| 45 | |
| 46 | // Read terraform state for backend information |
| 47 | tfStateByte, configErr := os.ReadFile(tfStatePath) |
| 48 | if configErr != nil { |
| 49 | return tfState, fmt.Errorf("failed to read working directory config -> %v: %w", functionName, configErr) |
| 50 | } |
| 51 | |
| 52 | // Parse terraform state |
| 53 | jsonErr := json.Unmarshal(tfStateByte, &tfState) |
| 54 | if jsonErr != nil { |
| 55 | return tfState, fmt.Errorf("failed to unmarshal terraform state information -> %v: %w", functionName, jsonErr) |
| 56 | } |
| 57 | |
| 58 | return tfState, nil |
| 59 | } |