| 1 |
# gitconfig Examples |
| 2 |
|
| 3 |
This directory contains practical examples demonstrating how to use the gitconfig library. |
| 4 |
|
| 5 |
## Examples |
| 6 |
|
| 7 |
### 1. [Basic Read](01-basic-read.go) |
| 8 |
|
| 9 |
Demonstrates reading configuration values from git config using the simple Config API. |
| 10 |
|
| 11 |
**Topics:** |
| 12 |
|
| 13 |
- Loading a config file |
| 14 |
- Reading string values |
| 15 |
- Handling missing keys |
| 16 |
|
| 17 |
**Run:** |
| 18 |
|
| 19 |
```bash |
| 20 |
go run -tags=examples examples/01-basic-read.go |
| 21 |
``` |
| 22 |
|
| 23 |
### 2. [Write and Persist](02-write-persist.go) |
| 24 |
|
| 25 |
Shows how to modify configuration values and persist changes back to disk. |
| 26 |
|
| 27 |
**Topics:** |
| 28 |
|
| 29 |
- Setting configuration values |
| 30 |
- Persistence to file |
| 31 |
- Verifying changes on disk |
| 32 |
|
| 33 |
**Run:** |
| 34 |
|
| 35 |
```bash |
| 36 |
go run -tags=examples examples/02-write-persist.go |
| 37 |
``` |
| 38 |
|
| 39 |
### 3. [Understanding Scopes](03-scopes.go) |
| 40 |
|
| 41 |
Demonstrates the configuration scope hierarchy and how gitconfig resolves values across scopes. |
| 42 |
|
| 43 |
**Topics:** |
| 44 |
|
| 45 |
- System-wide config |
| 46 |
- User config |
| 47 |
- Local repository config |
| 48 |
- Environment variables |
| 49 |
- Scope priority/precedence |
| 50 |
|
| 51 |
**Run:** |
| 52 |
|
| 53 |
```bash |
| 54 |
go run -tags=examples examples/03-scopes.go |
| 55 |
``` |
| 56 |
|
| 57 |
### 4. [Custom Paths](04-custom-paths.go) |
| 58 |
|
| 59 |
Shows how to work with custom configuration file paths instead of default Git locations. |
| 60 |
|
| 61 |
**Topics:** |
| 62 |
|
| 63 |
- Custom config paths |
| 64 |
- Non-standard locations |
| 65 |
- Loading from arbitrary files |
| 66 |
|
| 67 |
**Run:** |
| 68 |
|
| 69 |
```bash |
| 70 |
go run -tags=examples examples/04-custom-paths.go |
| 71 |
``` |
| 72 |
|
| 73 |
### 5. [Error Handling](05-error-handling.go) |
| 74 |
|
| 75 |
Demonstrates proper error handling patterns when working with gitconfig. |
| 76 |
|
| 77 |
**Topics:** |
| 78 |
|
| 79 |
- Parse errors |
| 80 |
- File not found |
| 81 |
- Permission errors |
| 82 |
- Invalid key formats |
| 83 |
|
| 84 |
**Run:** |
| 85 |
|
| 86 |
```bash |
| 87 |
go run -tags=examples examples/05-error-handling.go |
| 88 |
``` |
| 89 |
|
| 90 |
### 6. [Include Files](06-includes.go) |
| 91 |
|
| 92 |
Shows how gitconfig handles include directives for modular configuration. |
| 93 |
|
| 94 |
**Topics:** |
| 95 |
|
| 96 |
- Including other config files |
| 97 |
- Conditional includes |
| 98 |
- External config organization |
| 99 |
|
| 100 |
**Run:** |
| 101 |
|
| 102 |
```bash |
| 103 |
go run -tags=examples examples/06-includes.go |
| 104 |
``` |
| 105 |
|
| 106 |
## Prerequisites |
| 107 |
|
| 108 |
Before running these examples, ensure you have Go 1.24 or later installed: |
| 109 |
|
| 110 |
```bash |
| 111 |
go version |
| 112 |
``` |
| 113 |
|
| 114 |
## How to Use These Examples |
| 115 |
|
| 116 |
1. Each example is a standalone Go file |
| 117 |
2. Run with `go run -tags=examples examples/<number>-<name>.go` |
| 118 |
3. Some examples create temporary files for demonstration |
| 119 |
4. Review the source code to understand each pattern |
| 120 |
5. Modify and experiment to learn more |
| 121 |
|
| 122 |
## Learning Path |
| 123 |
|
| 124 |
Recommended order for learning: |
| 125 |
|
| 126 |
1. Start with **01-basic-read** to understand basic usage |
| 127 |
2. Move to **02-write-persist** to learn mutation |
| 128 |
3. Learn scope hierarchy with **03-scopes** |
| 129 |
4. Explore flexibility with **04-custom-paths** |
| 130 |
5. Master error handling with **05-error-handling** |
| 131 |
6. Build modular configs with **06-includes** |
| 132 |
|
| 133 |
## Common Patterns |
| 134 |
|
| 135 |
### Reading a Single Value |
| 136 |
|
| 137 |
```go |
| 138 |
cfg, _ := gitconfig.NewConfig("path/to/.git/config") |
| 139 |
value, ok := cfg.Get("user.name") |
| 140 |
if ok { |
| 141 |
fmt.Println("Name:", value) |
| 142 |
} |
| 143 |
``` |
| 144 |
|
| 145 |
### Setting and Saving |
| 146 |
|
| 147 |
```go |
| 148 |
cfg, _ := gitconfig.NewConfig("path/to/.git/config") |
| 149 |
cfg.Set("core.editor", "vim") |
| 150 |
_ = cfg.String() // Persist changes |
| 151 |
``` |
| 152 |
|
| 153 |
### Working with All Scopes |
| 154 |
|
| 155 |
```go |
| 156 |
cfg, _ := gitconfig.NewConfigs() // Load all scopes |
| 157 |
value := cfg.Get("user.name") // Respects scope priority |
| 158 |
cfg.SetLocal("core.pager", "less") // Write to local only |
| 159 |
``` |
| 160 |
|
| 161 |
## More Information |
| 162 |
|
| 163 |
- [README](../README.md) - Overview and quick start |
| 164 |
- [ARCHITECTURE](../ARCHITECTURE.md) - Design and structure |
| 165 |
- [CONTRIBUTING](../CONTRIBUTING.md) - How to contribute |
| 166 |
- [godoc](../doc.go) - API documentation |