| 1 |
//go:build examples |
| 2 |
// +build examples |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
|
| 12 |
"github.com/gopasspw/gitconfig" |
| 13 |
) |
| 14 |
|
| 15 |
// Example 3: Understanding Scopes |
| 16 |
// |
| 17 |
// This example demonstrates the configuration scope hierarchy. |
| 18 |
// Git config has multiple scopes with a clear precedence order: |
| 19 |
// 1. Environment variables (highest priority) |
| 20 |
// 2. Per-worktree config |
| 21 |
// 3. Per-repository config (local) |
| 22 |
// 4. Per-user config (global) |
| 23 |
// 5. System-wide config |
| 24 |
// 6. Presets (built-in defaults, lowest priority) |
| 25 |
// |
| 26 |
// When you call Get(), the library searches through scopes in order |
| 27 |
// and returns the first value it finds. |
| 28 |
func main() { |
| 29 |
// Create temporary directories for different config scopes |
| 30 |
tmpDir, err := os.MkdirTemp("", "gitconfig-example-") |
| 31 |
if err != nil { |
| 32 |
log.Fatal(err) |
| 33 |
} |
| 34 |
defer os.RemoveAll(tmpDir) |
| 35 |
|
| 36 |
if err := os.Setenv("GOPASS_HOMEDIR", tmpDir); err != nil { |
| 37 |
log.Fatal(err) |
| 38 |
} |
| 39 |
defer func() { |
| 40 |
_ = os.Unsetenv("GOPASS_HOMEDIR") |
| 41 |
}() |
| 42 |
|
| 43 |
gitDir := filepath.Join(tmpDir, ".git") |
| 44 |
if err := os.MkdirAll(gitDir, 0o755); err != nil { |
| 45 |
log.Fatal(err) |
| 46 |
} |
| 47 |
|
| 48 |
fmt.Println("=== Example 3: Understanding Scopes ===\n") |
| 49 |
|
| 50 |
// Create sample config files for different scopes |
| 51 |
systemConfig := filepath.Join(tmpDir, "gitconfig-system") |
| 52 |
globalConfig := filepath.Join(tmpDir, "gitconfig-global") |
| 53 |
localConfig := filepath.Join(gitDir, "config") |
| 54 |
|
| 55 |
// System config (lowest priority among files) |
| 56 |
err = os.WriteFile(systemConfig, []byte(`[user] |
| 57 |
name = System User |
| 58 |
email = [email protected] |
| 59 |
[core] |
| 60 |
pager = less |
| 61 |
`), 0o644) |
| 62 |
if err != nil { |
| 63 |
log.Fatal(err) |
| 64 |
} |
| 65 |
|
| 66 |
// Global/user config |
| 67 |
err = os.WriteFile(globalConfig, []byte(`[user] |
| 68 |
name = Global User |
| 69 |
email = [email protected] |
| 70 |
[core] |
| 71 |
editor = emacs |
| 72 |
`), 0o644) |
| 73 |
if err != nil { |
| 74 |
log.Fatal(err) |
| 75 |
} |
| 76 |
|
| 77 |
// Local/repository config (highest priority among files) |
| 78 |
err = os.WriteFile(localConfig, []byte(`[user] |
| 79 |
name = Local User |
| 80 |
[core] |
| 81 |
autocrlf = true |
| 82 |
`), 0o644) |
| 83 |
if err != nil { |
| 84 |
log.Fatal(err) |
| 85 |
} |
| 86 |
|
| 87 |
// Create a Configs object that loads all scopes |
| 88 |
// Note: This example uses custom paths since we don't have a real system setup |
| 89 |
cfg := gitconfig.New() |
| 90 |
|
| 91 |
// Manually customize paths for this example |
| 92 |
cfg.SystemConfig = systemConfig |
| 93 |
cfg.GlobalConfig = "gitconfig-global" |
| 94 |
cfg.LocalConfig = filepath.Join(".git", "config") |
| 95 |
|
| 96 |
fmt.Println("Config scope hierarchy (highest to lowest priority):") |
| 97 |
fmt.Println(" 1. Environment variables") |
| 98 |
fmt.Println(" 2. Per-worktree config") |
| 99 |
fmt.Println(" 3. Local (per-repository) config") |
| 100 |
fmt.Println(" 4. Global (per-user) config") |
| 101 |
fmt.Println(" 5. System-wide config") |
| 102 |
fmt.Println(" 6. Presets (built-in defaults)") |
| 103 |
|
| 104 |
// Load and display |
| 105 |
cfg.LoadAll(tmpDir) |
| 106 |
|
| 107 |
fmt.Println("\nResolved values (respecting scope priority):") |
| 108 |
fmt.Println(" user.name =", getOrDefault(cfg, "user.name")) |
| 109 |
fmt.Println(" ^ Comes from local config (highest priority)") |
| 110 |
fmt.Println(" user.email =", getOrDefault(cfg, "user.email")) |
| 111 |
fmt.Println(" ^ Comes from global config (not overridden locally)") |
| 112 |
fmt.Println(" core.editor =", getOrDefault(cfg, "core.editor")) |
| 113 |
fmt.Println(" ^ Comes from global config") |
| 114 |
fmt.Println(" core.pager =", getOrDefault(cfg, "core.pager")) |
| 115 |
fmt.Println(" ^ Comes from system config (not overridden)") |
| 116 |
fmt.Println(" core.autocrlf =", getOrDefault(cfg, "core.autocrlf")) |
| 117 |
fmt.Println(" ^ Comes from local config") |
| 118 |
|
| 119 |
// Show how to access specific scopes directly |
| 120 |
fmt.Println("\nAccessing specific scopes directly:") |
| 121 |
|
| 122 |
local, err := gitconfig.LoadConfig(localConfig) |
| 123 |
if err == nil { |
| 124 |
if name, ok := local.Get("user.name"); ok { |
| 125 |
fmt.Printf(" local user.name = %s\n", name) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
global, err := gitconfig.LoadConfig(globalConfig) |
| 130 |
if err == nil { |
| 131 |
if editor, ok := global.Get("core.editor"); ok { |
| 132 |
fmt.Printf(" global core.editor = %s\n", editor) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
fmt.Println("\n=== Summary ===") |
| 137 |
fmt.Println("Git has multiple config scopes with clear precedence.") |
| 138 |
fmt.Println("Use Configs.Get() to read values respecting all scopes.") |
| 139 |
fmt.Println("Use GetLocal(), GetGlobal(), etc. to read from specific scopes.") |
| 140 |
fmt.Println("Use SetLocal(), SetGlobal(), etc. to write to specific scopes.") |
| 141 |
} |
| 142 |
|
| 143 |
func getOrDefault(cfg *gitconfig.Configs, key string) string { |
| 144 |
if v := cfg.Get(key); v != "" { |
| 145 |
return v |
| 146 |
} |
| 147 |
return "(not set)" |
| 148 |
} |