| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | func renderScopeForPath(path string) RenderScope { |
| 10 | if isUserConfigPath(path) { |
| 11 | return RenderScopeUser |
| 12 | } |
| 13 | return RenderScopeProject |
| 14 | } |
| 15 | |
| 16 | func isUserConfigPath(path string) bool { |
| 17 | path = strings.TrimSpace(path) |
| 18 | if path == "" { |
| 19 | return false |
| 20 | } |
| 21 | for _, uc := range userConfigCandidatePaths() { |
| 22 | uc = strings.TrimSpace(uc) |
| 23 | if uc == "" { |
| 24 | continue |
| 25 | } |
| 26 | pathAbs, pathErr := filepath.Abs(path) |
| 27 | ucAbs, ucErr := filepath.Abs(uc) |
| 28 | if pathErr == nil && ucErr == nil { |
| 29 | if filepath.Clean(pathAbs) == filepath.Clean(ucAbs) { |
| 30 | return true |
| 31 | } |
| 32 | continue |
| 33 | } |
| 34 | if filepath.Clean(path) == filepath.Clean(uc) { |
| 35 | return true |
| 36 | } |
| 37 | } |
| 38 | return false |
| 39 | } |
| 40 | |
| 41 | // IsUserConfigPath reports whether path is one of Reasonix's current or legacy |
| 42 | // user-global config locations. Other paths use project-scoped rendering. |
| 43 | func IsUserConfigPath(path string) bool { |
| 44 | return isUserConfigPath(path) |
| 45 | } |
| 46 | |
| 47 | // Save writes the configuration back to the file it was loaded from |
| 48 | // (SourcePath), or to ./reasonix.toml when none exists yet — the conventional |
| 49 | // project-local target a fresh GUI session would create. |
| 50 | func (c *Config) Save() error { |
| 51 | path := SourcePath() |
| 52 | if path == "" { |
| 53 | path = "reasonix.toml" |
| 54 | } |
| 55 | return c.SaveTo(path) |
| 56 | } |
| 57 | |
| 58 | // SaveForRoot saves root's project config when it exists, falling back to the |
| 59 | // user's global config when root has no reasonix.toml. Existing project files |
| 60 | // are edited from their own TOML only, never from a runtime user+project merge. |
| 61 | func (c *Config) SaveForRoot(root string) error { |
| 62 | root = resolveRoot(root) |
| 63 | projectTOML := "reasonix.toml" |
| 64 | if root != "." { |
| 65 | projectTOML = filepath.Join(root, "reasonix.toml") |
| 66 | } |
| 67 | if _, err := os.Stat(projectTOML); err == nil { |
| 68 | projectCfg := LoadForEditWithoutCredentials(projectTOML) |
| 69 | return projectCfg.SaveTo(projectTOML) |
| 70 | } |
| 71 | if uc := userConfigPath(); uc != "" { |
| 72 | if err := os.MkdirAll(filepath.Dir(uc), 0o755); err != nil { |
| 73 | return err |
| 74 | } |
| 75 | return c.SaveTo(uc) |
| 76 | } |
| 77 | return c.SaveTo(projectTOML) |
| 78 | } |
| 79 |